How to use Attributed String in SwiftUI
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Modern SwiftUI has first-class support for rich text through AttributedString. That means many use cases that once required wrapping UILabel or juggling NSAttributedString can now be handled directly in SwiftUI with a clearer, value-type API.
Start with AttributedString
If you target a recent SwiftUI stack, the natural entry point is Foundation’s AttributedString.
Text can render an AttributedString directly. That is the simplest modern path.
Style Only Part of the Text
The main benefit of attributed text is that different ranges can carry different styles.
This is much cleaner than splitting a string into several Text views just to vary one word’s styling.
Build Rich Text from Markdown
AttributedString can also be initialized from Markdown, which is very convenient for content-driven UIs.
This is especially useful when formatted text comes from configuration, CMS content, or local documentation-like strings.
Bridge from NSAttributedString When Needed
You may still receive NSAttributedString from older APIs or UIKit code. Bridging is possible.
This lets you move incrementally from UIKit-style attributed text toward native SwiftUI code.
Links and Inline Semantics
Attributed text is also useful for semantic values such as links.
SwiftUI can render that as interactive rich text without a custom text view wrapper.
When Text Is Enough and When It Is Not
AttributedString is excellent for inline styling, links, emphasis, and moderate rich-text use cases.
It may still be insufficient when you need:
- advanced text editing
- complex text layout behavior
- full UIKit text-system features not surfaced in SwiftUI
In those cases, wrapping UIKit through UIViewRepresentable may still be appropriate. But for display-only rich text, modern AttributedString covers much more than older SwiftUI versions did.
Keep the Styling Logic Local
A practical pattern is to build a small helper that returns attributed content for one UI concern.
That keeps formatting rules explicit and testable instead of burying them in the view body.
Common Pitfalls
The biggest mistake is reaching for NSAttributedString wrappers immediately when AttributedString and Text already solve the display problem.
Another mistake is using many concatenated Text fragments for styling that would be cleaner as one attributed value.
Developers also sometimes forget that not every NSAttributedString attribute maps cleanly into SwiftUI’s native rendering model. Bridging helps, but it does not guarantee full UIKit behavior.
Finally, keep target-platform constraints in mind. If you support older OS versions, verify that the APIs you rely on are available in your deployment target.
Summary
- In modern SwiftUI, use
AttributedStringas the primary rich-text API. - '
Textcan render anAttributedStringdirectly.' - Style substrings by finding ranges and assigning attributes to them.
- Markdown and
NSAttributedStringbridging are both useful entry points. - Use UIKit wrappers only when your text needs exceed what SwiftUI’s native attributed text support provides.
Related reading
- How to use Auto Layout to move other views when a view is hidden?
- How to use background thread in swift?
- How to use background thread in swift?
- How to use background thread in swift?
- How to use Charles Proxy on the Xcode 6 iOS 8 Simulator?
- How to use custom font in a project written in Android Studio
- How to use dark mode in iOS simulator?
- How to use data-binding with Fragment
.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.