How to add table view section header with subscrip

2020-02-11 12:45发布

问题:

In my app, I have a table view that I have managed so far with storyboards (I have added sections:rows:cells, etc.. all via storyboards), the only change I have made programmatically was to add a UIButton as one of the sections headers by implementing:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [button1 setTitle:@"Hydro Volume" forState:UIControlStateNormal];

        button1.frame = CGRectMake(62.5, 5, 205, 44);

        [view addSubview:button1];

        [button1 addTarget: self
                    action: @selector(buttonClicked:)
          forControlEvents: UIControlEventTouchDown];

        return view;
    }

    return nil;
}

My current dilemma is that I have to add a section header that contains subscripts, i.e.: H2O

I am unable to just add the subscript directly in the storyboard inspector, can someone tell me what is the way to do this?

I reviewed this question, but it's not quite what I am looking for as I need to be able to add it to my section header.

回答1:

One simple solution is to use the Unicode subscript range, U+2080 through U+2089. Example: 2 H₂ + O₂ -> 2 H₂O.

You can type one of these characters by using the Unicode Hex Input keyboard layout, holding Option, and typing the hex digits (e.g. hold option and type “2080” for “₀”).

Given a single digit, you can format it into a string as a subscript like this:

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

http://www.unicode.org/charts/PDF/U2070.pdf



回答2:

I think this is the gist of what you would do using attributed strings. I don't have Xcode in front of me at the moment so there may be bugs:

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

Edit: likely only available on OS X: I also noticed that there are NSAttributedString initializers that take HTML. I haven't used that so can't say whether it would work, but if it does work in iOS and understands subscripts, that may be simpler if your are loading these subscripted labels from a data store rather than hard-coding them.