NSAttributedString vs NSMutableAttributedString

How ARE you going add formatting to that writing?

Strings can be tricky things. That is why the Swift overlords helped you out with formatting, using attributed Strings. Let’s get stuck in with this guide.

Image for post
Image for post

This post is about explaining what that is, and how you can use it to help you in your production Apps. That is — make your Strings look good!

Difficulty: Easy | Normal | Challenging

Prerequisites:

  • Know what a String is, and have some experience in using it in Swift

Terminology

Character: A character, usually associated with a letter of the alphabet.

NSAttributedString: is a piece of text that has attributes (like links or fonts) that apply to portions of text.

NSMutableAttributedString: is an NSAttributedString with additional methods for mutating the content.

String: A collection of Characters.

The video guide

You want a video? Fine. However, I’d suggest that you also read the text as the person who has narrated the video really hasn’t made themselves to clear.

String me along

Strings are fantastic for lots of things in Swift. Actually forget that. They are mostly useful for storing text.

What does that mean

One of the prerequisites for this post is knowing how to use Strings…seriously. Ok, anyway a string can be thought of as a series of characters in an array.

This nicely looks like this (if the text, “text” is stored):

Image for post
Image for post
The String “text” stored as text

Note the positions over the elements here, and that this particular representation is thought of as zero-indexed.

What does that have to do with formatting?

Pretty much nothing, actually. Which is where NSAttributedString comes in. They allow us to add formatting to a String (or subsection of a String) and UILabels and UITextViews actually accept them directly. Here is an example. No. Here:

myTextView.attributedText = myAttributedString

Which is not much good if you haven’t built an NSAttributedString and named it as myAttributedString.

Plus we will have to use some Apple Magical Technology™ to easily see the results of our work (although, yes, of course you can set up your own new project to do that).

Play to the ground

Pretty much everyone has set up a new playground before. But I bet you didn’t know that it would render text for you so you can see the results of creating an NSAttributedString (disclaimer: not a real bet).

Start a new Playground (Xcode> File New> Playground and call it NSAttributedStringExample). Then type the following to create both a String and an Attributed String:

let plainString = “Boring text”

let attributedString = NSAttributedString(string: plainString)

Now, if you’re anything like me your name is Steven. Well, hello Steven. Follow along with these instructions.

We have so far made the following String (as an abstract representation):

Image for post
Image for post

Now on your playground you can now press the little icon alongside your “boring text” — the one that looks like an eye

Image for post
Image for post

Click on this an you get presented with a rendering of the text

Image for post
Image for post
I know this promised to be boring, but really…come on.

Let us now get some style into these Strings

A larger String

Pretty much everyone at some point will want to change the size of the font. It’s pretty simple to type into Playground and once again we can press on the eye for the rendering of the text.

Image for post
Image for post

so what is happening here, is we are giving the text an attribute.

Image for post
Image for post

Note how the font size applies to the whole String. You might be aware that we could do with with a String in any case.

Let’s go further, and so things that we would not be able to do with a simple String.

Mixing Strings

We can mix-me-do for the NSAttributedStrings, which allows us to mix the attributes.

Want to mix colors and font sizes — with NSAttributedStrings this is no real problem.

Set up a String with a color. Red should be good.

Image for post
Image for post

We can then append this to another String. But in order to do so that NSAttributedString needs to be mutable. That is, an NSMutableAttributedString. As before we can make a String with a different font size:

Image for post
Image for post

And the code is repeated here to make it a little easier to read:

Giving us the following look for what is actually going on

Image for post
Image for post

Now a couple of things about joining the Strings. We need an NSMutableAttributedString, it is inefficient and we haven’t got a nice way to mix the attributes (I want the writing BIG AND RED).

Mixing attributes

To prove that I’m mixing the attributes, the B can be black. The ranges of the attributes will cross each other, or something.

Image for post
Image for post
Like this

The following code gives us that, really quite spectacular result :

Can’t we have a list of attributes to use ourselves?

I really wish Apple provided such a list. Like this one: https://developer.apple.com/documentation/foundation/nsattributedstring/key

In fact. Just use that one. It’ll do.

Conclusion

The difference between a NSAttributedString and an NSMutableAttributedString is…

That one is mutable. I bet you never saw that coming.

But the use of attributes for a String is really useful. It can make your App look half-decent.

Surely you want that? At least you should do…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store