前些天做了数据处理,其中涉及了字符串部分改变颜色,代码贴出来,还请大家指点哈:
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];
NSString *contentStr = @"xx:hello world";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:contentStr];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(0, 3)];
label.attributedText = str;