Search code examples
nsattributedstringios13core-text

Super/Subscript appear to be broken in iOS13 (NSAttributedString)


Trying to display super/subscript text using NSAttributedString in a UITextView seems broken in iOS13 - unless anyone knows otherwise?

Curiously if I use the UIFont systemFont then it works - but if I use any other font it doesn't.

See below for my code to setup a UITextView in my test app.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIFont* font = [UIFont systemFontOfSize:32];
    //UIFont* font = [UIFont fontWithName:@"Helvetica Neue" size:32];
    //UIFont* font = [UIFont fontWithName:@"Courier" size:32];
    //UIFont* font = [UIFont fontWithName:@"Arial" size:32];

    NSMutableAttributedString* as = [[NSMutableAttributedString alloc] initWithString:@"Super2Script" attributes:@{NSFontAttributeName : font}];

    [as addAttribute:(NSString*)kCTSuperscriptAttributeName value:@(1) range:NSMakeRange(5, 1)];



    UITextView* tv = [[UITextView alloc] initWithFrame:CGRectZero];
    tv.attributedText = as;
    [tv sizeToFit];

    [self.view addSubview:tv];
    tv.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);    
}

Solution

  • Simple 'fix' for this one.

    It appears kCTSuperscriptAttributeName no longer works in iOS13 (for non-system fonts.) You need to use NSSuperscriptAttributeName instead. No idea where the definition for this lives (which header) so the actual string value required is "NSSuperScript"