Swift
attributed string
iOS development
Swift programming
string manipulation

How do I make an attributed string using Swift?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

An attributed string lets you apply formatting to selected ranges of text instead of styling the whole string the same way. In Swift, you will usually work with either NSAttributedString and NSMutableAttributedString for UIKit and AppKit interoperability, or the newer Swift-native AttributedString type for modern code.

The Classic UIKit Approach: NSAttributedString

The most familiar approach in iOS development is NSAttributedString:

swift
1import UIKit
2
3let text = "Hello, world!"
4let attributes: [NSAttributedString.Key: Any] = [
5    .font: UIFont.systemFont(ofSize: 20, weight: .medium),
6    .foregroundColor: UIColor.systemBlue
7]
8
9let attributed = NSAttributedString(string: text, attributes: attributes)

This creates a read-only attributed string where the entire text shares the same attributes.

Use NSMutableAttributedString for Mixed Styling

If different parts of the text need different styles, use NSMutableAttributedString:

swift
1import UIKit
2
3let text = "Hello, world!"
4let attributed = NSMutableAttributedString(string: text)
5
6let helloRange = (text as NSString).range(of: "Hello")
7attributed.addAttributes([
8    .font: UIFont.boldSystemFont(ofSize: 24),
9    .foregroundColor: UIColor.systemRed
10], range: helloRange)
11
12let worldRange = (text as NSString).range(of: "world")
13attributed.addAttributes([
14    .font: UIFont.italicSystemFont(ofSize: 20),
15    .foregroundColor: UIColor.systemGreen
16], range: worldRange)

This is the normal pattern when labels, text views, or buttons need rich formatting in UIKit.

Useful Attributes to Know

Common attributes include:

  • '.font'
  • '.foregroundColor'
  • '.backgroundColor'
  • '.underlineStyle'
  • '.strikethroughStyle'
  • '.paragraphStyle'

Example with multiple attributes:

swift
1import UIKit
2
3let text = "Swift Attributed String"
4let attributed = NSMutableAttributedString(string: text)
5
6attributed.addAttributes([
7    .foregroundColor: UIColor.systemPurple,
8    .backgroundColor: UIColor.systemYellow,
9    .underlineStyle: NSUnderlineStyle.single.rawValue,
10    .font: UIFont.systemFont(ofSize: 18, weight: .semibold)
11], range: NSRange(location: 0, length: attributed.length))

Paragraph Styling

Paragraph-level formatting uses NSMutableParagraphStyle:

swift
1import UIKit
2
3let style = NSMutableParagraphStyle()
4style.alignment = .center
5style.lineSpacing = 8
6
7let text = "First line\nSecond line"
8let attributed = NSAttributedString(string: text, attributes: [
9    .paragraphStyle: style,
10    .font: UIFont.systemFont(ofSize: 18)
11])

This is useful for multiline labels and text views.

The Swift-Native Option: AttributedString

Modern Swift also includes AttributedString, which feels more Swifty and integrates well with SwiftUI:

swift
1import SwiftUI
2
3var message = AttributedString("Hello, world!")
4if let helloRange = message.range(of: "Hello") {
5    message[helloRange].foregroundColor = .red
6    message[helloRange].font = .headline
7}

This is a good choice when:

  • you are working in SwiftUI
  • you want stronger Swift type safety
  • you do not need to stay entirely in Foundation-era attributed string APIs

Converting Between the Two Worlds

Sometimes you need to interoperate with older UIKit APIs that expect NSAttributedString. In those cases, the classic Foundation types are still important even if you prefer the newer Swift-native model in your own code.

That is why it is useful to understand both:

  • 'NSAttributedString for framework compatibility'
  • 'AttributedString for modern Swift ergonomics'

A Practical UIKit Example

Assign an attributed string to a UILabel:

swift
1import UIKit
2
3let label = UILabel()
4let text = "Tap here to continue"
5let attributed = NSMutableAttributedString(string: text)
6
7let linkRange = (text as NSString).range(of: "here")
8attributed.addAttributes([
9    .foregroundColor: UIColor.systemBlue,
10    .underlineStyle: NSUnderlineStyle.single.rawValue
11], range: linkRange)
12
13label.attributedText = attributed

This is one of the most common real-world uses of attributed text on iOS.

Common Pitfalls

  • Using NSAttributedString when you actually need mutation and should be using NSMutableAttributedString.
  • Calculating ranges incorrectly when working with Foundation string APIs.
  • Forgetting that UIKit and SwiftUI often prefer different attributed-string styles.
  • Applying paragraph attributes to only part of the text when the entire paragraph layout is supposed to match.
  • Treating attributed strings as plain strings and expecting formatting to survive normal string operations automatically.

Summary

  • Use NSAttributedString for fixed rich text and NSMutableAttributedString for editable styling.
  • Apply attributes by range when different parts of the text need different formatting.
  • Use NSMutableParagraphStyle for alignment and line-spacing control.
  • Consider Swift's AttributedString for modern SwiftUI-friendly code.
  • Choose the API family that matches the frameworks you are working with.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.