NEW: My free 100 Days of SwiftUI challenge starts tomorrow! >>

Formatting strings with NSAttributedString

Swift’s strings are plain text, which works fine in the vast majority of cases we work with text. But sometimes we want more – we want to be able to add formatting like bold or italics, select from different fonts, or add some color, and for those jobs we have a new class called NSAttributedString.

Attributed strings are made up of two parts: a plain Swift string, plus a dictionary containing a series of attributes that describe how various segments of the string are formatted. In its most basic form you might want to create one set of attributes that affect the whole string, like this:

let string = "This is a test string"
let attributes: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.white,
    .font: UIFont.boldSystemFont(ofSize: 36)

let attributedString = NSAttributedString(string: string, attributes: attributes)

It’s common to use an explicit type annotation when making attributed strings, because inside the dictionary we can just write things like .foregroundColor for the key rather than NSAttributedString.Key.foregroundColor.

The values of the attributes dictionary are of type Any, because NSAttributedString attributes can be all sorts of things: numbers, colors, fonts, paragraph styles, and more.

If you look in the output pane of your playground, you should be able to click on the box next to where it says “This is a test string” to get a live preview of how our string looks – you should see large, white text with a red background.

Of course, we could get the same effect with a regular string placed inside a UILabel: change the font and colors, and it would look the same. But what labels can’t do is add formatting to different parts of the string.

To demonstrate this we’re going to use NSMutableAttributedString, which is an attributed string that you can modify:

let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 8), range: NSRange(location: 0, length: 4))
attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 5, length: 2))
attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 24), range: NSRange(location: 8, length: 1))
attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 32), range: NSRange(location: 10, length: 4))
attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 40), range: NSRange(location: 15, length: 6))

When you preview that you’ll see the font size get larger with each word – something a regular Swift string certainly can’t do even with help from UILabel.

There are lots of formatting options for attributed strings, including:

  • Set .underlineStyle to a value from NSUnderlineStyle to strike out characters.
  • Set .strikethroughStyle to a value from NSUnderlineStyle (no, that’s not a typo) to strike out characters.
  • Set .paragraphStyle to an instance of NSMutableParagraphStyle to control text alignment and spacing.
  • Set .link to be a URL to make clickable links in your strings.

And that’s just a subset of what you can do.

You might be wondering how useful all this knowledge is, but here’s the important part: UILabel, UITextField, UITextView, UIButton, UINavigationBar, and more all support attributed strings just as well as regular strings. So, for a label you would just use attributedText rather than text, and UIKit takes care of the rest.

LEARN SWIFTUI FOR FREE I wrote a massive, free SwiftUI tutorial collection, and also have a growing list of free SwiftUI tutorials on YouTube – get started today!

Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5