iOS开发

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

2017-01-09  本文已影响33人  镌写不一样的人生

+(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;

}

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

上一篇 下一篇

猜你喜欢

热点阅读