UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 100, 200, 100);
label.textColor = [UIColor blackColor];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSRange range = NSMakeRange(0, [[str string] rangeOfString:@"5"].location);
//NSRange range = NSMakeRange(0,6); //这里是设置从第一个字符到第六个字符
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
[label setAttributedText:str] ;
[label sizeToFit];
[self.view addSubview:label];
下面研究下AttributedString究竟可以设置哪些属性,具体来说,有以下21个:
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色&