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

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...