- 在开发中,有时会遇到某字符串字体跟其他的不一样的需求
第一个字的字号会不一样
这个时候,其实用一个UILabel就可以轻松搞定了,方法如下:
- (void)smallRMBByLabel:(UILabel *)label font:(int)font
{
//label 需要操作的Label
//font 该字符的字号
NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSRange stringRange = NSMakeRange(0, 1); //该字符串的位置
[noteString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:stringRange];
[label setAttributedText: noteString];
}
- 而有时也会遇到同一个Label展示不同颜色的需求
![](https://i-blog.csdnimg.cn/blog_migrate/d6a8869b2842a227213b854fec73b3dd.webp?x-image-process=image/format,png)
部分字符颜色不一
不啰嗦,直接上干货
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"照片中信息真实有效且清晰可见,包括手持证件人的五官、身份证上的所有信息(请看三遍再上传图片噢)"];
NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].location, [[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].length);
//需要设置的位置
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
//设置颜色
[label setAttributedText:noteStr];
搞定,收工。
文/余荣景(简书作者)
原文链接:http://www.jianshu.com/p/7ad6d1ccdb0b
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文链接:http://www.jianshu.com/p/7ad6d1ccdb0b
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。