nsattributedstring - 超级/下标似乎在 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);
}
解决方案
对此进行简单的“修复”。
看来 kCTSuperscriptAttributeName 在 iOS13 中不再适用(对于非系统字体)。您需要改用 NSSuperscriptAttributeName 。不知道它的定义在哪里(哪个标头),因此所需的实际字符串值是“NSSuperScript”
推荐阅读
- cobol - COBOL中的`NOT ON OVERFLOW`有什么用?
- javascript - 如何重写functions.php中创建的自定义cookie的值?
- c# - c#中的子字符串在字符串中查找第二部分
- c++ - 如何将 class1 的 std::function 绑定到 class2 的 func ?
- java - java.text.ParseException:无法解析的日期:“2018-07-22T14:00:00-03:00”
- javascript - 图像作为 html 画布的模板
- javascript - Firebase:orderByKey是否返回有序快照
- c - 使用 vsnprintf 格式化 C 中长度不确定的字符串
- vba - 无法使用索引匹配功能链接到外部文件
- c# - 如何为 json 结果删除 $?