UI Programming

NSAttributedString Unveiled

How to customize the text in your app.

Riccardo Cipolleschi
Published in
5 min readDec 2, 2020

--

The main way to show some text to the users is through UILabels. Labels let us customize their text with some attributes. Most of us always use the same attributes to customize them.

However, the NSAttributedString API’s offer so many customization possibilities that I took some time to explore them. In today’s article, I’d like to share them with everybody, showing how they behave so that, the next time, we could use the right attribute for the job.

The Base API

Starting from the basics, the first customization point is about changing fonts, font size, and colors. To do that, we need to define the strings to print and a couple of attributes.

The NSAttributedString API comes from Objective-C, a world where typing isn’t as strict as in Swift. However, Swift helps us a bit by adding some type safety. For example, the keys of the attributes dictionary must be of the NSAttributedString.Key type.

--

--

Riccardo Cipolleschi
The Startup

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.