How to detect a link in UILabel in swift 4?

I used ActiveLabel as a third-party library to make a link in the label for certain words. The code is great for Swift 3 and 3.2. but not working for quick 4.

Below is the code I used

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 } 

Can someone give me a solution using native code instead of using a third-party library.

+7
ios uilabel swift
source share
1 answer

I also managed to find a solution for 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 } 
+2
source share

All Articles