label显示不同颜色的字体

    NSString *contentSrt = [NSString stringWithFormat:@"%@ (%@)",categoryModel.categoryName, categoryModel.phoneCount];
    NSRange rang = [contentSrt rangeOfString:categoryModel.categoryName];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:contentSrt];
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:53.0/255.0 green:53.0/255.0 blue:53.0/255.0 alpha:1.0] range:NSMakeRange(0, contentSrt.length)];
    NSRange lightRang = NSMakeRange(rang.length, contentSrt.length - rang.length);
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor themeLightGrayColor] range:lightRang];

    contentLabel.attributedText = attStr;
    contentLabel.highlightedTextColor = [UIColor whiteColor];

posted @ 2017-05-19 17:21  clnchanpin  阅读(236)  评论(0)    收藏  举报