开发者

Superscripted ordinal suffix in NSString

开发者 https://www.devze.com 2023-03-03 20:46 出处:网络
Is there a way in NSString to output the开发者_如何学Python st, nd, and rd but in a superscripted format? Any known unicode perhaps?There doesn\'t seem to be any Unicode characters for this, but it\'s

Is there a way in NSString to output the开发者_如何学Python st, nd, and rd but in a superscripted format? Any known unicode perhaps?


There doesn't seem to be any Unicode characters for this, but it's easy enough to make an NSAttributedString that will do the trick:

NSDictionary * superscriptAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] 
                                                         forKey:NSSuperscriptAttributeName];
NSAttributedString * st = [[NSAttributedString alloc] initWithString:@"st"
                                                              attributes:superscriptAttrs];

NSMutableAttributedString * premiere = [[NSMutableAttributedString alloc] initWithString:@"1"];

[premiere appendAttributedString:st];
// Don't forget to release everything when you're done with it!

You might also want to change the font size of the superscript. This is accomplished by including the NSFontAttributeName in the attributes dictionary with an appropriate font object. Note that NSAttributedString is only available on the iPhone in iOS 4.0+, and on the iPad in 3.2+ (see comment).

0

精彩评论

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