UILabel
Strikethrough Text
iOS Development
Swift Programming
User Interface

How can I create a UILabel with strikethrough text?

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 shows strikethrough text by using an attributed string, not by setting a special label property. The core idea is to build an NSAttributedString or NSMutableAttributedString, apply the strikethrough attributes to the desired range, and assign the result to label.attributedText.

Apply Strikethrough to the Entire Label

If the whole label should be crossed out, the code is short.

swift
1import UIKit
2
3let label = UILabel()
4label.textColor = .label
5label.font = UIFont.systemFont(ofSize: 18)
6
7let text = "$19.99"
8let attributes: [NSAttributedString.Key: Any] = [
9    .strikethroughStyle: NSUnderlineStyle.single.rawValue,
10    .strikethroughColor: UIColor.systemRed
11]
12
13label.attributedText = NSAttributedString(string: text, attributes: attributes)

This is a common pattern for showing an old price next to a discounted one.

Apply Strikethrough to Only Part of the Text

If only one portion of the label should have a line through it, start with a mutable attributed string and apply the attribute to a specific range.

swift
1import UIKit
2
3let label = UILabel()
4let message = "Old price: $19.99  New price: $9.99"
5let attributed = NSMutableAttributedString(string: message)
6
7let oldPriceRange = (message as NSString).range(of: "$19.99")
8attributed.addAttribute(
9    .strikethroughStyle,
10    value: NSUnderlineStyle.single.rawValue,
11    range: oldPriceRange
12)
13attributed.addAttribute(
14    .strikethroughColor,
15    value: UIColor.systemRed,
16    range: oldPriceRange
17)
18
19label.attributedText = attributed

That approach scales well when a label mixes normal text, highlighted text, and struck-through text in one sentence.

Why Attributed Text Is the Right API

UILabel is designed to render styled text through attributedText. That gives you one place to control:

  • font
  • foreground color
  • strikethrough style
  • underline
  • kerning
  • paragraph style

So even if your current need is only a strikethrough, the attributed-string route is the correct long-term pattern because it composes naturally with other text styling features.

Styling Details That Matter

The key attribute is .strikethroughStyle. Its value is usually one of the NSUnderlineStyle raw values, such as .single.

If you also want a custom line color, use .strikethroughColor. If you omit it, the line typically follows the text color.

You can also combine strikethrough with other attributes in the same string, for example a lighter text color or smaller font for the crossed-out portion.

Updating Text in Reusable Views

If the label lives in a table or collection cell, build the attributed string every time the cell is configured. Reused cells can otherwise keep stale attributed content from earlier data.

A practical helper function keeps this logic contained.

swift
1func makeStrikethroughText(_ text: String) -> NSAttributedString {
2    return NSAttributedString(
3        string: text,
4        attributes: [
5            .strikethroughStyle: NSUnderlineStyle.single.rawValue,
6            .strikethroughColor: UIColor.systemGray
7        ]
8    )
9}

Then cell configuration becomes a simple assignment.

Interface Builder and UIKit Code

There is no Interface Builder checkbox that fully replaces attributed text here. You can set some static attributed content in design tools, but programmatic control is usually better because price strings, status labels, and task completion text are often dynamic.

That makes a code-based helper the most maintainable option.

Common Pitfalls

A common mistake is setting label.text after label.attributedText. Assigning plain text later replaces the styled content and removes the strikethrough.

Another mistake is using the wrong range when styling only part of the string. Working through NSString ranges is usually safer than guessing Swift string offsets for attributed text APIs.

A third issue is forgetting reused-cell behavior. If the label is reused in a scrolling list, always set the full attributed string during configuration.

Summary

  • 'UILabel uses attributed strings for strikethrough text.'
  • Use .strikethroughStyle for the line and .strikethroughColor when you need custom color.
  • Assign the result to label.attributedText, not label.text.
  • Use NSMutableAttributedString when only part of the text should be crossed out.
  • Rebuild the attributed text during cell reuse to avoid stale formatting.

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.