iOS:UILabel设置不同字体颜色

NSString *str = @"0123456789”;//label内容
NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:str];
//根据下标索引设置字体颜色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
//根据下标索引设置字体样式大小(式样只对字母有效)
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
_myLabel.attributedText = str1;

//以上方法为iOS6之后,iOS6之前需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架

你可能感兴趣的:(IOS:UI设计)