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
Detect Current Device With Ui_User_Interface_Idiom() in Swift
Ios: Multi-Line Uilabel in Auto Layout
Change Default Scrolling Behavior of Uitableview Section Header
Basic Example For Sharing Text or Image With Uiactivityviewcontroller in Swift
Transparent iOS Navigation Bar
Make a Phone Call Programmatically
Uilabel - Auto-Size Label to Fit Text
Adding a View Controller as a Subview in Another View Controller
How to Programmatically Check Whether a Keyboard Is Present in iOS App
How to Get the Color of a Pixel in a Uiimage With Swift
Using Custom Fonts in Wkwebview
How to Disable/Enable the Return Key in a Uitextfield
How to Programmatically Sense the Iphone Mute Switch
How to Know When Uitableview Did Scroll to Bottom in Iphone
Uibutton Inside a View That Has a Uitapgesturerecognizer