【问题标题】:UITextView attributed colour not being applied - iOS未应用 UITextView 属性颜色 - iOS
【发布时间】:2014-03-18 16:06:23
【问题描述】:

我有一个显示推文的 UITextView。一些推文有标签。我在 StackOverflow 上找到了一个很好的答案,它允许我识别主题标签,然后对其应用颜色。然而,我的 UITextView 的属性文本属性没有被应用....由于某种原因。这是我的代码:

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。

尽管属性文本属性的全部意义在于您可以设置这些类型的设置,例如文本颜色。

这里出了什么问题?

【问题讨论】:

    标签: ios cocoa-touch text uicolor hashtag


    【解决方案1】:

    你试过了吗?

    [attrString setAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] range:[message rangeOfString:word]];
    

    【讨论】:

    • 效果很好!!!!!!非常感谢。只要 StackOverflow 允许我,我就会勾选这个答案:)
    猜你喜欢
    • 2022-01-03
    • 2015-02-24
    • 1970-01-01
    • 2012-05-13
    • 2016-05-05
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多