OC字符串替换

05/20 10:15
阅读数 76

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

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部