关于如何在一个UILabel中实现不一样字体和颜色的问题一直困扰了我好久,以前一直想着如何自定义一个UILabelView来实现,结果老是失败,知道最近我深刻接触了NSMutableAttributedString以后,才发现要实现它原来是那么的简单。字体
遥想实现它,咱们得换一种思路,那就是从要输入的字符串下手,而不是一味的从UILabel找突破。那好,一个例子就能够说明一切问题:spa
NSString *title = @"Please rank from most to least the personality below you like your partner to have";
code
NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:title];
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];
orm
[lblTitle setAttributedText:string];字符串
好了,以上实例叫咱们如何来给一个UILableView同事设定不一样的颜色和字体。string