IOS 10.3: NSStrikethroughStyleAttributeName is not displayed if applied to the NSMutableAttributedString subband

Strikethrough (single, double, ...) added as an attribute to an NSMutableAttributedString instance is not displayed unless the range of application is an integer range of lines.

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

Broken by Apple in early versions of iOS 10.3 and not fixed in the final 10.3.

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

+37
ios uikit swift
Mar 28 '17 at 16:11
source share
7 answers

By adding NSBaselineOffsetAttributeName as described here , a strikethrough string is returned to the attribute string. Overriding drawText:in: can be slow, especially in Collection View or Table View cells.

+22
Apr 03 '17 at 3:39 on
source share

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

+72
Apr 12 '17 at 2:28 on
source share

A workaround was found for our specific scenario (we do not specify a style with UILabel 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 style properties with NSAttributedString attributes, you should consider creating a new attribute string before rendering, apply the UILabel style to it, and then reapply all attributedText attributedText to it.

+13
Mar 28 '17 at 16:11
source share

Swift 3 work file 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 
+7
Apr 27 '17 at 7:00
source share

Its a bug known to Xcode 8.3 (8E3004b) / iOS 10.3 with this workaround. NO extra line of code needed, just add [NSBaselineOffsetAttributeName:0] when declaring NSMutableAttributedString()

 let attrStr = NSMutableAttributedString(string: YOUR_STRING_HERE, attributes: [NSBaselineOffsetAttributeName : 0]) // Now if you add the strike-through attribute to a range, it will work attrStr.addAttributes([ NSFontAttributeName: UIFont.boldSystemFont(ofSize: 24), NSStrikethroughStyleAttributeName: 1 ], range: NSRange) 
+3
Jul 09 '17 at 18:39
source share

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 
+1
Dec 01 '17 at 18:33
source share
 (void)drawRect:(CGRect)rect{ [super drawRect:rect]; // 取文字的颜色作为删除线的颜色[self.textColor set]; CGFloat w = rect.size.width; CGFloat h = rect.size.height; // 绘制(0.5是label的中间位置,可以自己调整) UIRectFill(CGRectMake(0, h * 0.5, w, 1)); } 
-four
Mar 31 '17 at 7:50
source share



All Articles