#pragma mark 属性字符串颜色处理
- (NSMutableAttributedString *)manageSuperString:(NSString *)superString withRegExpStr:(NSString *)regExpStr {
// 正则
NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:regExpStr
options:NSRegularExpressionCaseInsensitive
error:nil];
// 获取字符串中所有匹配到的数组
NSArray *array = [regExp matchesInString:superString options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, superString.length)];
#pragma mark 创建属性字符串
NSMutableAttributedString *AttrStr = [[NSMutableAttributedString alloc] initWithString:superString];
for (NSTextCheckingResult *result in array) {
//改变字体的颜色
[AttrStr addAttribute:NSForegroundColorAttributeName value:RGB_COLOR(0, 168, 255) range:result.range]; //后边留一个字符不变
}
return AttrStr;
}
属性字符串固定字匹配
最新推荐文章于 2023-09-18 14:41:14 发布