Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a subscript with NSAttributedString

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

like image 458
Corey Floyd Avatar asked Feb 03 '10 19:02

Corey Floyd


2 Answers

Try NSSuperscriptAttributeName with a negative value.

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

like image 78
Peter Hosey Avatar answered Oct 24 '22 15:10

Peter Hosey


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.

like image 34
Christian J. B. Avatar answered Oct 24 '22 15:10

Christian J. B.