ios - UITextView 中的属性文本不起作用

标签 ios uitextview

我正在使用下面的代码尝试在我的 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/

相关文章:

ios - TableView 滚动/dequeueReusableCell 问题

ios - 添加 watchOS 版本后无法启动应用程序

ios - NSPredicate 不断使我的应用程序崩溃

ios - 没有动画 UITableViewCell 选择

ios - TextView 无法正确调整大小

ios - Swift:如何使 UITableViewCell 高度适应其中的 UITextView

swift - 如何更改不同文件中变量的值?

iphone - NavigationBar隐藏TabBarViewController中的TableView

ios - Swift 2.1 - 将信息输入到同时填充文本字段的 TextView 中?

objective-c - 成为第一响应者后如何识别点击 TextView ?