如何在swift 4中检测UILabel中的链接?

Tec*_*ain 6 uilabel ios swift

我使用ActiveLabel作为第三方库,在特定单词的标签中建立链接.代码适用于Swift 3和3.2.但不适用于swift 4.

我用的代码如下

let customType1 = ActiveType.custom(pattern: "\\sTerms & Conditions\\b") //Looks for "are"
            labelTc.enabledTypes.append(customType1)
            labelTc.customize { (label) in
                labelTc.text = "UserAgreement".localized
                label.numberOfLines = 0
                label.lineSpacing = 4
                label.textColor = UIColor(red: 131 / 255, green: 147 / 255, blue: 168 / 255, alpha: 1)
                //Custom types
                label.customColor[customType1] = Constant.AppColor.greenMeadow
                label.customSelectedColor[customType1] = Constant.AppColor.greenMeadow
                label.configureLinkAttribute = { (type, attributes, isSelected) in
                    var atts = attributes
                    switch type {
                    case customType1:
                        atts[NSAttributedStringKey.font._rawValue as String] = UIFont(name: self.labelTc.font.fontName, size: 15.0)
                        atts[NSAttributedStringKey.underlineStyle.rawValue] = NSUnderlineStyle.styleSingle
                        break


                    case .mention:
                        break
                    case .hashtag:
                        break
                    case .url:
                        break
                    case .custom(let pattern):
                        break

                    default :
                        break
                    }

                    return atts
                }
Run Code Online (Sandbox Code Playgroud)

任何人都可以使用本机代码而不是第三方库给我解决方案.

Tec*_*ain 3

我也找到了 swift 4 的解决方案。

label.configureLinkAttribute = { (type, attributes, isSelected) in
                var atts = attributes
                switch type {
                case customType1:
                    atts[NSAttributedStringKey.font.rawValue] = UIFont(name: self.labelTc.font.fontName, size: 15.0)
                    atts[NSAttributedStringKey.underlineStyle.rawValue] = NSUnderlineStyle.styleSingle.rawValue
                    break

              default: ()
                }
                return atts
            }
Run Code Online (Sandbox Code Playgroud)