天天看点

ios - 设置文本中不同字体颜色

- (void)viewDidLoad {
NSRange range = [orderPriceLabel.text rangeOfString:@"付款金额:"];
    [self setTextColor:orderPriceLabel FontNumber:_kFontMain AndRange:range AndColor:kColor262626];
    }


//设置不同字体颜色
-(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;
}