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
UILabel does not have a direct isUnderlined property, so underlining is done with attributed text. In UIKit, that means building an NSAttributedString with an underline attribute and assigning it to label.attributedText. Once you understand that pattern, you can underline the whole string or only selected ranges.
Underline the Entire Label Text
The most common solution is to create an attributed string and apply NSUnderlineStyle.single across the full text range.
This works because UILabel can render both plain text and attributed text. As soon as you assign attributedText, the underline is part of the rendered string rather than a separate label property.
If you also want a custom underline color, add .underlineColor:
Matching the text color and underline color usually produces the cleanest result.
Underline Only Part of the Text
Sometimes you only want one phrase underlined, such as a link-like suffix. For that, use NSMutableAttributedString and apply the underline to a range.
This is the right approach when only one word or phrase should look interactive. It also scales well when you need to mix fonts, colors, and underline styles in the same label.
Preserve Existing Font and Styling
A frequent annoyance is losing the label’s existing font when switching from text to attributedText. If you want the label to keep its current font, include it explicitly in the attributes.
That helper is useful when you already configured the label in Interface Builder and only want to add underlining at runtime.
What About SwiftUI?
The title of this article is about UILabel, which is UIKit. If you are actually using SwiftUI, the equivalent is much simpler:
That distinction matters because UILabel and Text do not share the same styling API. In UIKit, attributed strings are still the standard solution.
Common Pitfalls
One common mistake is setting label.text after label.attributedText. Assigning plain text later replaces the attributed string and removes the underline.
Another issue is forgetting to preserve font and color. If you only set the underline attribute, the label may still render correctly, but mixed styling and repeated updates can become inconsistent.
Developers also sometimes use a bottom border or a separate line view to fake underlining. That can work visually, but it does not move with descenders, line breaks, or dynamic type the way real text underlining does.
Finally, do not confuse UIKit and SwiftUI APIs. Text(...).underline() is a SwiftUI feature and does not apply to UILabel.
Summary
- '
UILabelunderlining is done withNSAttributedString, not with a dedicated label property.' - Use
.underlineStyleto underline the whole string or a selected range. - '
NSMutableAttributedStringis the right tool for partial underlining.' - Preserve the label’s font and color when replacing plain text with attributed text.
- If you are using SwiftUI instead of UIKit,
Text(...).underline()is the simpler equivalent.
Related reading
- 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
- How to use an existing database with an Android application
.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.