您的位置:首页 > 产品设计 > UI/UE

UILabel显示不同的字体和颜色

2015-09-23 16:50 423 查看

源代码

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




效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: