在这里,我们先封装一个方法
-(void)setAttributedTextColor:(UIColor *)color range:(NSRange)range
{
NSMutableAttributedString *str = [[NSMutableAttributedStringalloc] initWithAttributedString:self.attributedText];
[str addAttribute:NSForegroundColorAttributeNamevalue:colorrange:range];
self.attributedText = str;
}
接下来直接使用
_nameLabel.text = [[NSStringalloc] initWithFormat:@"购买商品消费¥%.2f元",amount];
[_nameLabelsetAttributedTextColor:[UIColor blackColor] range:NSMakeRange(0,7)];
[_nameLabelsetAttributedTextColor:[UIColor redColor] range:NSMakeRange(7, name.length)];
[_nameLabelsetAttributedTextColor:DEF_BlackColorrange:NSMakeRange(_nameLabel.text.length-1,1)];