Search code examples
iosswiftnsattributedstring

How to detect anchor tag link click in NSAttributedString created from html string


I am creating NSAttributedString by using html:

let htmlString = "<body style='padding-left:50px'><h1>Hello World</h1><div><a href=https://apple.com/offer/samsung-faq/>Click Here</a></div><p>This is a sample text</p><pre>This is also sample pre text</pre></body>"

And here i am setting it to UILabel by using extension method

someLabel.attributedText = htmlString.htmlToAttributedString

NSAttributedString Extension:

extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType:  NSAttributedString.DocumentType.html], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
}

Here i want an callback method to detect link which is there in html string as anchor tag. How will i get an event on click and how can i get the url in that event callback ?

Please help...


Solution

  • Use UITextView instead of UILabel and it has a property to convert your text to hyperlink.

    You will have to make your UIViewController confirm to UITextViewDelegate protocol and implement textView(_:shouldInteractWith:in:interaction:. your standard UITextView setup should look something like this, don't forget the delegate and dataDetectorTypes.

    @IBOutlet weak var txtView: UITextView!
    // make IBOutlet of UITextView
    
    txtTest.delegate = self
    txtTest.isUserInteractionEnabled = true // default: true
    txtTest.isEditable = false // default: true
    txtTest.isSelectable = true // default: true
    txtTest.dataDetectorTypes = [.link]
    

    UITextViewDelegate method shouldInteractWithURL:

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        print("Link Selected!")
        return true
    }
    

    HTML to NSAttributedString Extension:

    extension String{
        func convertHtml() -> NSAttributedString{
            guard let data = data(using: .utf8) else { return NSAttributedString() }
            do{
                return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
            }catch{
                return NSAttributedString()
            }
        }
    }
    

    then you can use it like so.

    let htmlString = "<body style='padding-left:50px'><h1>Hello World</h1><div><a href=https://apple.com/offer/samsung-faq/>Click Here</a></div><p>This is a sample text</p><pre>This is also sample pre text</pre></body>"
    
    txtTest.attributedText = htmlString.convertHtml()