Parsing HTML into NSAttributedText - how to set font?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you convert HTML into NSAttributedString, the HTML importer often brings along its own font choices. That is why simply setting a font on the label afterward does not always produce the result you want. The reliable solution is to parse the HTML into an attributed string first, then walk the attributed ranges and replace the imported fonts with your preferred font family while preserving traits such as bold and italic.
Parse The HTML First
Start by converting HTML data into an attributed string.
This gives you a mutable attributed string containing whatever fonts the HTML parser inferred.
Why Setting label.font Is Not Enough
If a UILabel or UITextView receives an attributed string, the font attributes inside that attributed string usually take priority over the plain font property of the view.
So this often disappoints people:
The imported HTML fonts still win because the attributed text already contains font attributes.
Replace Imported Fonts While Preserving Traits
The practical fix is to enumerate .font attributes and swap each one for a version of your target font family with matching symbolic traits.
Usage:
Now the output uses your preferred family and size while still respecting bold and italic where possible.
A Complete Helper
This is a good reusable utility for labels and text views.
CSS In The HTML Is Another Option
You can also inject a CSS style block into the HTML itself so the importer starts closer to your desired font.
This can help, but it is not always enough on its own, especially when imported HTML includes nested tags with their own styling. Post-processing the attributed string is usually more reliable.
Preserve More Than Just Font Family
If your design requires custom line spacing, color, or paragraph style, handle those attributes separately. Font replacement solves the typography family problem, but it does not automatically normalize every other imported HTML attribute.
That is another reason a dedicated post-processing helper is useful.
Common Pitfalls
- Setting
label.fontand expecting it to override fonts already embedded in the attributed string. - Replacing fonts without preserving bold and italic traits.
- Assuming the HTML importer will use your app's preferred font automatically.
- Injecting CSS only and skipping attributed-string cleanup when the imported result is inconsistent.
- Forgetting that
NSAttributedTextin the question is reallyNSAttributedStringin the Apple APIs.
Summary
- Parse HTML into an attributed string first, then adjust the font attributes inside it.
- View-level
fontsettings usually do not override fonts already stored in attributed text. - Enumerate
.fontattributes and replace them with your preferred font family. - Preserve symbolic traits so bold and italic styling still works.
- CSS can help, but post-processing the attributed string is usually the most reliable fix.

