标签:
1 2 NSString * str =@"您一共购买2件商品,消费总额为:196"; 3 // 搜索文字 4 NSString *tempStr = @"\\d{1,}"; 5 6 NSMutableArray *results = [NSMutableArray array]; 7 NSRange searchRange = NSMakeRange(0, [str length]); 8 NSRange range; 9 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str]; 10 while ((range = [str rangeOfString:tempStr options:NSRegularExpressionSearch range:searchRange]).location != NSNotFound) 11 { 12 [results addObject:[NSValue valueWithRange:range]]; 13 searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range)); 14 [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; 15 } 16 summary.attributedText = string;
效果如下图:
标签:
原文地址:http://www.cnblogs.com/angongIT/p/4180240.html