OC字符串替换

一笑奈何 提交于 2020-08-12 03:02:18

- (NSMutableAttributedString *)attributedString:(NSString *)string{
    
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:HEXCOLOR(0x999999)}];

        //遍历字符串中指定字符的所有位置
        NSRange isRange = [attributedString.string rangeOfString:self.keyword];
        while (isRange.location != NSNotFound || isRange.length != 0) {
            //设置指定字符的属性
            [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor rgb:BK_Color] range:isRange];
            //字体背景颜色
    //        [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor rgb:BK_Color] range:isRange];
            //重设指定字符的位置
            NSRange searchIsRange = NSMakeRange(isRange.location + isRange.length, attributedString.string.length - (isRange.location + isRange.length));
            isRange = [attributedString.string rangeOfString:self.keyword options:NSCaseInsensitiveSearch range:searchIsRange];
        }

    return attributedString;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!