我有一个显示推文的UITextView。一些推文有主题标签。我在StackOverflow上找到了一个很好的答案,它允许我识别主题标签,然后对其应用颜色。但是由于某种原因,我的UITextView的attributionText属性没有被应用....这是我的代码:
NSString *message = final_data[index];
NSArray *words = [message componentsSeparatedByString:@" "];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:message];
for (NSString *word in words) {
if ([word hasPrefix:@"#"]) {
NSRange matchRange = [message rangeOfString:word];
[attrString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor blueColor] range:matchRange];
}
}
cell_tw.message_post.attributedText = attrString;
“message_post”是我的UITableview中自定义单元格中的UITextView。
虽然我感觉属性textText属性的全部内容都可以设置这些类型的设置,例如文本颜色。
这里出了什么问题?
谢谢你的时间:)
答案 0 :(得分:2)
[attrString setAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] range:[message rangeOfString:word]];
谢谢!