/*
首先我們使用正則去匹配找到對應字符位置:假設我們要找以下字符串中的本人兩個字,并且顯示到Label上大小為16號字,那么,看代碼吧,寫的很清晰。
*/
NSString * str = @"哈哈本人在這里測試一遍,本人是在測驗,本人在寫代碼";
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:str];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\u672c\u4eba]" options:0 error:nil];
NSArray *matches = [regex matchesInString:str options:0 range:NSMakeRange(0,str.length)];
for(NSTextCheckingResult *result in [matches objectEnumerator]) {
NSRange matchRange = [result range];
[attrString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.f]} range:matchRange];
}
UILabel * label = [[UILabel alloc] init];
label.attributedText = attrString;
好了,就這些吧!