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;
当我运行代码时,我得到了异常
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [UICachedDeviceRGBColor ctFontRef]:无法识别的选择器发送到实例
我尝试调试代码但无法获得崩溃点。
答案 0 :(得分:2)
为什么要将颜色传递给字体属性?
您正在创建此词典:
@{ NSFontAttributeName : [UIColor greenColor] }
那两个不匹配。将NSFontAttributeName
替换为NSForegroundColorAttributeName
或将[UIColor greenColor]
替换为正确的UIFont
引用。变化取决于您的需求。要设置颜色还是设置字体?