How to create a subscript with NSAttributedString

谁说胖子不能爱 提交于 2019-12-19 07:06:31

问题


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


回答1:


Try NSSuperscriptAttributeName with a negative value.

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




回答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.



来源:https://stackoverflow.com/questions/2194860/how-to-create-a-subscript-with-nsattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!