1

I've succeeded to display HTML as attributed String in UILable.

The problem is when that HtmlString is containing href link nothing happens when you click on that href like the screenshot.

enter image description here

html string example

"The Privacy Policy can be found <a href=\"https://www.w3schools.com/python/python_for_loops.asp\">here</a> "

my code

let stringHTMl = "The Privacy Policy can be found <a href=\"https://www.w3schools.com/python/python_for_loops.asp\">here</a> "
cell.textUILable.attributedText = stringHTMl.convertHtml()

extension for converting stringToattributesHTMLString

func convertHtml() -> 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()
    }
}
Community
  • 1
  • 1
Mohamed Shaban
  • 2,263
  • 1
  • 15
  • 23

2 Answers2

2

You can use UITextView. Solution with UITextView here.

Also, You can add textView.isEditable = false to looks like UILabel.

Emre Ozdil
  • 401
  • 6
  • 12
0

Swift 4.0 or latest

Yo can achieve it like this:

let tapAction = UITapGestureRecognizer(target: self, action:@selector(actionTapped(_:)))
cell.textUILable.isUserInteractionEnabled = true
cell.textUILable.addGestureRecognizer(tapAction)

Action Tapped Defination is

func actionTapped(_ sender: UITapGestureRecognizer) {
    // on tap text Label code here what you want to do
}

Do what ever you want to do in actionTapped