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

时间:2015-08-03 01:16:12

标签: ios objective-c uitextview nsattributedstring dtcoretext

我希望使用DTCoreText从HTML到NSAttributedString有多个/嵌套的超类。

如果我采用以下HTML:

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

在Stackoverflow上正确呈现为:

  

一些带有sup er scr ipt

的文本

但在DTCoreText中,它呈现为:

enter image description here

如果你注意到它几乎以相反的顺序显示,那么上标就会下降。

如何正确显示?

1 个答案:

答案 0 :(得分:5)

如何使用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:NSFontAttributeName value:myStringFont1 range:NSMakeRange(0,4)];
[myString addAttribute:NSUnderlineColorAttributeName value:myStringColor1 range:NSMakeRange(0,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(30) 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:@(50) 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:@(60) range:NSMakeRange(12,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont4 range:NSMakeRange(12,4)];
[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(12,4)];

这是第二个例子的输出: output from second example

WITH 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)];
[myString addAttribute:NSFontAttributeName value:myStringFont1 range:NSMakeRange(0,4)];
[myString addAttribute:NSUnderlineColorAttributeName value:myStringColor1 range:NSMakeRange(0,4)];
[myString addAttribute:NSBaselineOffsetAttributeName value:@(30) 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:@(50) 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:@(60) range:NSMakeRange(12,4)];
[myString addAttribute:NSFontAttributeName value:myStringFont4 range:NSMakeRange(12,4)];
[myString addAttribute:NSParagraphStyleAttributeName value:myStringParaStyle1 range:NSMakeRange(12,4)];

with HTML