How can I create a UILabel with strikethrough text?

84,301

Solution 1

SWIFT 5 UPDATE CODE

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))

then:

yourLabel.attributedText = attributeString

To make some part of string to strike then provide range

let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)

Objective-C

In iOS 6.0 > UILabel supports NSAttributedString

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                        value:@2
                        range:NSMakeRange(0, [attributeString length])];

Swift

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

Definition :

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange

Parameters List:

name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

value : The attribute value associated with name.

aRange : The range of characters to which the specified attribute/value pair applies.

Then

yourLabel.attributedText = attributeString;

For lesser than iOS 6.0 versions you need 3-rd party component to do this. One of them is TTTAttributedLabel, another is OHAttributedLabel.

Solution 2

In Swift, using the single strikethrough line style:

let attributedText = NSAttributedString(
    string: "Label Text",
    attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
label.attributedText = attributedText

Additional strikethrough styles (Remember to use the .rawValue):

  • .none
  • .single
  • .thick
  • .double

Strikethrough patterns (to be OR-ed with the style):

  • .patternDot
  • .patternDash
  • .patternDashDot
  • .patternDashDotDot

Specify that the strikethrough should only be applied across words (not spaces):

  • .byWord

Solution 3

I prefer NSAttributedString rather than NSMutableAttributedString for this simple case:

NSAttributedString * title =
    [[NSAttributedString alloc] initWithString:@"$198"
                                    attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];

Constants for specifying both the NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName attributes of an attributed string:

typedef enum : NSInteger {  
  NSUnderlineStyleNone = 0x00,  
  NSUnderlineStyleSingle = 0x01,  
  NSUnderlineStyleThick = 0x02,  
  NSUnderlineStyleDouble = 0x09,  
  NSUnderlinePatternSolid = 0x0000,  
  NSUnderlinePatternDot = 0x0100,  
  NSUnderlinePatternDash = 0x0200,  
  NSUnderlinePatternDashDot = 0x0300,  
  NSUnderlinePatternDashDotDot = 0x0400,  
  NSUnderlineByWord = 0x8000  
} NSUnderlineStyle;  

Solution 4

Strikethrough in Swift 5.0

let attributeString =  NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
                                     value: NSUnderlineStyle.single.rawValue,
                                         range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString

It worked for me like a charm.

Use it as extension

extension String {
    func strikeThrough() -> NSAttributedString {
        let attributeString =  NSMutableAttributedString(string: self)
        attributeString.addAttribute(
            NSAttributedString.Key.strikethroughStyle,
               value: NSUnderlineStyle.single.rawValue,
                   range:NSMakeRange(0,attributeString.length))
        return attributeString
    }
}

Call like this

myLabel.attributedText = "my string".strikeThrough()

UILabel extension for strikethrough Enable/Disable.

extension UILabel {

func strikeThrough(_ isStrikeThrough:Bool) {
    if isStrikeThrough {
        if let lblText = self.text {
            let attributeString =  NSMutableAttributedString(string: lblText)
            attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
            self.attributedText = attributeString
        }
    } else {
        if let attributedStringText = self.attributedText {
            let txt = attributedStringText.string
            self.attributedText = nil
            self.text = txt
            return
        }
    }
    }
}

Use it like this :

   yourLabel.strikeThrough(btn.isSelected) // true OR false

Solution 5

SWIFT CODE

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

then:

yourLabel.attributedText = attributeString

Thanks to Prince answer ;)

Share:
84,301
Dev
Author by

Dev

iOS developer

Updated on January 20, 2022

Comments

  • Dev
    Dev over 2 years

    I want to create a UILabel in which the text is like this

    enter image description here

    How can I do this? When the text is small, the line should also be small.

  • Dev
    Dev over 11 years
    For iOS 5.1.1 lesser version how can I use 3 party attributed Label to display attributed text: ?
  • Dev
    Dev over 11 years
    Can You suggest a good Toutorial? The link which you provided is little bit difficult to understand.. :(
  • Dev
    Dev over 11 years
    Can you explain what I should do for creating a third party attributed Label for ios
  • pronebird
    pronebird about 10 years
    What is @2? Magic number?
  • pronebird
    pronebird about 10 years
    I guess you forgot to commit that. You should use a proper value from NSUnderlineStyle instead of @2. I am little bit pedantic here.
  • Mikkel Selsøe
    Mikkel Selsøe about 9 years
    It can be expressed shorter, without the need for mutability: [[NSAttributedString alloc] initWithString:@"string" attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)}];
  • Mihai Fratu
    Mihai Fratu over 8 years
    Up voted for using the right constant instead of a number
  • Jonauz
    Jonauz about 7 years
    For me it doesn't work if I try to strikethrough only part of string, but not all. Is this intended? And is it possible to still make it strikethrough just one word inside long text?
  • Hassan Taleb
    Hassan Taleb about 7 years
    Why NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue is not working in swift 4 ?
  • Rashid KC
    Rashid KC almost 7 years
    How to get striked on the center of the label?
  • James Toomey
    James Toomey almost 7 years
    See this thread if you have issues with the strikethrough appearing.
  • kuzdu
    kuzdu almost 6 years
    Doesn't work for me. Purnendu roy's answer work for me. The only difference is that you pass in value 0 and Purnendu roy pass value: NSUnderlineStyle.styleSingle.rawValue
  • Muhammad Asyraf
    Muhammad Asyraf almost 6 years
    @kuzdu funny thing that my answer was back in dec 2017 it works back then he just copied my code and add up NSUnderlineStyle.styleSingle.rawValue ^-^ But no problem i will update this answer just to make you happy
  • Naresh
    Naresh almost 5 years
    Difference between value: 1 and value: 2
  • Vladimir Pchelyakov
    Vladimir Pchelyakov almost 5 years
    @iOS value is the thickness of the line that strikethrough the text. The larger the value, the thicker the line that crosses out the text
  • Jeroen
    Jeroen about 4 years
    Do you happen to know a solution to StrikeThrough not being removed? Similar to forums.developer.apple.com/thread/121366
  • Dania Delbani
    Dania Delbani almost 4 years
    smart solution 👍
  • Leo Dabus
    Leo Dabus over 3 years
    @VladimirPchelyakov No. The value corresponds to NSUnderlineStyle rawValue (NSNumber). 1 = single, 2 = thick, 9 = double, and there is many other styles between thick and double