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.
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:
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:
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:
Paragraph Styling
Paragraph-level formatting uses NSMutableParagraphStyle:
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:
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:
- '
NSAttributedStringfor framework compatibility' - '
AttributedStringfor modern Swift ergonomics'
A Practical UIKit Example
Assign an attributed string to a UILabel:
This is one of the most common real-world uses of attributed text on iOS.
Common Pitfalls
- Using
NSAttributedStringwhen you actually need mutation and should be usingNSMutableAttributedString. - 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
NSAttributedStringfor fixed rich text andNSMutableAttributedStringfor editable styling. - Apply attributes by range when different parts of the text need different formatting.
- Use
NSMutableParagraphStylefor alignment and line-spacing control. - Consider Swift's
AttributedStringfor modern SwiftUI-friendly code. - Choose the API family that matches the frameworks you are working with.
Related reading
- How do I make an enum Decodable in Swift?
- How do I make UILabel display outlined text?
- How do I make WRAP_CONTENT work on a RecyclerView
- How do I modify the background color of a List in SwiftUI?
- How do I obtain crash-data from my Android application?
- How do I open developer tools on iOS Simulator?
- How do I open phone settings when a button is clicked?
- How do I open phone settings when a button is clicked?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.