How can I create a UILabel with strikethrough text? How can I create a UILabel with strikethrough text? swift swift

How can I create a UILabel with strikethrough text?


SWIFT 4 UPDATE CODE

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, 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.


In Swift, using the enum for single strikethrough line style:

let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])label.attributedText = attrString

Additional strikethrough styles (Remember to access the enum using .rawValue):

  • NSUnderlineStyle.StyleNone
  • NSUnderlineStyle.StyleSingle
  • NSUnderlineStyle.StyleThick
  • NSUnderlineStyle.StyleDouble

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

  • NSUnderlineStyle.PatternDot
  • NSUnderlineStyle.PatternDash
  • NSUnderlineStyle.PatternDashDot
  • NSUnderlineStyle.PatternDashDotDot

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

  • NSUnderlineStyle.ByWord


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;