/*首先我们使用正则去匹配找到对应字符位置:假设我们要找以下字符串中的本人两个字,并且显示到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;
好了,就记这些吧。