Ios 自定义tableviewcell中UILabel的动态数量
我创建了一个自定义tableview单元格,并希望在该单元格中显示一些字符串。我从后端获取字符串,因此我不知道需要多少标签。我尝试将字符串合并到一个标签中,实现方式如下,但我希望在char之后显示具有不同属性的字符串“:” 我无法更改位于char“:”之后的字符串的属性。Ios 自定义tableviewcell中UILabel的动态数量,ios,objective-c,uitableview,cocoa-touch,Ios,Objective C,Uitableview,Cocoa Touch,我创建了一个自定义tableview单元格,并希望在该单元格中显示一些字符串。我从后端获取字符串,因此我不知道需要多少标签。我尝试将字符串合并到一个标签中,实现方式如下,但我希望在char之后显示具有不同属性的字符串“:” 我无法更改位于char“:”之后的字符串的属性。 有办法吗?我可以在单元格中创建动态数量的标签,还是只更改仅位于“:”之后的字符串的属性?听起来您想更改属性,例如UILabel中文本部分的格式。您可以通过以下方式完成此操作: 创建字符串的属性化、可变副本(即将NSString
有办法吗?我可以在单元格中创建动态数量的标签,还是只更改仅位于“:”之后的字符串的属性?听起来您想更改属性,例如UILabel中文本部分的格式。您可以通过以下方式完成此操作:
attributedText
属性设置为属性字符串NSString *stringOne = @"My name is ";
NSString *stringTwo = @"John Citizen";
NSString *joinedStrings = [stringOne stringByAppendingString:stringTwo];
NSRange rangeOfStringTwo = [joinedStrings rangeOfString:stringTwo];
NSString *myString = @"This is my string";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:myString];
NSMutableAttributedString *mutableAttributedString = [attributedString mutableCopy];
// The range of text to change, i.e. start from the 5th index
// (starting from 0 like arrays), and continue for 2 characters:
NSRange rangeOfSecondWord = NSMakeRange(5, 2);
// The list of attributes to apply to that range:
NSDictionary *myAttributes = @{
NSForegroundColorAttributeName : [UIColor redColor],
};
// Actually apply the attributes:
[mutableAttributedString setAttributes:myAttributes range:rangeOfSecondWord];
// Set the text of the label to the attributed string:
myLabel.attributedText = mutableAttributedString;
NSString *stringOne = @"My name is ";
NSString *stringTwo = @"John Citizen";
NSString *joinedStrings = [stringOne stringByAppendingString:stringTwo];
NSRange rangeOfStringTwo = [joinedStrings rangeOfString:stringTwo];