关于如何在一个UILabelView中实现不同字体和颜色的问题一直困扰了我很久,之前一直想着如何自定义一个UILabelView来实现,结果总是失败,知道最近我深入接触了NSMutableAttributedString之后,才发现要实现它原来是那么的简单。
遥想实现它,我们得换一种思路,那就是从要输入的字符串下手,而不是一味的从UILabelView找突破。那好,一个例子就可以说明一切问题:
NSString *title = @"Please rank from most to least the personality below you like your partner to have";
NSRange range1, range2;
range1 = NSMakeRange (17, 13);//通过NSRange来划分片段
range2 = NSMakeRange (62, 12);
UIColor * color1 = TITLE_TEXT_COLLOR ; //TITLE_TEXT_COLLOR 是我自定义的颜色
[string addAttribute : NSForegroundColorAttributeName value :color1 range :range1];//给不同的片段设置不同的颜色
[string addAttribute : NSForegroundColorAttributeName value :color1 range :range2];
[string addAttribute : NSFontAttributeName value :[ UIFont fontWithName : @"HelveticaNeueLTStd-BdCn" size :15] range :range1];//给不同的片段设置不同的字体
[string addAttribute : NSFontAttributeName value :[ UIFont fontWithName : @"HelveticaNeueLTStd-BdCn" size :15] range :range2];
[lblTitle setAttributedText:string];
好了,以上实例叫我们如何来给一个UILableView同事设定不同的颜色和字体。