Attributed String in Swift: Complete Guide with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Attributed String in Swift: When Plain Text Isn't Enough



Have you ever wondered why in some apps the text looks like a gray boring sheet, while in others it looks like a work of art? The secret is simple: attributed string. This is a powerful iOS developer tool that allows you to control every pixel of the text: color, font, spacing, shadows, and even interactive links.



In this article, we will break down how to create NSAttributedString in Swift, how to configure it, and where to apply it. You will learn not just dry facts, but real techniques that will make your app more beautiful and user-friendly. Let's go!



What is an Attributed String? The Basics You Need to Know



In a classic String, all characters are the same — one font, one color, one size. NSAttributedString (and its mutable version NSMutableAttributedString) is a string where each character or range of characters can have its own set of attributes. Imagine you are writing the word "Hello": the letter "H" is red and bold, "e" is blue and italic, and so on. Sounds like magic? No, it's simply Swift attributed string.



Where is this used in real applications?

  • Text editors (bold, italic highlighting)
  • Social networks (@username mentions are clickable and highlighted in color)
  • Chat applications (different colors for different users)
  • E-books (different fonts for headings and quotes)
  • Any interface where text is not just text, but a design element


How to Create NSAttributedString in Swift: From Simple to Complex



Let's start with a basic example. In Swift, creating an attributed string is a process where you define a dictionary of attributes and apply it to the text.



Example 1: Simplest attributed string

import UIKit

let plainText = "Hello, Swift!"let attributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor.red, .font: UIFont.boldSystemFont(ofSize: 18)]let attributedString = NSAttributedString(string: plainText, attributes: attributes)

// Use in UILabellet label = UILabel()label.attributedText = attributedString


In this example, we created a string where all the text is red, bold font 18pt. Note NSAttributedString.Key — this is an enumeration of all possible attributes. There are dozens of them, and we will look at the most useful ones.



Example 2: Different attributes for different parts of the text

Now here's where the real fun begins. Let's use NSMutableAttributedString:

let mutableString = NSMutableAttributedString(string: "This is bold, and this is red text")

// Apply bold font to the first 10 charactersmutableString.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 16), range: NSRange(location: 0, length: 10))

// Apply red color to the last 15 charactersmutableString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 14, length: 15))

// Use in UITextViewlet textView = UITextView()textView.attributedText = mutableString


Important: NSRange works with character positions, not Swift indices. Be careful, especially with emojis and multi-byte characters.



Top 10 Attributes You Will Use Every Day



Here is a list of keys you will use most often when working with NSAttributedString Swift:



  • .font — font (UIFont)
  • .foregroundColor — text color
  • .backgroundColor — text background
  • .underlineStyle — underline (NSUnderlineStyle)
  • .strikethroughStyle — strikethrough
  • .paragraphStyle — p

Blogs

Book Recommendations