Search code examples
stringcocoa-touchcocoansstringstrikethrough

NSString with lines through each letter


How can I write a string like the following:

H̶̶o̶̶w̶ ̶t̶̶o̶ ̶w̶̶r̶̶i̶̶t̶̶e̶ ̶t̶̶h̶̶i̶̶s̶ ̶t̶̶e̶̶x̶̶t̶

In my app, I need to display the past price of a product that has been replaced by a new price. Like so:

$̶8̶̶0̶̶0̶ -> $600


Solution

  • To get this result you should use NSAttributedString and set the attribute name NSStrikethroughStyleAttributeName. The number used as object for this key defines the style of the line over the text.

    self.myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"800" attributes:@{NSStrikethroughStyleAttributeName: @1}];
    

    Cheers