6

I see for NSAttributedString has a specific attribute for superscript, but I can't find one for subscript. What is the general practice for using NSAttributedString to create a subscript character?

Example: H2O

2 Answers 2

6

Try NSSuperscriptAttributeName with a negative value.

Failing that, the hard way would be to replace [0123456789] characters with [₀₁₂₃₄₅₆₇₈₉] in the string.

2
  • 1
    Works like a breeze with NSSuperscriptAttributeName! Note that the font size won't change though - just the y pos, the subscript must explicitly be made smaller (e.g. NSFontAttributeName).
    – Jay
    Nov 1, 2012 at 15:30
  • NB - NSSuperscriptAttributeName takes an integer. This allows only for pretty coarse adjustments of where you want the superscript (or subscript) to be positioned. I've found NSBaselineOffsetAttributeName easier to use for finer-grained control..
    – Jay
    Nov 23, 2012 at 13:50
2

I struggled with subscripts/superscripts in NSMutableAttributedString for a while because the most basic solutions require inputting a NSRange for each and every character you want to subscript. Surely there's a more automatic way of doing things?

Well, yes there is, but it requires a little bit of work.

My method is to indicate characters which are subscripted, superscripted, italicized and so on in a NSString by enclosing the text to be altered with % signs followed by information on what type of font adjustment should be made, e.g. 'The force on the second particle is given by f-subscript-b' would in my scheme be written @"The force on the second particle is given by f%&sb%".

I then use the handy method:

NSArray *substrings = [string componentsSeparatedByString:@"%"];

to chop up the string into substrings delimited by the % signs, e.g.

@"Hello %&Bhow are% you?" ->the array containing elements: @"Hello",@"&Bhow are",@"you?"

I then inspect the first character of each element of the array to see if it contains an & mark, which I use to denote that the next character will be either B=Bold, S=Superscript, I=italics etc.

So, in the example above, the substring @"&Bhow are" is intended to be converted into a bold-face string "how are" and the input @"Hello %&Bhow are% you?" is intended to be converted to "Hello how are you?".

All of the font modifications are performed using NSMutableAttributedString and its associated methods and finally all the NSMutableAttributedString substrings can be pasted back together using methods like 'appendAttributedString'.

If anyone's interested, my code is as follows:

-(void) appendFontString:(NSMutableAttributedString*) attribString
                         :(NSString*) string{
    NSArray *substrings = [string componentsSeparatedByString:@"%"];
    for(int i=0;i<(int) [substrings count];i++){
        if([substrings[i] length]>0){
        NSString* firstCharacter=[substrings[i] substringToIndex:1];

        if([firstCharacter isEqualToString:@"&"]){
            NSString* fontType=[substrings[i] substringWithRange:NSMakeRange(1, 1)];

            //remove first two characters
            NSString* newSubString=[substrings[i] substringFromIndex:2];
            if([fontType isEqualToString:@"S"]){
            [self appendWithSuperscript:attribString :newSubString];
            } else if([fontType isEqualToString:@"s"]){
                [self appendWithSubscript:attribString :newSubString];
            } else if([fontType isEqualToString:@"B"]){
                [self appendWithBold:attribString :newSubString];
            } else if([fontType isEqualToString:@"I"]){
                [self appendWithItalics:attribString :newSubString];
            }
            } else{
            //regular string
            [self append:attribString :substrings[i]];
        }
        }
    }

}

where the appendWithBold etc. methods are user-created methods which convert the NSString into a formatted bold/superscripted/subscripted/etc. NSMutableAttributedString and then append it to the variable 'attribString'.

Mine may not be the best method, but at least it demonstrates that with a bit of work you can automate subscripting and superscripting in Cocoa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.