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 program
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.
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