iOS开发中给字符串中多出重复字符变色

+(NSMutableAttributedString*)changeTextColorWith:(NSString*)keyStr AndSourceStr:(NSString*)sourceStr

{

NSMutableAttributedString*changeStr = [[NSMutableAttributedStringalloc]initWithString:sourceStr];

if([sourceStrcontainsString:keyStr]) {

NSArray*commpentArr = [sourceStrcomponentsSeparatedByString:keyStr];

NSIntegerlocation =0;

for(inti=0;i < commpentArr.count-1;i++) {

NSString*str = commpentArr[i];

location += (i >0? (str.length+keyStr.length):str.length);

[changeStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(location,keyStr.length)];

}

}

returnchangeStr;

}

这里颜色是变成红色,若想自定义颜色传进参数即可......

你可能感兴趣的:(iOS开发中给字符串中多出重复字符变色)