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

十年热恋 提交于 2019-12-31 07:18:07

问题


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);    
}

回答1:


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"



来源:https://stackoverflow.com/questions/58300329/super-subscript-appear-to-be-broken-in-ios13-nsattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!