UILabel
Swift
iOS Development
Underline Text
Swift Programming

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

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.

swift
1import UIKit
2
3let label = UILabel()
4let text = "Tap to learn more"
5
6let attributes: [NSAttributedString.Key: Any] = [
7    .underlineStyle: NSUnderlineStyle.single.rawValue
8]
9
10label.attributedText = NSAttributedString(string: text, attributes: attributes)

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.

swift
1let attributes: [NSAttributedString.Key: Any] = [
2    .font: UIFont.systemFont(ofSize: 16, weight: .medium),
3    .foregroundColor: UIColor.systemBlue,
4    .underlineStyle: NSUnderlineStyle.single.rawValue
5]
6
7label.attributedText = NSAttributedString(string: "Open details", attributes: attributes)

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.

swift
1let fullText = "Read the terms and conditions"
2let attributed = NSMutableAttributedString(string: fullText)
3
4let range = (fullText as NSString).range(of: "terms and conditions")
5attributed.addAttribute(
6    .underlineStyle,
7    value: NSUnderlineStyle.single.rawValue,
8    range: range
9)
10
11label.attributedText = attributed

This lets you underline one phrase without affecting the rest of the label.

Different Underline Styles

NSUnderlineStyle supports more than one visual style.

swift
1let attributes: [NSAttributedString.Key: Any] = [
2    .underlineStyle: NSUnderlineStyle.double.rawValue,
3    .underlineColor: UIColor.systemRed
4]
5
6label.attributedText = NSAttributedString(string: "Warning", attributes: attributes)

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.

swift
1label.attributedText = NSAttributedString(
2    string: "Underlined",
3    attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue]
4)
5
6label.text = "Plain text now"

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.

swift
1func underline(label: UILabel, text: String) {
2    label.attributedText = NSAttributedString(
3        string: text,
4        attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue]
5    )
6}
7
8underline(label: label, text: "Profile")

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 UILabel by assigning an attributed string to attributedText.
  • Use .underlineStyle with an NSUnderlineStyle raw value.
  • 'NSMutableAttributedString is the right choice for underlining only part of a label.'
  • Reassigning label.text later removes the attributed styling.
  • Underlined text looks link-like, but interaction still needs separate UI handling.

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.