我正在使用下面的代码尝试在我的 UITextView
中设置文本子字符串的颜色,但它不起作用。看起来应该很简单;我错过了什么?
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:10];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
[title addAttribute:kCTForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
descriptionCommentLabel.attributedText = title;
最佳答案
您应该将 kCTForegroundColorAttributeName
替换为 NSForegroundColorAttributeName
。您可以在该文件的常量部分检查有效属性列表:Apple docs: NSAttributedString UIKit additions .您的代码现在是:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:10];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
[title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
当 UIKit 不支持 NSAttributedString
并且您不得不使用 CoreText 时,kCT...
属性用于 iOS 5 和更早的版本。
关于ios - UITextView 中的属性文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448129/