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

前端之家收集整理的这篇文章主要介绍了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)];

原文链接:https://www.f2er.com/iOS/336450.html

猜你在找的iOS相关文章