#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;
}
属性字符串固定字匹配
正则表达式与NSAttributedString实战
最新推荐文章于 2024-10-07 20:12:23 发布
本文介绍如何使用正则表达式与NSAttributedString处理字符串颜色,通过实例演示如何为匹配到的文本添加颜色属性,适用于iOS开发中对文本颜色进行动态控制的需求。
607

被折叠的 条评论
为什么被折叠?



