Superscripted ordinal suffix in NSString
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).
精彩评论