系统字体中没有字形时如何下标字母?

时间:2013-08-01 18:16:56

标签: ios objective-c nsattributedstring subscript

我已经能够通过以下方式在我的应用中成功下标数字:

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

问题是这样的:我尝试用unicode字符中可以找到的其他字符做同样的事情但是它不起作用,我得到的只是一个方框而不是我的字符,结果证明没有我的字体中的字形为该字符。我查看了之前提出的问题Here,并尝试使用除数字以外的其他字符:

    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;
}

但我的问题是:如果我更改subscriptOffset中的十进制数字它根本没有效果(所以-0.1给出的结果与-0.9相同)但是当我将该数字从1.0更改为2.0时等等......我的问题是跳得太大了。我怎么能接近这个并获得我想要的漂亮的下标外观?

更新以下是我目前情况的截图:我需要能够下标“t” enter image description here

0 个答案:

没有答案