Example of NSAttributedString with two different font sizes?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Example or explanation of Core Data Migration with multiple passes?
- Exception has occurred. FlutterError setState called after dispose _MyProfileStatec3ad1lifecycle state defunct, not mounted
- Exception 'open failed EACCES Permission denied' on Android
- Exception when trying to use tensorflow classify android example on a model trained from scratch
- Exchanging data between Android SQL-Lite and SQL Sever without using webserive
- Execute action when back bar button of UINavigationController is pressed
- Execute action when back bar button of UINavigationController is pressed
- Expand/collapse section in UITableView in iOS
.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.