[Swift 3.1] NSSuperScript in NSAttributedString not working as expected

The application I am working on encountered an issue when testing with iOS 10.3 Simulator via XCode 8.3 beta 2, where the superscript in AttributedString displayed on the same line with normal text. For iOS 10.2.x and below, it is displaying 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

Here's how I handled the text:

- Initially, the string is in HTML format, and <sup> tag for the text "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 then converted into 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)
   }

- The NSAttributedString then will be rendered via UILabel.attributedText

This is the 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 issue was related to our custom font, but the issue still happen when we use the default font.


Is this an issue related with Swift 3.1 and should be fixed?

Replies

Is this an issue related with Swift 3.1 and should be fixed?

IMO this is unlikely to be a Swift 3.1 beta problem, but instead a problem with the base OS. It’s easy to test that theory:

  1. Create a small test app

  2. Build it with Xcode 8.3 beta and run that built version on both iOS 10.2 and 10.3 beta

  3. Build it with Xcode 8.2.1 and run that on both iOS 10.2 and iOS 10.3 beta

My guess is that the problem will occur on iOS 10.3 beta, regardless of how you build the app. If that’s the case, you should file a bug against the beta.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

This bug is still present iOS 10.3 release version.

This bug is still present iOS 10.3 release version.

Bummer. Just for my own records, what was your bug number for this?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Arrgh. Still not fixed in the new 10.3.1 update.


My bug number for this was 31411715, but that came back as duplicate of 30649979.

My bug number for this was 31411715, but that came back as duplicate of 30649979.

AFAICT this is Just a Bug™ )-:

How much grief is this causing your real app? There’s an obvious workaround here (implement your own view that draws the text manually using some lower-level API, like CoreText) but it’s likely to be a lot of work so I wouldn’t recommend going down that path unless it’s really critical.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have a graphing calculator app which allow entry of equations. For example:

x² + 2

now displays as

x2 + 2


Not good. This bug occurs in UITextField/UILabel, not UITextView, but I do not really want to change all my UITextFields to UITextViews to work around an iOS bug.

I had another look at this and noticed something interesting: the superscript attribute (

NSSuperscriptAttributeName
) is not actually present in the iOS SDK (it’s in the macOS SDK, but not the iOS SDK). Thus it’s not a huge surprise that this doesn’t work (although it is super annoying that it used to work and is now broken).

Have you thought about translating

<sup>
into some other attributes? Perhaps a change of font size (
NSFontAttributeName
) and a baseline shift (
NSBaselineOffsetAttributeName
) would be sufficient?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hmm, based on the fact it is not in the iOS SDK, it may not be treated as a bug.


This is unfortunate, as I have macOS, iOS, and wstchOS versions of my app that share a lot of code. That seemed like the direction Apple is going, but in this case it seems to be diverging. Looks like I will have to rework my iOS code. Thanks for the suggestion.

… based on the fact it is not in the iOS SDK, it may not be treated as a bug.

Perhaps, but it’s not really my call. You’ll have to wait for the UIKit engineering to take an in depth look at your bug and then make the final judgement here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"