NSError *error;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"@[\u4e00-\u9fa5a-z0-9]{1,30}[:|\\s]{0,1}"
options:0
error:&error];
if (!error) { // 如果没有错误
// 获取特特定字符串的范围
NSArray *array1 = [regex matchesInString:cell.dongContentLable.text options:0 range:NSMakeRange(0, cell.dongContentLable.text.length)];
NSLog(@"%ld",[array1 count]);
if ([array1 count]) {
for (NSTextCheckingResult *objc in array1) {
NSRange range = objc.range;
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithAttributedString:cell.dongContentLable.attributedText];
[attribute addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexCode:@"008ff3"] range:NSMakeRange(range.location, range.length - 1)];
cell.dongContentLable.attributedText = attribute;
NSLog(@"%@",NSStringFromRange(objc.range));
}
}
} else { // 如果有错误,则把错误打印出来
NSLog(@"error - %@", error);
}
本文介绍如何使用Objective-C中的正则表达式NSRegularExpression进行文本模式匹配,并通过NSMutableAttributedString实现匹配文本的高亮显示。
1998

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



