Swift 3.1 - NSSuperScript in NSAttributedString not working properly

The application I'm working with has encountered a problem when testing with iOS 10.3 Simulator via Xcode 8.3 beta 2, where the superscript in AttributedString is displayed on the same line as plain text. For iOS 10.2.x and below, it displays correctly.

iOS 10.3 screenshot: https://www.dropbox.com/s/p5v71g722cg5qhy/Screen%20Shot%202017-02-21%20at%2010.24.21%20AM.png?dl=0

iOS 10.2.x and below screenshot: https://www.dropbox.com/s/lcfsic6xyz953qp/Screen%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0

This is how I processed the text:

  • The original string is in HTML format and the tag for the text is "8.9" above

<html> <head> <style> body { color: #554344 ; font-family: \'MyCustomFont\'; font-size: 18px; } sup { font-size: 13px; } </style> </head> <body> ABCdef<sup>1,2,3,8,9</sup> </body> </html> 
  • The html string is then converted to NSAttributedString using the following script

 private func convertHTMLToAttributedString(string: String) -> NSAttributedString { guard let data = string.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return NSAttributedString() } return try! NSAttributedString( data: data, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) } 
  • Then NSAttributedString will be displayed through UILabel.attributedText This is an attributedText description:

 1,2,3,8,9{ NSColor = "kCGColorSpaceModelRGB 0.333333 0.262745 0.266667 1 "; NSFont = "<UICTFont: 0x7fed0a469880> font-family: \"MyCustomFont\"; font-weight: normal; font-style: normal; font-size: 10.00pt"; NSKern = 0; NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 13/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; NSStrokeColor = "kCGColorSpaceModelRGB 0.333333 0.262745 0.266667 1 "; NSStrokeWidth = 0; NSSuperScript = 1; } 

Note. I thought the problem was with our custom font, but the problem still occurs when we use the default font.

Is this a problem with Swift 3.1 and needs to be fixed?

+1
swift3 nsattributedstring xcode8-beta6 superscript
source share
1 answer

We found that the problem in UILabel attributed text rendering in iOS 10.3, and not in Swift 3.1. Affected style for us.

For our specific scenario (we have all the attributes specified in NSAttributedString without using the UILabel properties) this solution:

 /// 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) } } } 
+1
source share

All Articles