swift 可点击的富文本label

swift 轻量级富文本label ,带点击事件;支持snp layout布局。
ActiveLabel

使用方法

        let label = ActiveLabel()
        label.isShowUnderLine = true
        label.font = UIFont.systemFont(ofSize: 14)
        self.view.addSubview(label)
        label.snp.makeConstraints {
            $0.top.equalTo(40)
            $0.left.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
            $0.height.equalTo(300)
        }
        //  pattern筛选条件,"\\s筛选条件\\b" :筛选过滤按照空格文本
        //   "筛选条件" : :筛选文本
        let customType = ActiveType.custom(pattern: "") //Looks for ""
         /**
         enabledTypes.append 方式会默认筛选 @ # url
         label.enabledTypes = [] 只会显示 []中的
         */
//        label.enabledTypes.append(customType)
        label.enabledTypes = [customType]
        label.urlMaximumLength = 31
        label.customize { (label) in
            label.text =  "Thisis a post with #multiple #hashtags and a @userhandle. Links are also supported like" +
                       " this one: http://optonaut.co. Now it also supports custom patterns -> are\n\n" +
                           "Let's trim a long link: \nhttps://twitter.com/twicket_app/status/649678392372121601"
            label.numberOfLines = 0
            label.lineSpacing = 4
            label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
            label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
            label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
            label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
            label.URLSelectedColor = UIColor(red: 82.0/255, green: 190.0/255, blue: 41.0/255, alpha: 1)
            /// @
            label.handleMentionTap {
                self.alert("Mention", message: $0)
                
            }
            /// ##
            label.handleHashtagTap {
                self.alert("Hashtag", message: $0)
                
            }
            /// url
            label.handleURLTap {
                self.alert("URL", message: $0.absoluteString)
            }
            
            //Custom types
            label.customColor[customType] = UIColor.purple
            label.customSelectedColor[customType] = UIColor.green

            /// 修改选中富文本
            label.configureLinkAttribute = { (type, attributes, isSelected) in
                var atts = attributes
                switch type {
                case customType:
                    atts[NSAttributedString.Key.font] = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.boldSystemFont(ofSize: 14)
                default: ()
                }

                return atts
            }

            label.handleCustomTap(for: customType) {
                self.alert("Custom type", message: $0)
                
            }
        }

有的时候可能需要下滑线,我增加了一个属性
label.isShowUnderLine = true

    /// 修改了 、增加了是否显示下划线
    open var isShowUnderLine: Bool = false {
        didSet { updateTextStorage(parseText: false) }
    }

    /// add link attribute
    fileprivate func addLinkAttribute(_ mutAttrString: NSMutableAttributedString) {
        var range = NSRange(location: 0, length: 0)
        var attributes = mutAttrString.attributes(at: 0, effectiveRange: &range)
        
        attributes[NSAttributedString.Key.font] = font!
        attributes[NSAttributedString.Key.foregroundColor] = textColor
        mutAttrString.addAttributes(attributes, range: range)
        
        attributes[NSAttributedString.Key.foregroundColor] = mentionColor
        
        for (type, elements) in activeElements {
            
            switch type {
            case .mention: attributes[NSAttributedString.Key.foregroundColor] = mentionColor
            case .hashtag: attributes[NSAttributedString.Key.foregroundColor] = hashtagColor
            case .url: attributes[NSAttributedString.Key.foregroundColor] = URLColor
            case .custom: attributes[NSAttributedString.Key.foregroundColor] = customColor[type] ?? defaultCustomColor
            }
            
            if let highlightFont = hightlightFont {
                attributes[NSAttributedString.Key.font] = highlightFont
            }
            
            if let configureLinkAttribute = configureLinkAttribute {
                attributes = configureLinkAttribute(type, attributes, false)
            }
            
            for element in elements {
                mutAttrString.setAttributes(attributes, range: element.range)
                /// 修改了
                if isShowUnderLine == true {
                    mutAttrString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: element.range.location, length:  element.range.length))
                }

            }
        }
    }

github: https://github.com/optonaut/ActiveLabel.swift

你可能感兴趣的:(swift 可点击的富文本label)