Swift
UILabel
iOS Development
Underline Text
SwiftUI

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.

Browse interview questions

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.

swift
1import UIKit
2
3let label = UILabel()
4label.textColor = .label
5
6let text = "Terms and Conditions"
7let attributed = NSAttributedString(
8    string: text,
9    attributes: [
10        .underlineStyle: NSUnderlineStyle.single.rawValue
11    ]
12)
13
14label.attributedText = attributed

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:

swift
1let attributed = NSAttributedString(
2    string: "Reset password",
3    attributes: [
4        .underlineStyle: NSUnderlineStyle.single.rawValue,
5        .underlineColor: UIColor.systemBlue,
6        .foregroundColor: UIColor.systemBlue
7    ]
8)
9
10label.attributedText = attributed

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.

swift
1import UIKit
2
3let label = UILabel()
4let fullText = "Read the privacy policy"
5let attributed = NSMutableAttributedString(string: fullText)
6
7let target = (fullText as NSString).range(of: "privacy policy")
8attributed.addAttribute(
9    .underlineStyle,
10    value: NSUnderlineStyle.single.rawValue,
11    range: target
12)
13attributed.addAttribute(
14    .foregroundColor,
15    value: UIColor.systemBlue,
16    range: target
17)
18
19label.attributedText = attributed

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.

swift
1import UIKit
2
3func underline(label: UILabel) {
4    let text = label.text ?? ""
5    let attributed = NSAttributedString(
6        string: text,
7        attributes: [
8            .font: label.font as Any,
9            .foregroundColor: label.textColor as Any,
10            .underlineStyle: NSUnderlineStyle.single.rawValue
11        ]
12    )
13    label.attributedText = attributed
14}

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:

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        Text("Terms and Conditions")
6            .underline()
7            .foregroundColor(.blue)
8    }
9}

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

  • 'UILabel underlining is done with NSAttributedString, not with a dedicated label property.'
  • Use .underlineStyle to underline the whole string or a selected range.
  • 'NSMutableAttributedString is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.