Is there a way in NSString to output the st, nd, and rd but in a superscripted format? Any known unicode perhaps?
Asked
Active
Viewed 1,049 times
1 Answers
2
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).

Sam Clewlow
- 4,293
- 26
- 36

jscs
- 63,694
- 13
- 151
- 195
-
iOS 3.2+ for the iPad; iOS 4.0+ for the iPhone according to [a comment on this answer](http://stackoverflow.com/questions/729135/why-no-nsattributedstring-on-the-iphone/2705683#2705683). – May 06 '11 at 05:30
-
@Bavarious: ...and this is why I need to learn to look for the tag of the OS that I don't so know much about before getting 95% of the way through answering a question! :) Thanks for pointing that out. – jscs May 06 '11 at 05:34
-
Awesome, unfortunately just realised that NSAttributedString does not inherit from NSString =( Just curious, if I happen to do this: NSString *string = [premiere string]; What would be the result of string? Thanks – Rpranata May 06 '11 at 06:33
-
@Ren-D: That's right. I believe that `NSAttributedString` actually wraps an `NSString`. There is a method called `string` which gives you the characters (as an `NSString`), but not the attributes. – jscs May 06 '11 at 06:36