美文网首页iOS开发
iOS开发中给字符串中多出重复字符变色

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

作者: 镌写不一样的人生 | 来源:发表于2017-01-09 15:32 被阅读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;

    }

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

    相关文章

      网友评论

        本文标题:iOS开发中给字符串中多出重复字符变色

        本文链接:https://www.haomeiwen.com/subject/mqlhbttx.html