How to display HTML in UITextView/UILabel with custom color, font, etc. in iOS using Swift

How to display HTML in UITextView/UILabel with custom color, font, etc. in iOS using Swift

If you want to show a list that contains items in HTML in a UITableView or UICollectionView, it’s preferable to use the 3rd party library DTCoreText. It’s a lot of faster that the native way. I have made a tutorial about it, here.

When you want to show a text from HTML content from a website to your app, you have to convert the text to an NSAttributedString format to show it correctly on a UITextView/UILabel.

In this tutorial, I’m gonna saw you how to do this very easily by using an extension that uses custom text color, font and more with the help of CSS.

We’re not going to cover how to add a custom font to your iOS project in this tutorial, but we will focus only on the NSAttributedString implementation.

Just paste the following extension to your Extensions.swift file: 

extension String {
private var convertHtmlToNSAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else {
return nil
}
do {
return try NSAttributedString(data: data,options: [.documentType: NSAttributedString.DocumentType.html,.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
}
catch {
print(error.localizedDescription)
return nil
}
}
public func convertHtmlToAttributedStringWithCSS(font: UIFont? , csscolor: String , lineheight: Int, csstextalign: String) -> NSAttributedString? {
guard let font = font else {
return convertHtmlToNSAttributedString
}
let modifiedString = "<style>body{font-family: '\(font.fontName)'; font-size:\(font.pointSize)px; color: \(csscolor); line-height: \(lineheight)px; text-align: \(csstextalign); }</style>\(self)";
guard let data = modifiedString.data(using: .utf8) else {
return nil
}
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
}
catch {
print(error)
return nil
}
}
}
view raw Extensions.swift hosted with ❤ by GitHub

After that, go to your string you want to convert to NSAttributedString and place it like the example below:

myUILabel.attributedText = "Swift is awesome&#33;&#33;&#33;".convertHtmlToAttributedStringWithCSS(font: UIFont(name: "Arial", size: 16), csscolor: "black", lineheight: 5, csstextalign: "center")
view raw example.swift hosted with ❤ by GitHub

Here’s what every parameter takes:

  • font: Add your font as usually do in a UILabel/UITextView, using UIFont with the name of your custom font and the size.
  • csscolor: Either add color in HEX format, like "#000000" or use the name of the color, like "black".
  • lineheight: It’s the space between the lines when you have multiple lines in a UILabel/UITextView.
  • csstextalign: It’s the alignment of the text, the value that you need to add is "left" or "right" or "center" or "justify"

and Voila! 🎉🎉🎉🎉

If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!

Leave a Reply

avatar
  Subscribe  
Notify of

Stay Connected

Newsletter

Subcribe for weekly emails! Get my posts of the week right in your inbox!