I've been trying for a while now to create a NSString with subscripted character without success. Is it even possible to do this in iOS?
I need a way to change characters in a string to subscript or superscript, and I can't use the Unicode for this as Unicode doesn't have all the letters.
My guess could be to use the HTML tags <sub>
and <sup>
but I haven't find a way to convert said HTML tags to a NSString.

- 315
- 2
- 3
- 9
3 Answers
I wasn't able to get NSSuperscriptAttributeName to work but had success with the following:
UILabel *label = [[UILabel alloc] init];
NSString *string = @"abcdefghi";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSInteger num1 = 1;
CFNumberRef num2 = CFNumberCreate(NULL, kCFNumberNSIntegerType, &num1);
[attrString addAttribute:(id)kCTSuperscriptAttributeName value:(id)num2 range:NSMakeRange(4,2)];
label.attributedText = attrString;
[attrString release];
This gives you:
Assigning the attributed String to a label via label.attributedText is new with 6.0, but the way the attributed string is set-up might work with earlier versions of iOS.
Sending a negative value to kCTSuperscriptAttributeName give you a subscript.
Don't forget to add the CoreText framework.

- 594
- 5
- 12
-
import #import
– FirstTimer Oct 30 '12 at 15:54 -
1Thanks - I was struggling to find subscripts, and the -1 (or as a string: @"-1") for value worked perfect. NB: you don't need to do the CFNumberRef mess, just pass in a string to the value: parameter – Adam Sep 16 '13 at 07:45
-
Thanks for this answer, it really helped me out. Should be the accepted answer. – Frederic Adda Jan 30 '17 at 19:14
Subscript and superscript are not character traits. With few exceptions (e. g. ², ³, ª), this is the way regular characters are rendered - in a smaller font and above/below the regular characters' baseline. With this in mind, you cannot have an "NSString with subscripted characters", no more than you can have an NSString with bold or italic characters.
So as a result, unless the desired subscripted character exists in Unicode already, subscript and superscript is created on string rendering, not on string creation. And this is not a limitation of iOS, this is the limitation of the way strings are processed in modern computers.
What do you do to that NSString? Do you display it on a UILabel? Do you send it over the network? Do you render it as HTML? Note that <sub>
and <sup>
are HTML tags; unless the NSString is interpreted specifically as HTML (say, by a UIWebView), they won't be interpreted as sup/superscript.

- 59,826
- 25
- 160
- 281
-
I want to display the string on a UILabel. How do I change the rendering then? – Xrieaz Feb 22 '11 at 15:57
-
@Xrieaz: UILabel does not support sub/superscript. Either replace with a UIWebView, or combine several UILabels, some with regular font and some with smaller font and carefully shifted placement. I recommend the former approach; then you can use HTML. – Seva Alekseyev Feb 22 '11 at 16:05
-
2I made it work with a `UIWebView` and the `loadHTMLstring:baseURL:`function. I wonder if I should post an answer for it or not. – Xrieaz Feb 24 '11 at 12:23
-
To clarify, there are Unicode ranges for both superscript and subscript Latin letters (http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts), but they are not widely supported by fonts. – richardtallent Jun 21 '11 at 17:47
-
2@SevaAlekseyev UILabel can use an attributedString which supports sub/super-scripting – Frederic Adda Jan 30 '17 at 19:13
There's a component that can do that, via some categories on NSString and UILabel. Although it might be overkill for your situation:
NSString *stringWithTags = @"<b>Bold</b> <sup>sup</sup> <sub>sub</sub> <u>Under</u> <strike>strike</strike> <i>Italic</i> <small>small</small> <b><u>U+B</u></b>";
NSAttributedString *attributedString = [[RCTagProcessor defaultInstance] attributedStringForText:stringWithTags];
Or you could get set the text on a label straight away:
[self.label rc_setTaggedText:stringWithTags];

- 2,318
- 18
- 20
-
Or nowadays, you can easily convert this without external libraries https://stackoverflow.com/a/18886718/1271826. – Rob Jan 16 '18 at 17:19
-
Agreed, however if performance is a concern it might still be worth the tradeoff: "Comparing RCTagProcessor with NSAttributedString's -[initWithData: options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: error:] shows 1.6x to 8.3x better performance, depending on the string complexity. Most likely due to the much bigger flexibility of the NSHTMLTextDocument conversion's approach that requires a more cumbersome parser." – user3099609 Jan 17 '18 at 18:42