UITextView归因颜色未应用 - iOS [英] UITextView attributed colour not being applied - iOS

查看:139
本文介绍了UITextView归因颜色未应用 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextView显示tweets。一些tweet有hashtag。我发现一个很好的答案StackOverflow,它允许我识别的标签,然后应用一种颜色。然而,我的UITextView的属性Text属性没有被应用....出于某种原因。这是我的代码:

  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。



我虽然attributText属性的整个点是你可以设置这些种类的设置,如文本颜色。



感谢您的时间:)

解决方案


$ b <

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

谢谢!


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 :)

解决方案

have you tried this?

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

Thanks!

这篇关于UITextView归因颜色未应用 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆