Superscripted ordinal suffix in NSString

吃可爱长大的小学妹 提交于 2020-01-02 00:16:09

问题


Is there a way in NSString to output the st, nd, and rd but in a superscripted format? Any known unicode perhaps?


回答1:


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).



来源:https://stackoverflow.com/questions/5905883/superscripted-ordinal-suffix-in-nsstring

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