屬性字符串固定字匹配

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章