首页 > 解决方案 > 超级/下标似乎在 iOS13 中被破坏(NSAttributedString)

问题描述

尝试在 UITextView 中使用 NSAttributedString 显示超级/下标文本似乎在 iOS13 中被破坏了 - 除非有人知道吗?

奇怪的是,如果我使用 UIFont systemFont 那么它可以工作 - 但如果我使用任何其他字体,它就不会。

请参阅下面的代码以在我的测试应用程序中设置 UITextView。

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

标签: nsattributedstringios13core-text

解决方案


对此进行简单的“修复”。

看来 kCTSuperscriptAttributeName 在 iOS13 中不再适用(对于非系统字体)。您需要改用 NSSuperscriptAttributeName 。不知道它的定义在哪里(哪个标头),因此所需的实际字符串值是“NSSuperScript”


推荐阅读