How can I create a UILabel with strikethrough text?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.
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.
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.
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
- '
UILabeluses attributed strings for strikethrough text.' - Use
.strikethroughStylefor the line and.strikethroughColorwhen you need custom color. - Assign the result to
label.attributedText, notlabel.text. - Use
NSMutableAttributedStringwhen only part of the text should be crossed out. - Rebuild the attributed text during cell reuse to avoid stale formatting.

