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
- Know what a String is, and have some experience in using it in Swift
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):
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):
Now on your playground you can now press the little icon alongside your “boring text” — the one that looks like an eye
Click on this an you get presented with a rendering of the text
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.
so what is happening here, is we are giving the text an attribute.
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.
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.
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:
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
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).
To prove that I’m mixing the attributes, the B can be black. The ranges of the attributes will cross each other, or something.
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.
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…
The Repo link
Nobody wants to type. Unless they have to. And you don’t. Here is the completed Playground:
Wanna Follow? I sometime write on Twitter:
The latest Tweets from Steven Curtis (@stevenpcurtis). Studying for a Masters in Computing while developing iOS Apps…
Thanks for making it to the end, if indeed, you did.