Skip to content

HTML tags: <sub></sub>, <sup></sup> #49

@cmaldonados

Description

@cmaldonados

I needed to use superscript/subscript but MDHTMLLabel doesn't support it, so I implemented it and though it works, it doesn't for iOS9.x. Texts are not subscripted nor superscripted. I've reviewed the code but can't make it where the problem could be.
This is the code I'm using that I implemented, showing subscript only:

- (void)applySubscriptToText:(CFMutableAttributedStringRef)text
                       range:(NSRange)range {
    /* Because Helvetica Neue doesn't handle subscript AND because CT added kCTSuperscriptAttributeName key until ios11, this code is commented out:
    CFAttributedStringSetAttribute(text, CFRangeFromNSRange(range), kCTSuperscriptAttributeName, (__bridge CFTypeRef)(@-1));
     */
    
    NSMutableAttributedString *attributedString = (__bridge NSMutableAttributedString *)(text);
    UIFont *subScriptFont = [UIFont fontWithName:self.font.fontName size:self.font.pointSize * 0.6];
    [attributedString addAttribute:NSFontAttributeName value:subScriptFont range:range];
    [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(-1.5) range:range];
}

If you use iOS11 you can use the commented code and discard the rest (or you can leave the code as is and it will work for iOS > 9.x), otherwise it only works for iOS > 9.x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions