更改字符串颜色(长度不确定,有服务器返回)

            cell.Content.text = [NSString stringWithFormat:@"回复 %@:%@",item.nickname,item.content];

            //将前面一部分字更改为灰色

            NSString *str=cell.Content.text;

            NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:str];

            NSString *str1=item.nickname;

            NSInteger a=str1.length;

            [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:163.0/255 green:163.0/255 blue:163.0/255 alpha:1]  range:NSMakeRange(0, a+4)];

            cell.Content.attributedText = AttributedStr;

原文地址:https://www.cnblogs.com/-ios/p/5777361.html