This is my code that show the Links in a UILabel
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textFiled: UITextField!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let attrStr = try! NSAttributedString(
data: "This is a link <a href='http://www.google.com'>google</a>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
label.attributedText = attrStr
}
}
As you can see from the picture, it shows the link correctly that I just want it, but the link can't click to jump to a web view or others. Actually, I need to custom the link click event, and I really have no idea:
- How to get the URL of the link.
- If there are more than one links, what I need to do.
Thanks for checking this question firstly.