在TextView中为文本添加颜色

问题描述:

我有一个TextView,其中需要一些文本来更改颜色和字体。任何方法都可以使用核心文本。请帮忙。在TextView中为文本添加颜色

+0

你想改变所有文本一些文本,对不对? – Dilip 2013-02-26 14:53:12

+0

您应该通过搜索互联网轻松找到问题的答案。 – bdesham 2013-02-26 15:00:39

我需要一些文字来改变颜色和字体。

你肯定找NSAttributedString


Source

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; 
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)]; 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)]; 
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)]; 
label.attributedText = str; 
+0

不错的代码..只能将**标签**更改为** textView ** :) – pbibergal 2013-02-26 14:55:23

+0

谢谢Anoop Vaidya – Deepak 2013-02-27 12:31:49

+0

@Bing:感觉很高兴接受并赞扬:) – 2013-02-27 13:09:48