I want to have multiple/nested superscipts from HTML to NSAttributedString using DTCoreText.
If I take the following HTML:
Some text with a sup<sup>er<sup>scr<sup>ipt</sup></sup></sup>
Which renders on Stackoverflow correctly as:
Some text with a superscript
But in DTCoreText it renders as:
Where if you notice it's shown in reverse order almost, where the superscript descends.
How do I display it properly?
How about using NSAttributedString and avoiding 3rd Party libraries that already do what IOS does natively? Here's what you probably want, the first part is just standard mutableStrings, then I'll add on the 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)];
Here's another example, this one has an even greater effect to match what you want:
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)];
Here's the output from the 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)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With