Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67469

How to display HTML text(as string) on UILabel with hyperlinks?

$
0
0

I want to display a string(which is actually a small HTML code, with hyperlink) on UILabel. E.g. The string which I have to display is: "Click here to know more". So is there a way to display it on UILabel, and on clicking the hyperlink(Click here) it opens up the desired web page? Here's what I did:

@IBOutlet weak var testLabel: UILabel!

override func viewDidLoad() {
   super.viewDidLoad()
   let htmlData = NSString(string: "<a href=\"http://www.google.com\">Click here</a> to know more").data(using: String.Encoding.unicode.rawValue)
   let attributedString = try? NSAttributedString(data: htmlData!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
   testLabel.attributedText = attributedString
}

The label displayed the string just like I wanted, but on clicking the hyperlink, it didn't do what what desired, i.e. open a web page.


Viewing all articles
Browse latest Browse all 67469

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>