ios – 如何在DTCoreText中正确呈现嵌套/多个上标?

我想要使​​用 DTCoreText从HTML到NSAttributedString的多个/嵌套超级.

如果我采取以下HTML:

Some text with a sup<sup>er<sup>scr<sup>ipt</sup></sup></sup>

它在Stackoverflow上正确呈现:

Some text with a superscript

但是在DTCoreText中,它呈现为:

如果你注意到它几乎以相反的顺序显示,上标下降.

如何正确显示

解决方法

如何使用NSAttributedString和避免已经做了IOS本机的第三方库?这是你可能想要的,第一部分只是标准的mutableStrings,然后我将添加HTML:
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc]initWithString:
                                       @"thisthisthisthis"];

UIFont *myStringFont1 = [UIFont systemFontOfSize:24.0];
UIFont *myStringFont2 = [UIFont systemFontOfSize:14.0];
UIFont *myStringFont3 = [UIFont systemFontOfSize:12.0];
UIFont *myStringFont4 = [UIFont systemFontOfSize:11.0];

UIColor *myStringColor1 = [UIColor redColor];

NSMutableParagraphStyle *myStringParaStyle1 = [[NSMutableParagraphStyle alloc]init];
myStringParaStyle1.alignment = NSTextAlignmentCenter;


[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(0,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont1 range:NSMakeRange(0,4)];
[myString addAttribute:NSUnderlineColorAttributeName value:myStringColor1 range:NSMakeRange(0,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(4) range:NSMakeRange(4,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont2 range:NSMakeRange(4,4)];
[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(4,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(6) range:NSMakeRange(8,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont3 range:NSMakeRange(8,4)];
[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(8,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(8) range:NSMakeRange(12,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont4 range:NSMakeRange(12,4)];
[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(12,4)];

这是另一个例子,这一个有更大的效果来匹配你想要的:

NSMutableAttributedString *myString = [[NSMutableAttributedString alloc]initWithString:
                                       @"thisthisthisthis"];

UIFont *myStringFont1 = [UIFont systemFontOfSize:50.0];
UIFont *myStringFont2 = [UIFont systemFontOfSize:25.0];
UIFont *myStringFont3 = [UIFont systemFontOfSize:12.5];
UIFont *myStringFont4 = [UIFont systemFontOfSize:6.25];

UIColor *myStringColor1 = [UIColor redColor];

NSMutableParagraphStyle *myStringParaStyle1 = [[NSMutableParagraphStyle alloc]init];
myStringParaStyle1.alignment = NSTextAlignmentCenter;


[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(0,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(30) range:NSMakeRange(4,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(50) range:NSMakeRange(8,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(60) range:NSMakeRange(12,4)];

这是第二个例子的输出

带HTML:

Nsstring *htmlString = @"<h1>Header</h1><h2>Subheader</h2>";

NSAttributedString *myString1 = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];


NSMutableAttributedString *myString = [[NSMutableAttributedString alloc]initWithAttributedString:
                                       myString1];

UIFont *myStringFont1 = [UIFont systemFontOfSize:50.0];
UIFont *myStringFont2 = [UIFont systemFontOfSize:25.0];
UIFont *myStringFont3 = [UIFont systemFontOfSize:12.5];
UIFont *myStringFont4 = [UIFont systemFontOfSize:6.25];

UIColor *myStringColor1 = [UIColor redColor];

NSMutableParagraphStyle *myStringParaStyle1 = [[NSMutableParagraphStyle alloc]init];
myStringParaStyle1.alignment = NSTextAlignmentCenter;


[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(0,4)];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...