- (void)viewDidLoad {
NSRange range = [_amountLabel.text rangeOfString:@"0.00"];
[self setTextColor:_amountLabel FontNumber:[UIFont systemFontOfSize:13] AndRange:range AndColor:[UIColor orangeColor]];
}
//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
//设置字号
[str addAttribute:NSFontAttributeName value:font range:range];
//设置文字颜色
[str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
label.attributedText = str;
}
例如:356.00元 只显示数字是红色 元是原来的色调
NSRange range2 = [cell.moneyLabel.text rangeOfString:@"元"];
NSRange range22 = NSMakeRange (0,range2.location);
[self setTextColor:cell.moneyLabel FontNumber:[UIFont systemFontOfSize:15] AndRange:range22 AndColor:[UIColor redColor]];
效果:
改变字符串中指定字符的颜色
最新推荐文章于 2023-05-10 17:52:11 发布
本文介绍了一种在iOS开发中使用UILabel时,如何通过Objective-C代码实现对UILabel内特定文本进行不同字体颜色设置的方法。具体包括如何选取文本范围、设置字体大小及颜色等。
292

被折叠的 条评论
为什么被折叠?



