Example of NSAttributedString with two different font sizes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NSAttributedString makes it easy to display mixed font sizes in one label, such as a large number with smaller unit text. The key is combining attributes over ranges in one mutable attributed string. A reusable pattern keeps formatting logic clean and avoids fragile hardcoded indices.
Core Sections
Build a Basic Mixed-font Attributed String
Create one NSMutableAttributedString, then apply font attributes to target ranges.
This is the baseline approach for mixed emphasis text.
Add Color and Baseline Alignment
Different font sizes can misalign visually. Use baseline offset and color attributes for polished results.
Small baseline adjustments often improve readability in metric displays.
Build a Reusable Formatter Function
Avoid duplicating attribute code across view controllers. A helper function gives consistent typography.
Using helpers makes theme changes significantly easier.
Handle Localization and Dynamic Type
If text is localized, range logic should avoid brittle hardcoded offsets. Compute ranges using string search on localized output. For accessibility, combine attributed styling with scalable fonts.
You may still need custom scaling behavior for visually balanced mixed-size text.
Integrate in Reusable Cells
When using attributed strings in table or collection cells, reset label properties during reuse to avoid style carryover. Keep formatting deterministic based on model data.
This prevents stale styling artifacts during fast scrolling.
Apply Attributed Text in SwiftUI via UIKit Bridge
If your project uses SwiftUI but still needs rich attributed formatting, use UIViewRepresentable to host a UILabel. This allows consistent formatting logic across UIKit and SwiftUI screens.
Keeping one formatter function for both frameworks avoids duplicated typography rules and keeps localization updates centralized.
A centralized text-style factory also reduces visual drift across teams and features.
When visual consistency matters, define typography tokens for value text, unit text, and baseline offset in one theme layer. Then build attributed strings from those tokens rather than hardcoded constants. This makes dark-mode and brand refresh updates straightforward and keeps rendering predictable across different screens.
Add snapshot tests for key attributed labels to prevent subtle typography regressions.
Consistent attributed-text utilities also speed UI maintenance across teams.
It also improves consistency during design-system migrations.
Common Pitfalls
- Hardcoding numeric ranges that break when text values change.
- Ignoring baseline adjustment and ending with awkward vertical alignment.
- Repeating attribute setup code in many view files.
- Forgetting accessibility scaling for mixed-size text.
- Not clearing attributed text in reusable UI components.
Summary
- Use
NSMutableAttributedStringand range-based attributes for mixed font sizes. - Apply font, color, and baseline attributes for polished typography.
- Encapsulate formatting logic in helper functions.
- Compute ranges dynamically for localization-safe behavior.
- Reset attributed content in reusable views to avoid visual artifacts.

