xcode 字符串IOS中的上标字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10512255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
superscript characters in string IOS
提问by Daniel
My app in xcode has a tableview with some product data. One of my products have a superscript e in its name. How can I use superscript characters in a string like: texte
我在 xcode 中的应用程序有一个包含一些产品数据的 tableview。我的一款产品的名称中有一个上标 e。如何在字符串中使用上标字符,例如:text e
I can get it to work with numbers: text\u2070 -> text0or text\u2071 -> text1. But how to do this with other characters?
我可以让它与数字一起使用: text\u2070 -> text 0或 text\u2071 -> text 1。但是如何对其他角色做到这一点呢?
thx!
谢谢!
回答by Jason
tl;dr: NSString does not support concept of super/sub script. That's more or less a UI formatting concern.
tl;dr:NSString 不支持超级/子脚本的概念。这或多或少是一个 UI 格式问题。
One possible solution is to dynically add UILabels in code instead of interface builder. You can add a second UILabel with a smaller font size.
一种可能的解决方案是在代码中动态添加 UILabels 而不是界面构建器。您可以添加具有较小字体大小的第二个 UILabel。
回答by Adam
Add CoreText framework, import CoreText.h, and use UIlabel.attributedText - it has full support for NSAttributedString. Asked and answered repeatedly on SO already.
添加 CoreText 框架,导入 CoreText.h,并使用 UIlabel.attributedText - 它完全支持 NSAttributedString。已经在 SO 上反复询问和回答。
回答by Bhushan
For SubScript use value for kCTSuperscriptAttributeName as @-1.
As per the document
@discussion Value must be a CFNumberRef. Default is int value 0. If supported by the specified font, a value of 1 enables superscripting and a value of -1 enables subscripting.
extern const CFStringRef kCTSuperscriptAttributeName CT_AVAILABLE(10_5, 3_2);
对于 SubScript,将 kCTSuperscriptAttributeName 的值设为 @-1。
根据文件
@discussion 值必须是 CFNumberRef。默认值为 int 值 0。如果指定字体支持,则值为 1 启用上标,值 -1 启用下标。
extern const CFStringRef kCTSuperscriptAttributeName CT_AVAILABLE(10_5, 3_2);
Example- [lblHeader setText:@“Headers [Alpha1 – text”];
NSMutableAttributedString *headerSubscript = [[NSMutableAttributedString alloc]initWithAttributedString: lblHeader.attributedText];
[headerSubscript addAttribute:(NSString *)kCTSuperscriptAttributeName value:@-1 range:NSMakeRange(14,1)];
[lblHeader setAttributedText:headerSubscript];