关键字数组:
- (NSAttributedString *)colorAttributeString:(NSString *)sourceString sourceSringColor:(UIColor *)sourceColor sourceFont:(UIFont *)sourceFont keyWordArray:(NSArray<NSString *> *)keyWordArray keyWordColor:(UIColor *)keyWordColor keyWordFont:(UIFont *)keyWordFont{
if (sourceString == nil || ![sourceString isKindOfClass:[NSString class]]) {
sourceString = @"";
}
NSMutableArray *muKeyWordsArr;
if (keyWordArray == nil) {
muKeyWordsArr = [NSMutableArray arrayWithCapacity:0];
}else{
muKeyWordsArr = keyWordArray.mutableCopy;
}
if (sourceColor == nil) {
sourceColor = [UIColor blackColor];
}
if (keyWordColor == nil) {
keyWordColor = [UIColor blackColor];
}
if (sourceFont == nil) {
sourceFont = [UIFont systemFontOfSize:15];
}
if (keyWordFont == nil) {
keyWordFont = [UIFont systemFontOfSize:15];
}
NSMutableAttributedString *attributeContent;
NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[ps setLineBreakMode:NSLineBreakByTruncatingTail];
NSDictionary *attriDic = @{NSFontAttributeName : sourceFont,NSForegroundColorAttributeName : sourceColor,NSParagraphStyleAttributeName : ps};
attributeContent = [[NSMutableAttributedString alloc] initWithString:sourceString attributes:attriDic];
[muKeyWordsArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *searchKey = (NSString *)obj;
NSError *error = nil;
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:searchKey options:NSRegularExpressionCaseInsensitive error:&error];
if (!expression) {
}else{
[expression enumerateMatchesInString:sourceString options:NSMatchingReportProgress range:NSMakeRange(0, sourceString.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[attributeContent addAttributes:@{NSFontAttributeName : keyWordFont,NSForegroundColorAttributeName:keyWordColor} range:result.range];
} ];
}
}];
return attributeContent;
}
调用:
- (void)colorText{
UILabel *label = [UILabel new];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 0;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(15);
make.right.equalTo(self.view).offset(-15);
make.centerY.equalTo(self.view);
}];
NSString *source = @"1、你是东西,我是南北,我们只要在一起就好了 ----烽火戏诸侯\n2、情到深处,知悔不愿悔。 ----烽火戏诸侯\n3、食君之禄,忠君之事,两不相欠。我张巨鹿最后跟天下百姓无非是要了一壶酒喝,不算多吧? ----烽火戏诸侯\n4、世间人,纵有不舍,终有离别。世间事,纵有遗憾,且放心间。 ----烽火戏诸侯\n5、提壶绿蚁酒。\n倒酒在棋盘。倒尽了壶中绿蚁,独处一室的徐凤年泪流满面,哽咽道:“师父,你让我以后带酒给谁喝?” ----徐凤年\n6、独走独停独自坐,手上青蛇掠白线。独人独衫独自剑,剑尖锋芒生三千。世间无人能识我,只是冷眼笑疯癫。唯有山鬼与龙王,知是神仙在眼前。 ----烽火戏诸侯\n7、既说用人不疑疑人不用。又说防人之心不可无。反正天底下的道理都给说光了,但道理太多,也就其实等于没说。 ----烽火戏诸侯\n8、我有四十年郁气出不得。\n";
NSArray *array = [NSArray arrayWithObjects:@"烽火",@"徐凤年", nil];
NSAttributedString *attri2 = [self colorAttributeString:source sourceSringColor:[UIColor blackColor] sourceFont:[UIFont systemFontOfSize:15] keyWordArray:array keyWordColor:[UIColor orangeColor] keyWordFont:[UIFont systemFontOfSize:20]];
label.attributedText = attri2;
}
显示效果:

IMG_2037.PNG
网友评论