美文网首页
iOS提取文本中链接添加高亮点击,String NSString

iOS提取文本中链接添加高亮点击,String NSString

作者: 再好一点点 | 来源:发表于2021-08-17 09:41 被阅读0次

    直接上代码,通过以下计算就可以完成链接的识别以及替换,然后添加高亮以及对高亮文字添加点击事件

    let label = YYLabel()
            label.backgroundColor = .red
            label.numberOfLines = 0
            label.preferredMaxLayoutWidth = 200
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalTo(50)
                make.width.equalTo(200)
                make.top.equalTo(300)
            }
            
            var urls: [String] = []
            
            var string = "test\\n\ntest/\\n\n😂\nhttps://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9827769369551786176%22%7D&n_type=0&p_from=1 第二个 https://weibo.com/mygroups?gid=201209041125312675&wvr=6&leftnav=1&isspecialgroup=1 今天又是好天气!今天又是好天气!今天又是好天气!今天又是好天气!今天又是好天气!";
            
    //        var string = "est\\n\ntest/\\n\n😂\nhttps://www.weibo.com/beat/beatslist/beat?keyword=测试 https://weibo.com/mygroups?gid=201209041125312675&wvr=6&leftnav=1&isspecialgroup=1 每天都很美好";
    
            let detector = try? NSDataDetector.init(types: NSTextCheckingResult.CheckingType.link.rawValue)
            let matches = detector?.matches(in: string, options: [.reportProgress], range: NSRange(location: 0, length: string.count))
            for match in matches! {
                if match.resultType == .link {
                    if let url = match.url {
                        //这里获取到的url是经过encode处理的
                        urls.append(url.absoluteString)
                    }
                }
            }
            
            print("encode urls \(urls)")
            
            let font = UIFont.systemFont(ofSize: 15)
            let attribbutes = NSMutableAttributedString(string: "")
            for str in urls {
               
                var strArray = string.components(separatedBy: str)
                if strArray.count < 2 {
                    //如果没有匹配到字符说明string没有经过encode,但是str为encode以后的字符,所以需要decode str才能识别
                    if let decode = str.removingPercentEncoding {
                        strArray = string.components(separatedBy: decode)
                    }
                }
                if let firstStr = strArray.first {
                    let subAttribute = NSMutableAttributedString(string: firstStr)
                    subAttribute.setColor(.cyan, range: NSRange(location: 0, length: firstStr.count))
                    attribbutes.append(subAttribute)
                    
                    let hightText = YYTextHighlight()
                    hightText.tapAction = { view, text, range, rect in
                        guard let url = URL(string: str) else {
                            print("error url \(str)")
                            return
                        }
                        print("open url \(url)")
                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    }
                    
                    let imageAttribute = NSMutableAttributedString.attachmentString(withContent: UIImage(named: "commend_kink"), contentMode: .scaleAspectFit, attachmentSize: .init(width: 18, height: 18), alignTo: font, alignment: .center)
                    let replaceStr = NSMutableAttributedString(string: "查看链接")
                    replaceStr.setColor(.yellow, range: NSRange(location: 0, length: 4))
                    imageAttribute.append(replaceStr)
                    imageAttribute.setTextHighlight(hightText, range: NSRange(location: 0, length: 5))
                    attribbutes.append(imageAttribute)
                }
                
                if let lastStr = strArray.last {
                    string = lastStr
                }
                
            }
            let lastAttribute = NSMutableAttributedString(string: string)
            lastAttribute.setColor(.cyan, range: NSRange(location: 0, length: string.count))
            attribbutes.append(lastAttribute)
            
            label.attributedText = attribbutes
    
    注意点YYLabel UILabel用法差异

    使用YYLabel和使用UILabel还是有一些区别的。如果使用UILabel设置了lable的font、textColor,在给label的text赋值或者attributedText赋值时,上述设置都会生效。但是YYLabel设置font、textColor只会对label的text生效,对于attributedText属性必须对NSAttributedString、NSMutableAttributedString进行设置。

    YYLabel高度自适应 preferredMaxLayoutWidth

    对于UIlabel仅设置label.numberOfLines = 0,并且不设置高度lable就会随着显示内容自动增长,但是对于YYLable除了设置label.numberOfLines = 0,还要设置label.preferredMaxLayoutWidth,只有这样YYLabel才会自增长。

    String NSString 对于表情NSRange计算长度不同

    由下图可以看出对于纯文字不管是String类型NSString计算结果都一样。但是对于含有表情的文本计算,两者计算结果不一样。对于这种情况可以使用NSString计算也可以使用String调用.utf16.coun计算。NSString默认使用utf16计算,而表情大部分也需要使用utf16计算,所以直接使用NSString就可以得到正确的结果。

     let range = NSMakeRange(0, string.count)
     let nsStr = string as NSString
     let conTentRange = NSMakeRange(0, nsStr.length)
     //let conTentRange = NSMakeRange(0, string.utf16.count)
     printf("range \(range) conTentRange \(conTentRange)  text \(text)")
    
    image
    YYTextHighlight和父视图手势会冲突问题
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
            //YYTextHighlight和父视图手势会冲突,所以需要识别点击位置是否包含YYTextHighlight
            if touch.view is YYLabel {
                let label = touch.view as? YYLabel
                if let label = label {
                    var range = NSMakeRange(0, 1);
                    let highlight = label._getHighlight(at: touch.location(in: label), range: &range)
                    if highlight != nil {
                        return false
                    }
                }
            }
            return true
        }
    

    上边代码中-(YYTextHighlight *)_getHighlightAtPoint:(CGPoint)point range:(NSRangePointer)range;为私有方法,外部不可调用,又考虑到引用的是别人的第三方库,所以需要创建一个YYLabel的分来,然后将此方法在YYLabel分类的.h中声明。如下:

    #import "YYLabel.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YYLabel (YHYYLabel)
    
    - (YYTextHighlight *)_getHighlightAtPoint:(CGPoint)point range:(NSRangePointer)range;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    本人原文博客地址

    相关文章

      网友评论

          本文标题:iOS提取文本中链接添加高亮点击,String NSString

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