title: NSAttributedString with sub/superscript on iOS 13
body: <p>I maintain an app written in Objective-C and in some places it shows chemical formulas To implement the sub- and superscripts I have this code in an <code>NSString</code> extension.</p>
  
  <pre><code>- (NSMutableAttributedString *) chemicalFormulaWithFont: (UIFont *) font
  {
      if (self.length == 0)
          return nil;
  
      NSDictionary *normalAttribute = @{NSFontAttributeName: font};
      NSDictionary *baselineAttribute = @{NSBaselineOffsetAttributeName : @0};
  
      UIFont *smallFont = [UIFont fontWithName: font.fontName
                                          size: font.pointSize * 0.75];
  
      NSString *superscriptAttributeName = (NSString *)kCTSuperscriptAttributeName;
  
      NSDictionary *subscriptAttribute = @{superscriptAttributeName : @-1,
                                           NSFontAttributeName: smallFont};
  
      NSDictionary *superscriptAttribute = @{superscriptAttributeName : @1,
                                             NSFontAttributeName: smallFont};
  
      NSCharacterSet *decimalDigits = [NSCharacterSet decimalDigitCharacterSet];
  
      NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: self];
  
      NSUInteger length = attributedString.length;
      NSRange range = NSMakeRange(0, length);
      [attributedString addAttributes: normalAttribute range: range];
  
      while (range.location != NSNotFound)
      {
          range = [attributedString.string rangeOfCharacterFromSet: decimalDigits options: 0 range: range];
  
          if (range.location != NSNotFound)
          {
              unichar ch = [attributedString.string characterAtIndex: range.location - 1];
  
              if ((ch != '-') &amp;&amp; (ch != '+'))
              {
                  [attributedString addAttributes: subscriptAttribute range: range];
              }
  
              range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
          }
      }
  
      range = NSMakeRange(0, length);
  
      // if last two characters are +, - , 2+, 3+, 2-, etc then add superscript
  
      unichar ch = [attributedString.string characterAtIndex: length - 1];
  
      if ((ch == '-') || (ch == '+'))
      {
          range = NSMakeRange(length - 1, 1);
  
          ch = [attributedString.string characterAtIndex: length - 2];
  
          if (ch &gt;= '0' &amp;&amp; ch &lt;='9')
              range = NSMakeRange(length - 2, 2);
          [attributedString addAttributes: superscriptAttribute range: range];
      }
  
      [attributedString addAttributes: baselineAttribute range: range];
  
      return attributedString;
  }
  </code></pre>
  
  <p>The font is "Avenir-Medium"</p>
  
  <p>This worked fine for iOS 12 but on iOS 13 I no longer see the sub and superscripts.</p>
  
  <p>Problem is, my Mac is too old to run Xcode 11 - I am stuck on version 10.2. So I cannot debug this for iOS > 12.1, not even with my iPhone connected (which does run iOS 13).</p>
  
  <p>One thing I could think of is that Avenir-Medium no longer supports this on iOS 13, but it seems unlikely.</p>
  
  <p>Anything else that could have changed in iOS 13 with respect to <code>NSAttributedString</code> ?</p>