继承UILabel ,重写text的set方法,上代码:
- (void)setText:(NSString *)text {
[super setText:text];
//将数字变成蓝色
NSString *labelStr = self.text; //初始化string为传入label.text的值
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSInteger remainSeconde = [[labelStr stringByTrimmingCharactersInSet:nonDigits] integerValue];//获取过滤出来的数值
NSString *stringRange = [NSString stringWithFormat:@"%ld",(long)remainSeconde];//将过滤出来的Integer的值转换成String
NSRange range = [labelStr rangeOfString:stringRange];//获取过滤出来的数值的位置
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:self.text];
[attrStr addAttribute:NSForegroundColorAttributeName value:AB_Color_0084FF range:range];
self.attributedText = attrStr;
}
网友评论