Underline text in UIlabel
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UILabel does not support underline styling through the plain text property. Underlines are applied with attributed strings, which gives precise control over style, color, and range. A clean implementation should also handle reuse, localization, and accessibility so formatting remains correct as text changes.
Apply Full Underline With Attributed Text
The simplest case is underlining the entire label string.
Use this when the whole label should be link-like or emphasized.
Underline Only Part of a String
Most UI needs partial underline such as “Read privacy policy” where only one phrase is underlined.
Using NSString range APIs avoids mistakes with Unicode indexing in many practical cases.
Preserve Existing Attributes While Updating Text
If labels already have fonts or colors assigned via attributed text, replacing everything can drop styles accidentally. Merge attributes carefully.
This pattern prevents style regressions when UI state updates frequently.
Reusable Extension for Teams
Centralize underline logic to avoid duplicate formatting code across view controllers.
With one helper, style changes become easier to enforce consistently.
Handle Dynamic Type and Reuse
In table or collection views, labels are reused. Always set attributed text in configure methods so old underline state does not leak to new content.
This avoids reused-cell artifacts where unrelated rows appear underlined.
Accessibility and Design Guidance
Underline often implies tappable content. If text is interactive, pair underline with traits and hit targets that communicate intent clearly. Do not rely on color alone to indicate link behavior. In high-contrast settings, verify underline remains visible and text remains legible.
If label text changes from localization, re-run range calculations on localized strings rather than assuming fixed English indices.
When underline indicates a tap action, pair label styling with gesture or button wrappers so interaction behavior matches visual expectations.
Common Pitfalls
A common pitfall is assigning label.text after attributedText, which removes underline formatting immediately. Another issue is computing ranges with hard-coded index offsets that break under localization. Teams also forget to reset attributed text in reusable cells, causing stale formatting. Underline color confusion is common as well, since underline follows text color unless explicitly configured in styled rendering contexts. Finally, accessibility can suffer when underline is used as the only signal for interactive content without proper traits.
Summary
- Underline in
UILabelrequires attributed strings, not plaintext. - Use full or partial underline based on UX needs.
- Preserve existing style attributes when updating dynamic content.
- Centralize formatting logic with a reusable extension.
- Validate reuse, localization, and accessibility to keep underline behavior reliable.

