如何向 NSString 添加 UTF-8 字符?
我需要制作化学公式(SO4^2-),制作下标和上标的最简单方法似乎是添加 UTF-8 字符,因为 NSAttributedString 的 KCTSuperscriptAttributeName:
属性不起作用。
我可以用普通字符和utf-8字符制作一个nsstring吗?
谢谢
I need to make chemistry formulas (SO4^2-), and the easiest way to make subscripts and superscripts seems to be adding UTF-8 characters, since KCTSuperscriptAttributeName:
property of NSAttributedString doesn't work.
Is it possible for me to make an nsstring with normal characters and utf-8 characters?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 NSString 参考 “NSString 的实现是为了表示 Unicode 字符数组,换句话说,就是一个文本字符串。”
写成下面这样会比较方便:
NSString* myStr = @"你想要的任何 Unicode 字符";
只需确保您的默认文本编码是 unicode 即可。
According to NSString Reference "NSString is implemented to represent an array of Unicode characters, in other words, a text string."
It would be convenient to write as below:
NSString* myStr = @"Any Unicode Character You Want";
Just make sure that your default text encoding is unicode.
贾斯汀的回答很好,但我认为您可能真正需要的是
NSAttributedString
(为您链接的文档)或NSMutableAttributedString
,您可以在其中添加上标、下标以及NSString
本身无法处理的其他字符样式。在这里或通过 Google 查看其他
NSAttributedString
问题,例如 这个可能相关的问题 或这个。希望这对您有帮助!
Justin's answer is good but I think what you might really be looking for is
NSAttributedString
(documentation linked for you) orNSMutableAttributedString
, where you can add superscripts, subscripts, and other character styles thatNSString
by itself can't handle.Take a look at other
NSAttributedString
questions here or via Google, like this potentially related question or this one.Hope this helps you out!
是的。我假设您知道创建
NSString
的常规方法 - 这是从 utf8 字符串创建NSString
的一种方法:-[NSString initWithUTF8String:]
。yes. i assume you know the normal approach to make an
NSString
- here's one method to create anNSString
from a utf8 string:-[NSString initWithUTF8String:]
.