iOS 10.3: Nsstrikethroughstyleattributename Is Not Rendered If Applied to a Sub Range of Nsmutableattributedstring

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.

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

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.

WKInterfaceLabel Attributed String Fails on Width

This appears to be a bug but one with a workaround. The fix is to use the following line of code:

attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributedString.length))

This solves it in both Watch OS as well as iOS.

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

NSBackgroundColorAttributeName doesn't seem to work on iOS 10.3

Copied from @schystz comment:

Adding NSBaselineOffsetAttributeName: 0 resolves the issue.

let paddedLineAttributed = NSMutableAttributedString(string: paddedLine, attributes: [NSFontAttributeName : newFont, NSParagraphStyleAttributeName : paragraphStyle, NSBackgroundColorAttributeName : color, NSBaselineOffsetAttributeName: 0])

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))


Related Topics



Leave a reply



Submit