Display html text in uitextview
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UITextView does not render HTML directly, but UIKit can turn HTML into an attributed string with reasonable fidelity. That makes it useful for showing formatted article bodies, email previews, or legal copy without embedding a full browser.
Convert HTML Into NSAttributedString
The core approach is to take an HTML string, encode it as Data, and initialize NSAttributedString with the .html document type. The result can be assigned to textView.attributedText.
That conversion is the important step. UITextView itself is only the display surface.
A Practical UIViewController Example
A small controller shows the usual setup. The example enables tapping links, applies a fallback font, and handles malformed HTML without crashing the screen.
This is enough for most app screens where the HTML is trusted and fairly simple.
Control Styling With Embedded CSS
HTML rendered through NSAttributedString supports a subset of CSS. Basic font, color, margin, emphasis, lists, and links usually work. Advanced layout, scripting, and many web features do not.
A useful pattern is to wrap incoming content in your own HTML template so the app controls typography consistently. That avoids depending on whatever inline styling a backend happens to send.
If the incoming content is only a fragment, such as a single paragraph, you can still wrap it in a small html and body document before parsing. This makes font handling more predictable.
Know When To Use WKWebView Instead
UITextView is good for rich text, not for full web content. If you need JavaScript, complex CSS layout, embedded media, or exact browser rendering, use WKWebView instead.
A good rule is simple:
- use
UITextViewfor formatted text with links - use
WKWebViewfor document-like or browser-like experiences
That distinction matters for performance and for maintenance. A text view is lighter and easier to integrate with the rest of a native layout.
Handling Dynamic Content Safely
If the HTML comes from an API or user input, sanitize it before rendering. Even though NSAttributedString is not a browser, malformed or unexpectedly large HTML can still cause layout issues or poor performance.
It is also worth doing the conversion off the main thread when the content is large. Create the attributed string in a background task, then assign it back on the main queue.
Common Pitfalls
The most common problem is assuming every HTML feature will render. UITextView is not a browser, so layout differences are normal.
Another frequent issue is broken character encoding. If the server response is not UTF-8, text can display with replacement characters or fail to parse. Confirm the actual encoding before creating Data.
Developers also run into font inconsistency because the incoming HTML defines its own styles. Wrap the fragment in CSS you control instead of trying to patch the attributed string afterward.
Finally, avoid enabling editing unless the screen truly needs it. Editing can interfere with link behavior and change text container layout in ways that are surprising.
Summary
- Render HTML in
UITextViewby converting it toNSAttributedString. - Use embedded CSS to keep typography and link colors consistent.
- Prefer
UITextViewfor rich text andWKWebViewfor full browser behavior. - Sanitize or validate dynamic HTML before rendering it.
- Handle encoding and parsing errors explicitly so the UI fails gracefully.

