您的位置:首页 > 其它

改变字符串中指定字符的颜色

2017-04-13 16:30 447 查看
- (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]];

 效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: