UILabel显示不同的字体和颜色

来源:互联网 发布:python服务器日志分析 编辑:程序博客网 时间:2024/04/27 17:24

源代码

#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;    }}

 

效果图

0 0