-->

How to subscript letters when not there is no glyp

2019-07-23 02:55发布

问题:

I have been able to successfully subscript numbers in my app by doing:

static const unichar kSubscriptZero = 0x2080;
    int numberOfHydrogens = 2;
    NSString *header1 = [NSString stringWithFormat:@"H%CO",
                         kSubscriptZero + numberOfHydrogens];

The problem is this: I tried doing the same things with other characters that can be found in unicode characters but it did not work, all I got was a square box instead of my character, turns out that means that there is no glyph in my font for that character. I reviewed a previous question asked Here, and tried this for other characters other than numbers:

    if (section == whicheverSectionIndexIsCorrect) {
    NSString *plainText = @"2H2 + O2 → 2H2O";
    id subscriptOffset = @(-0.5); // random guess here, adjust offset as needed

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:plainText];

    // apply attributes for each character to subscript
    [text addAttribute:(NSString *)kCTSuperscriptAttributeName 
                 value:subscriptOffset
                 range:NSMakeRange(2, 1)];
    [text addAttribute:(NSString *)kCTSuperscriptAttributeName
                 value:subscriptOffset
                 range:NSMakeRange(7, 1)];
    // etc.

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
    UILabel *label = [[UILabel alloc] init];
    label.attributedText = text;
    [view addSubview:label];
    return view;
}

But my problem is this: If I change the decimal digits in subscriptOffset it has no effect at all (so -0.1 gives me the same result as -0.9) but when I change that number from 1.0 to 2.0 and so on... my problem is that is too big of a jump. How can I approach this and get the nice subscript look I am going for?

Update The following is a screenshot of my current situation: I need to be able to subscript "t"