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

    xiaoxiao2021-04-12  73

    - (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]];  效果:
    转载请注明原文地址: https://ju.6miu.com/read-667662.html

    最新回复(0)