宽容他人,放过自己。

属性字符串固定字匹配

Posted on By anchoriteFili

#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;
}