我正在尝试将属性字符串设置为 UITableViewCell 的文本标签 我正在使用以下代码
I am trying to set attributed string to UITableViewCell's text label I am using following code
NSDictionary * dic = [arrayForTableView objectAtIndex:indexPath.row];
NSNumber* lostPets = [dic valueForKey:@"lostPets"];
NSNumber * foundPets = [dic valueForKey:@"foundPets"];
NSString * breed = [dic valueForKey:@"breed"];
NSMutableAttributedString* message = [[NSMutableAttributedString alloc] init];
//set text
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ (",breed]]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[foundPets stringValue] attributes:@{
NSFontAttributeName : [UIColor greenColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"), ("]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[lostPets stringValue] attributes:@{
NSFontAttributeName : [UIColor redColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@")"]];
cell.textLabel.attributedText = message;
当我 运行 代码时,我得到以下异常
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor ctFontRef]: unrecognized selector sent to instance
我尝试调试代码,但无法找到崩溃点。
为什么要将颜色传递给字体属性?
您正在创建这本词典:
@{ NSFontAttributeName : [UIColor greenColor] }
这两个不匹配。将 NSFontAttributeName
替换为 NSForegroundColorAttributeName
或将 [UIColor greenColor]
替换为适当的 UIFont
引用。更改取决于您的需要。您是要设置颜色还是设置字体?
NSDictionary * dic = [arrayForTableView objectAtIndex:indexPath.row];
NSNumber* lostPets = [dic valueForKey:@"lostPets"];
NSNumber * foundPets = [dic valueForKey:@"foundPets"];
NSString * breed = [dic valueForKey:@"breed"];
NSMutableAttributedString* message = [[NSMutableAttributedString alloc] init];
//set text
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ (",breed]]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[foundPets stringValue] attributes:@{
NSFontAttributeName : [UIColor greenColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"), ("]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:[lostPets stringValue] attributes:@{
NSFontAttributeName : [UIColor redColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@")"]];
cell.textLabel.attributedText = message;
当我 运行 代码时,我得到以下异常
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor ctFontRef]: unrecognized selector sent to instance
我尝试调试代码,但无法找到崩溃点。
为什么要将颜色传递给字体属性?
您正在创建这本词典:
@{ NSFontAttributeName : [UIColor greenColor] }
这两个不匹配。将 NSFontAttributeName
替换为 NSForegroundColorAttributeName
或将 [UIColor greenColor]
替换为适当的 UIFont
引用。更改取决于您的需要。您是要设置颜色还是设置字体?