NSString 中的上标序数后缀

发布于 2024-11-05 12:46:03 字数 59 浏览 2 评论 0原文

NSString 有没有办法以上标格式输出 st、nd 和 rd?也许有任何已知的 unicode 吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-11-12 12:46:03

似乎没有任何 Unicode 字符,但制作一个 NSAttributedString 就可以了窍门:

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!

您可能还想更改上标的字体大小。这是通过在属性字典中包含带有适当字体对象的 NSFontAttributeName 来实现的。请注意,NSAttributedString 仅适用于 iOS 4.0+ 的 iPhone 和 3.2+ 的 iPad(请参阅评论)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文