How to underline a UILabel in swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A UILabel does not have a dedicated "underline" Boolean property. To underline its text, you assign an attributed string with the appropriate underline attribute and set that on the label's attributedText property.
Underline The Entire Label Text
The simplest case is underlining the full string.
That tells UIKit to render the string with a single underline.
Keep Existing Font And Color Styling
If you only set the underline attribute, UIKit still displays the text, but in real UI code you often want to preserve or explicitly set the font and color as part of the attributed string.
This is especially useful when the underlined text should resemble a tappable link.
Underline Only Part Of The Text
Use NSMutableAttributedString when only a range should be underlined.
This lets you underline one phrase without affecting the rest of the label.
Different Underline Styles
NSUnderlineStyle supports more than one visual style.
A separate underline color is optional, but it can help when the underline should stand out from the text color.
Reapply The Attributed Text After Changing The String
A frequent source of confusion is setting label.text after setting label.attributedText. Assigning text later replaces the styled content.
After that second assignment, the underline is gone because the label is no longer using the attributed string.
Helper Function For Reuse
If multiple labels need the same styling, wrap it in a helper.
That keeps view-controller code cleaner and avoids repeated attribute dictionaries. If the label already uses mixed fonts or colors, build the underline on top of the existing attributed content rather than recreating the whole string with one flat attribute set.
Interactive Labels Are A Separate Concern
Underlining text can make a label look tappable, but the underline itself does not add interaction. If the user should tap it like a link, you still need a gesture recognizer or a button-like control.
For truly button-like behavior, UIButton is often the better widget unless the surrounding layout or attributed text requirements specifically call for UILabel.
Common Pitfalls
The most common mistake is setting label.text instead of label.attributedText. Another is creating an attributed string and then later overwriting it with plain text. Developers also often forget that partial underlining requires a text range, which means using NSMutableAttributedString. Finally, visual styling does not create interactivity by itself, so an underlined label is not automatically tappable.
Summary
- Underline a
UILabelby assigning an attributed string toattributedText. - Use
.underlineStylewith anNSUnderlineStyleraw value. - '
NSMutableAttributedStringis the right choice for underlining only part of a label.' - Reassigning
label.textlater removes the attributed styling. - Underlined text looks link-like, but interaction still needs separate UI handling.
Related reading
- How to underline a UILabel in swift?
- How to uninstall downloaded Xcode simulator?
- How to update Gradle in Android Studio?
- How to update RecyclerView Adapter Data
- How to update the constant height constraint of a UIView programmatically?
- How to use 3rd Party Libraries in Ios Extension?
- How to use a custom font with dynamic text sizes in iOS7
- How to use ADB Shell when Multiple Devices are connected? Fails with error more than one device and emulator
.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.