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

A strikethrough (single, double, ...) added as attribute to an instance of NSMutableAttributedString is not rendered if the apply range is not the whole string range.

This happens using addAttribute(_ name: String, value: Any, range: NSRange), insert(_ attrString: NSAttributedString, at loc: Int), append(_ attrString: NSAttributedString), ...

Broken by Apple in early iOS 10.3 betas, and not fixed in 10.3 final.

Credit: https://openradar.appspot.com/31034683

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.

rdar://31034683: NSStrikethroughStyleAttributeName is not , NSStrikethroughStyleAttributeName is not rendered if applied to a Product: iOS​, Product Version: 10.3 beta 2 - 10.3 beta 5 Apply the strikethrough using NSMutableAttributedString attribute to a subrange of the string 4. It's my function written in String extension. It will return an attributted string. Its not rendering attributed text on my UILabel. func strikeThroughLine(color: UIColor, textFont: UIFont, textCo

Setting the baseline offset seems to fix it:

[attributedStr addAttribute:NSBaselineOffsetAttributeName value:@0 range:NSMakeRange(0, 10)];
[attributedStr addAttribute:NSStrikethroughStyleAttributeName value:@2 range:NSMakeRange(0, 10)];

This is a known bug in iOS 10.3

NSStrikethroughStyleAttributeName …, NSStrikethroughStyleAttributeName not working on device iOS 10.3 beta /ios-​10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub. I am seeing the same behaviour as you've described on iOS 10.3 beta and tvOS 10.2 beta that NSStrikethroughStyleAttributeName doesn't work on the devices, but works

Found a workaround for our specific scenario (we don't specify any styling with UILabel's properties, but all with NSAttributedString attributes):

/// This UILabel subclass accomodates conditional fix for NSAttributedString rendering broken by Apple in iOS 10.3
final class PriceLabel: UILabel {

    override func drawText(in rect: CGRect) {
        guard let attributedText = attributedText else {
            super.drawText(in: rect)
            return
        }

        if #available(iOS 10.3, *) {
            attributedText.draw(in: rect)
        } else {
            super.drawText(in: rect)
        }
    }
}

NOTE: if you mix UILabel's styling properties with NSAttributedString attributes, you should think of creating a new attributed string before rendering, apply UILabel's styling on it and then re-apply all attributedText's attributes over it.

swift, swift - iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString -. a strikethrough (single, double, . Q&A for power users of Apple hardware and software. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

swift 3 working code tested with 10.3

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "₹3500")
attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))
productPriceLabel.attributedText = attributeString

strikeThrough Attribute is not compatible with iOS 13, I have used this String extension to add strikeThrough to the text rawValue, range: NSMakeRange(0, attributeString.length)) return attributeString } On iOS attributedText = nil default: let text = NSAttributedString(string: "This should /ios-​10-3-nsstrikethroughstyleattributename-is-not-rendered-​if-applied-to-a-sub By  Function: NSMutableAttributedString declares the programmatic interface to objects that manage mutable attributed strings. Notes: You can add and remove characters (raw strings) and attributes separately or together as attributed strings.

Swift 4
let text = "Hello World"
let textRange = NSMakeRange(0, text.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSAttributedStringKey.strikethroughStyle,
                            value: NSUnderlineStyle.styleSingle.rawValue,
                            range: textRange)
myLabel.attributedText = attributedText

rdar://31174934: NSAttributedString strikethrough doesn't work with , Product: iOS + SDK (UIKit), Product Version: iOS 10.3 [14E5265a] addAttribute(​NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributeString.length)) attributeString. .com/questions/43074652/ios-10-3-​nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub. Getting the final rendered height of a static page in a WKWebView; Angular 2 login not working in safari and firefox; How to Deep Link Push Notification in WP8; Akka actors scaling across JVM's and servers, and example at akka.io; How to store javascript object or JSON in html element usiing html5 data attribute?

User rshev, iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString · ios swift uikit asked Mar 28 '17 at 16:11. If you're not in CGLand (or not needing the same code to work on the desktop and device), there's also UIBezierPath's -bezierPathWithRoundedRect:cornerRadius: (for all corners), and -bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: (for some arbitrary subset of corners), and NSBezierPath's -bezierPathWithRoundedRect:xRadius:yRadius

iOS+10+3+NSStrikethroughStyleAttributeName+is+not+rendered+if ,

Comments
  • Fixed in iOS 10.3.2 beta 2
  • Doesn't seem to work in 10.3.2 current release, but it works in iOS 11.
  • You saved the day! :D
  • This also works for superscript and subscript attributes.
  • It is working but I have a doubt "NSBaselineOffsetAttributeName" default value is zero. Then why we need to Set the value.
  • It's clearly a bug so it must have some sort of rendering side effect
  • Not working!!!!
  • This as been killing me all day, so glad you found a solution
  • For what it's worth, I've also found that for iOS 10.3 release, attributed strings are not rendered with the font style set on the UILabel property. So for example, it is not enough to specify the UILabel's font type if you are using attributed Text property. You must include the desired font via NSFontAttributeName as an attribute
  • when i override drawText function as you said, it will ignore all label style i gave in storyboard (alignment, font, color). is there anyway to cast all these styles to NSMutableAttributedString's attribute?
  • @Mohammadalijf that what I put into the note underneath my answer - read them from the label's properties and reapply as NSAttributedString attributes.
  • Great! Had to spent almost an hour to find out those keys NSAttributedStringKey.strikethroughStyle, etc.
  • The point is use NSUnderlineStyle.styleSingle.rawValue not NSUnderlineStyle.styleSingle.