Attributed Strings in Swift

Payal Kandlur
2 min readOct 29, 2021

Swift Strings are plain text values that you want to store or display. Styling them is possible, of course, the Storyboard has a few options which let you select a few fonts and their sizes.

But what if you need to style them in a way apart from what's just mentioned in them? That's when Attributed strings come into the picture!

NSAttributedString is the foundation’s all-in-one string handling class. These are most likely used with UILabel and UITextView, both of which accept attributed strings directly.

Let's begin with a simple attributed string, just regular text no stylings!

let str = "String one"
let attributedQuote = NSAttributedString(string: str)

Now when you see the preview for “attributedQuote” all you see is a regular strig exactly the same as “str”, because we haven't added any stylings.

Now let's try adding some size and font colour to our string.

let str = "String one"
let font = UIFont.systemFont(ofSize: 72)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: UIColor.red,
]
let attributedQuote = NSAttributedString(string: str, attributes: attributes)

What do you see now?

The font size has changed and the colour is red! Well, this could be done in the playground too.

In a similar way, you can add shadow, text alignment, indents, line spacing, font family etc.

Adding Attributes to an existing text

So far you have seen how you can add an attribute to a text from scratch, but what if you want to add an attribute to a text that has already been used elsewhere.

All you need to do is use addAttribute.

let str = "String one"let font = UIFont.systemFont(ofSize: 72)let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: UIColor.red,
]
let attributedQuote = NSMutableAttributedString(string: str, attributes: attributes)attributedQuote.addAttribute(.foregroundColor, value: UIColor.blue, range: NSRange(location: 2, length: 5))

Do not forget to change it to NSMutableAttributedString. The NSRange adds the range in the text from where you want the added attribute to be applied.

Placing an Image

You can also place an image inside an attributed string.

let imageString = NSMutableAttributedString(string: "Your image: ")let imageAttachment = NSTextAttachment()imageAttachment.image = UIImage(named: "sampleImage.jpg")let imageString = NSAttributedString(attachment: imageAttachment)imageString.append(imageAttachment)

NSTextAttachment is a class specifically designed to let us attach images to attributed strings.

Placing a Hyperlink

Weblinks are just like any other attributed string attribute, meaning that you can add NSAttributedString.Key.link to any range of your string, along with a URL that should be shown when the link is clicked.

let attributedString = NSMutableAttributedString(string: "Subscribe to get my articles in your inbox!")attributedString.addAttribute(.link, value: "https://payalkandlur.medium.com/", range: NSRange(location: 19, length: 9))

That’s all! Comment below for suggestions, queries or corrections! Do subscribe and share a word!😊

--

--