NSArray* numbers =@[@"→"];
NSMutableAttributedString*stringss = [[NSMutableAttributedStringalloc]initWithString:nameArrayM[indexPath.row]];
for(inti =0; i < nameText.length; i ++) {
//这里的小技巧,每次只截取一个字符的范围
NSString*a = [nameTextsubstringWithRange:NSMakeRange(i,1)];
//判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符,从而筛选出符合要求的数字字符的范围NSMakeRange
if([numberscontainsObject:a]) {
[stringsssetAttributes:@{NSForegroundColorAttributeName:[UIColorcolorWithHexString:@"f4f5f6"],NSFontAttributeName:[UIFontsystemFontOfSize:15]}range:NSMakeRange(i,1)];
}
}
cell.name.attributedText= stringss;
网友评论
NSMutableArray *arrayRanges = [NSMutableArray array];
//方法一、NSRegularExpression ,这个最快
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:subString options:0 error:nil];
NSArray *matches = [regex matchesInString:totalString options:0 range:NSMakeRange(0, totalString.length)];
for(NSTextCheckingResult *result in matches){
NSRange matchRange = [result range];
[arrayRanges addObject:[NSNumber valueWithRange:matchRange]];
}
return arrayRanges;
这是我用的,还有别的方法在:http://www.jianshu.com/p/11a57360d261