1首选初始化对象,正则表达式选取0到9的数字范围
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9]" options:0 error:nil];
2获取查询结果,得到的数组里面有一行文本中数字的范围
NSArray *numArr = [regex matchesInString:content options:0 range:NSMakeRange(0, [content length])];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content attributes:@{NSForegroundColorAttributeName:numColor}];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0,[content length])];
3循环找到数字的范围用富文本来给这行文本赋值
for (NSTextCheckingResult *attirbute in numArr) {
[attributedString setAttributes:@{NSForegroundColorAttributeName:color} range:attirbute.range];
}
return attributedString;
4效果如下所示