自定义字符串中 某个字符串的大小、颜色


你想实现这样的?

自定义字符串中 某个字符串的大小、颜色_第1张图片
需求是这样的!


或者是这样的!

有时候,需要在某个Lable中添加一行话,大多情况要在后边跟个单位,有的同学会直接在后边添加一个Lable做单位显示,完美实现!

but,是不是太麻烦?

现在给你一个完美解决的办法,直接上代码:




NSMutableAttributedString*str = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"%.2fkm",[self.currentRecordtotalDistance]/1000]];

//设置自定义位置字符串大小颜色

[straddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(0,2)];

//设置自定义位置字符串大小

[straddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Helvetica-Bold"size:5]range:NSMakeRange(str.length-2,2)];

//然后,我们创建个UILabel对象进行测试

你的UILabel.attributedText=str;

OK!完美,别忘了给个赞哦

你可能感兴趣的:(自定义字符串中 某个字符串的大小、颜色)