Creating attributed text from an HTML string

Paolo Montalto
1 min readApr 16, 2021

Every now and then you might have found yourself in the need to showing attributed text in your iOS app (e.g. in a UILabel) and considered how easy it is in HTML compared to an NSAttributedString.

Suppose you wanted to have a label with some underlined text, in HTML you would simply do the following:

Next word is <u>underlined</u>.

To achieve the same within a UILabel you should do something like this:

Wouldn’t it be great if you could just do the following?

Less verbose and more effective, don’t you agree?

It is not hard to achieve, you just need to create an extension to the NSAttributedString class, as follows:

--

--

Paolo Montalto

Android Engineer, freelance, mobile developer, software craftsman, guitar strummer, husband, father, humble.