源代码
#pragma mark 改变描述信息某些字的颜色
- (void)changeDescTextColor:(NSString *)desc color:(UIColor *)color
{
if (desc == nil) {
return;
}
NSRange range = [self.descLabel.text rangeOfString:desc];
if (range.location != NSNotFound)// 找到
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.descLabel.text];
NSRange allRange = NSMakeRange(0, self.descLabel.text.length);
[str addAttribute:NSForegroundColorAttributeName value:self.descLabel.textColor range:allRange];// 默认色
[str addAttribute:NSFontAttributeName value:self.descLabel.font range:allRange];// 默认字体
[str addAttribute:NSForegroundColorAttributeName value:color range:range];// 可变区域色彩替换
// 加载新的样式
self.descLabel.attributedText = str;
}
}