UITextView attributed colour not being applied - iOS

£可爱£侵袭症+ 提交于 2019-12-08 11:26:27

问题


I have a UITextView which displays tweets. Some tweets have hashtags. I found a great answer on StackOverflow which allows me to identify the hashtag and then apply a colour to it. However the attributedText property of my UITextView is not being applied.... for some reason. Here is my code:

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" is my UITextView in my Custom Cell in my UITableview.

I though the whole point of the attributedText property is so you can set these kinds of settings such as text colour.

What is going wrong here?

Thanks for you're time :)


回答1:


have you tried this?

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

Thanks!



来源:https://stackoverflow.com/questions/22484780/uitextview-attributed-colour-not-being-applied-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!