Nsstrikethroughstyleattributename , How to Strike Out the String in iOS 10.3

NSStrikethroughStyleAttributeName , How to strike out the string in iOS 10.3?

it is the bug in iOS 10.3 , NSStrikethroughStyleAttributeName (any NSUnderlineStyle cases) is not working any more on iOS SDK 10.3.

if anyone found the updated answer related to this , please inform here, I will update my answer.

Product Version: 10.3

Created: 14-Mar-2017

Originated: 14-Mar-2017

Open Radar Link: http://www.openradar.appspot.com/31034683

Radar status is Currently Open state

you can see the alternate sample also here may be it useful.

iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString

Adding a NSBaselineOffsetAttributeName, as explained here, to the attributed string brings back the strikethrough line. Overriding drawText:in: can be slow especially on Collection View or Table View Cells.

How can I create a UILabel with strikethrough text?

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.

Why strikethrough is not working in iOS 14

Adding baselineOffset with value 0 to the attributed string brings back the strikethrough line. More details can be found on apple dev forum and this SO thread.

attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 0, range: totalNsString.range(of: strikeString))

NSMutableAttributedString not working in tableviewcell with a specific range

Try this,

   let strOriginalPrice = "my price"
let strdiscountedPrice = "discounted price"
let strPrice = strOriginalPrice+" "+strdiscountedPrice

// let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
// attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

let attributedDiscunt: NSMutableAttributedString = NSMutableAttributedString(string: strPrice)
attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
attributedDiscunt.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, strOriginalPrice.characters.count-1))

cell.lblPrice.attributedText = attributedDiscunt


Related Topics



Leave a reply



Submit