开发者

Is there an easy way to make part of an NSString object a superscript or subscript?

For examp开发者_StackOverflowle, suppose I have an NSString @"20O(2H,1H)19O", and I want all the numbers to be superscript. Is there an easy way to do this?


I think you probably want NSAttributedString with NSSuperScriptAttributeName. If you need to keep it in an NSString, unicode has characters for superscripted digits.


This function should return a given number in superscript. Very simple

-(NSString *)superScriptOf:(NSString *)inputNumber{

    NSString *outp=@"";
    for (int i =0; i<[inputNumber length]; i++) {
    unichar chara=[inputNumber characterAtIndex:i] ;
    switch (chara) {
        case '1':
            NSLog(@"1");
            outp=[outp stringByAppendingFormat:@"\u00B9"];
            break;
        case '2':
            NSLog(@"2");
            outp=[outp stringByAppendingFormat:@"\u00B2"];
            break;
        case '3':
            NSLog(@"3");
            outp=[outp stringByAppendingFormat:@"\u00B3"];
            break;
        case '4':
            NSLog(@"4");
            outp=[outp stringByAppendingFormat:@"\u2074"];
            break;
        case '5':
            NSLog(@"5");
                            outp=[outp stringByAppendingFormat:@"\u2075"];
            break;
        case '6':
            NSLog(@"6");
                            outp=[outp stringByAppendingFormat:@"\u2076"];
            break;
        case '7':
            NSLog(@"7");
            outp=[outp stringByAppendingFormat:@"\u2077"];
            break;
        case '8':
            NSLog(@"8");
            outp=[outp stringByAppendingFormat:@"\u2078"];
            break;
        case '9':
            NSLog(@"9");
            outp=[outp stringByAppendingFormat:@"\u2079"];
            break;
        case '0':
            NSLog(@"0");
            outp=[outp stringByAppendingFormat:@"\u2070"];
            break;
        default:
            break;
    }
}
return outp;   
}

Given an input string of numbers it just returns the equivalent superscript string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜