美文网首页
iOS 富文本 遍历字符串包含的关键词并修改颜色

iOS 富文本 遍历字符串包含的关键词并修改颜色

作者: 秋叶红90 | 来源:发表于2021-07-08 19:14 被阅读0次

    字符串分类

    -(NSMutableAttributedString *)keyWords:(NSString *)keyWords withKeyWordsColor:(UIColor *)color
    {
        
        NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:self];
        if (color == nil) {
            color = [UIColor colorWithHexString:@"52c683"];
        }
        
        if (keyWords.length<=0) {
             return mutableAttributedStr;
        }
        
        for (NSInteger j=0; j<=keyWords.length-1; j++) {
            
            NSRange searchRange = NSMakeRange(0, [self length]);
            NSRange range;
            NSString *singleStr = [keyWords substringWithRange:NSMakeRange(j, 1)];
            while
                ((range = [self rangeOfString:singleStr options:0 range:searchRange]).location != NSNotFound) {
                    //改变多次搜索时searchRange的位置
                    searchRange = NSMakeRange(NSMaxRange(range), [self length] - NSMaxRange(range));
                    //设置富文本
                    [mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
                    
                }
        }
        
        return mutableAttributedStr;
        
        
        
    }
    

    swift 版本

    extension String{
        
        func keyWords_swift(_ keyWords: String, withKeyWordsColor color: UIColor = .red) -> NSMutableAttributedString? {
            
            let mutableAttributedStr = NSMutableAttributedString.init(string: self)
            if keyWords.isEmpty {
                return mutableAttributedStr
            }
            let text = NSString.init(string: self)
            var range = text.range(of: keyWords)
            
            var searchRange = NSMakeRange(0, self.count);
            
            while range.location != NSNotFound {
                mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
                //改变多次搜索时searchRange的位置
                searchRange = NSMakeRange(NSMaxRange(range), self.count - NSMaxRange(range));
                range = text.range(of: keyWords, options: [], range: searchRange)
            }
            
            return mutableAttributedStr
        }
        
        func keyWords_swiftNormal(_ keyWords: String, withKeyWordsColor color: UIColor = .red) -> NSMutableAttributedString? {
            
            let mutableAttributedStr = NSMutableAttributedString.init(string: self)
            if keyWords.isEmpty {
                return mutableAttributedStr
            }
            let text = NSString.init(string: self)
            let range = text.range(of: keyWords)
            if range.location != NSNotFound {
                 mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
            }
            
            return mutableAttributedStr
        }
    }
    
    func range(from nsRange: NSRange) -> Range<String.Index>? {
                guard
                    let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
                    let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
                    let from = String.Index(from16, within: self),
                    let to = String.Index(to16, within: self)
                    else { return nil }
                return from ..< to
            }
        func nsRange(from range: Range<String.Index>) -> NSRange {
                let from = range.lowerBound.samePosition(in: utf16)!
                let to = range.upperBound.samePosition(in: utf16)!
                return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
                               length: utf16.distance(from: from, to: to))
            }
    
    
    func getMoreKeyWordsAttributedString(members: (keywrod:String,color:UIColor)...) ->NSMutableAttributedString {
            let mutableAttributedStr = NSMutableAttributedString.init(string: self)
            if members.isEmpty {
                return mutableAttributedStr
            }
            for i in members {
                let keywrod = i.keywrod
                let color = i.color
                if keywrod.isEmpty {
                    continue
                }
                
                let text = NSString.init(string: self)
                var range = text.range(of: keywrod)
                var searchRange = NSMakeRange(0, self.count);
                while range.location != NSNotFound {
                    mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
                    //改变多次搜索时searchRange的位置
                    searchRange = NSMakeRange(NSMaxRange(range), self.count - NSMaxRange(range));
                    range = text.range(of: keywrod, options: [], range: searchRange)
                }
                
            }
            
            return mutableAttributedStr
        }
    
    

    剩下看demo 吧

    相关文章

      网友评论

          本文标题:iOS 富文本 遍历字符串包含的关键词并修改颜色

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