1

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 image of a checmical formula with subscripts

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.

Community
  • 1
  • 1
vzm
  • 2,440
  • 6
  • 28
  • 47
  • 1
    UILabel has an `attributedText` property that accepts an `NSAttributedString`. So, have your section header view be a UILabel (or a view containing a UILabel) with attributedText? – Mike Mertsock Jul 30 '13 at 20:57
  • @esker would you mind showing me some sample code of this? – vzm Jul 30 '13 at 21:05

2 Answers2

4

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

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

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.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
  • Pretty sure that HTML stuff is Mac-only. – Zev Eisenberg Jul 30 '13 at 21:55
  • I was afraid of that. Good to know. – Mike Mertsock Jul 30 '13 at 22:54
  • @esker I tried this, but I get the following build warning: `Incompatible pointer types sending 'const CFStringRef' (aka 'const struct __CFString *const') to parameter of type 'NSString *'` and nothing happens to my section header. I've added the appropriate framework and imported the right header files, any suggestions? – vzm Jul 30 '13 at 23:06
  • I edited the answer to include some `(NSString *)` casts that appeared to be necessary, and chose a value for the subscript offset that seems to be about right. – Mike Mertsock Jul 30 '13 at 23:24