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

- (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]];



 效果: 

你可能感兴趣的:(错误处理+小技术点)