Use multiple font colors in a single label
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Multi-color text in one label is a common UI requirement for status badges, highlighted values, and contextual emphasis. Most platforms support this through attributed text models that style specific ranges without splitting content into many separate views. Reliable implementations depend on dynamic range detection, accessibility-safe color choices, and testable style logic.
Core Approach: Style by Text Range
The universal pattern is:
- construct full text,
- identify target segment ranges,
- apply color and optional additional attributes,
- render as one label element.
This keeps layout simple and avoids alignment issues that come from multiple adjacent labels.
iOS Example with NSAttributedString
Notice the dynamic range lookup for amount. It is safer than hardcoded indexes when values vary.
Android Example with SpannableString
For dynamic strings, always compute start and end from content, not fixed positions.
Web Example with Semantic Inline Segments
For browser interfaces, split the sentence into semantic inline spans and style in CSS.
This approach is easy to theme and works well with design tokens.
Localization-Safe Styling
Hardcoded character offsets often break when UI text is translated. Instead:
- style known tokens by substring search,
- style placeholders after interpolation,
- keep style rules tied to semantics rather than fixed indices.
Localization-safe iOS pattern:
Then locate status dynamically and apply attributes.
Accessibility and Contrast
Color should not be the only carrier of meaning. Pair color with weight, icon, or explicit text label so users with color-vision differences still understand state.
Also verify contrast ratio in both light and dark modes. A color that looks fine on a white background can fail badly in dark theme.
Practical checks:
- meet platform contrast guidance for normal text,
- test dynamic type sizes,
- verify high-contrast accessibility modes.
Performance in Recyclable Lists
On screens with many rows, avoid rebuilding attributed strings on every bind if content repeats frequently. Cache style computation by value key or precompute formatted text in view-model layers.
Simple memoization idea in Kotlin:
Keep caches bounded to avoid memory growth.
Testing Styled Labels
Style regressions are easy to miss visually. Add UI tests or snapshots that verify highlighted segments and expected color tokens. For platform unit tests, assert range detection logic so refactors do not silently style the wrong substring.
Common Pitfalls
- Hardcoding index ranges that break when text changes or localizes.
- Relying on color alone to communicate critical status information.
- Choosing colors with poor contrast in one theme mode.
- Recomputing heavy attributed styling in large scrolling lists without caching.
- Mixing business rules and style construction in UI classes without test coverage.
Summary
- Use attributed or spannable text to apply multiple colors in one label.
- Compute style ranges dynamically for variable and localized strings.
- Pair color with secondary emphasis for accessibility.
- Validate styling across themes, font scales, and contrast modes.
- Keep formatting logic testable and optimized for list-heavy interfaces.

