Swift
HTML
UILabel
UITextView
iOS Development

Swift Display HTML data in a label or textView

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UIKit does not render raw HTML directly inside a UILabel or UITextView. The usual approach is to convert the HTML string into an NSAttributedString and assign that attributed text to the view.

Convert HTML Into NSAttributedString

The core step is turning the HTML string into Data and asking NSAttributedString to parse it as HTML.

swift
1import UIKit
2
3func htmlAttributedString(_ html: String) -> NSAttributedString? {
4    guard let data = html.data(using: .utf8) else { return nil }
5
6    return try? NSAttributedString(
7        data: data,
8        options: [
9            .documentType: NSAttributedString.DocumentType.html,
10            .characterEncoding: String.Encoding.utf8.rawValue
11        ],
12        documentAttributes: nil
13    )
14}

Once you have the attributed string, both UILabel and UITextView can display it.

Display HTML in a UILabel

A UILabel can show styled text through its attributedText property.

swift
let html = "<b>Hello</b> <i>world</i><br><font color='#cc0000'>Important</font>"
label.numberOfLines = 0
label.attributedText = htmlAttributedString(html)

This works well for short, mostly static rich text where editing and link interaction are not required.

Display HTML in a UITextView

If the content is longer or includes links, UITextView is often a better fit.

swift
1let html = "Visit <a href='https://example.com'>Example</a>"
2textView.isEditable = false
3textView.isScrollEnabled = false
4textView.dataDetectorTypes = [.link]
5textView.attributedText = htmlAttributedString(html)

UITextView gives you richer interaction behavior than UILabel, especially when the content resembles article or message text.

Know the Limits of HTML Rendering

This parser supports a useful subset of HTML, but it is not a full browser engine. Complex CSS, scripts, advanced layout, and web-like rendering rules are not the right expectation.

If you need real webpage rendering, use WKWebView instead of forcing heavy HTML into text views.

Normalize the Styling After Parsing

HTML-derived attributed strings may not automatically match the fonts or colors used by the rest of your app. It is often worth applying a consistent font afterward.

swift
1if let attributed = htmlAttributedString("<b>Hello</b> world")?.mutableCopy() as? NSMutableAttributedString {
2    attributed.addAttribute(.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: attributed.length))
3    label.attributedText = attributed
4}

That keeps the rendering integrated with the app’s visual language rather than whatever the HTML happened to imply.

Parse Off the Main Thread if the Content Is Large

For very large HTML snippets, conversion into an attributed string can be noticeable if you do it repeatedly on the main thread. In that case, build the attributed string in background work and assign it to the UI on the main queue.

That keeps rendering smooth while still using the same NSAttributedString HTML conversion approach.

Pick the Control Based on Interaction Needs

If the rendered content includes tappable links, long paragraphs, or selectable text, UITextView is usually the safer choice even if the visual result looks similar to a label. UILabel is best when the HTML is short and presentation-only.

Common Pitfalls

The biggest mistake is expecting raw HTML strings to display correctly without converting them to an attributed string first.

Another issue is using UILabel for long, interactive, or link-heavy content where UITextView is the better tool.

A third problem is treating NSAttributedString HTML parsing like a browser. It is a text-rendering feature, not full web rendering.

Summary

  • Convert HTML to NSAttributedString before assigning it to UIKit text views.
  • Use UILabel for short rich text and UITextView for more interactive or longer content.
  • Expect only limited HTML and styling support.
  • Use WKWebView if you truly need webpage-style rendering.
  • Normalize fonts and colors after parsing so the result matches the rest of the app.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.