如何向 NSString 添加 UTF-8 字符?

发布于 2024-12-23 10:16:08 字数 176 浏览 4 评论 0原文

我需要制作化学公式(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 技术交流群。

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

发布评论

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

评论(3

岛徒 2024-12-30 10:16:08

根据 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.

口干舌燥 2024-12-30 10:16:08

贾斯汀的回答很好,但我认为您可能真正需要的是 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) or NSMutableAttributedString, where you can add superscripts, subscripts, and other character styles that NSString 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!

如梦亦如幻 2024-12-30 10:16:08

是的。我假设您知道创建 NSString 的常规方法 - 这是从 utf8 字符串创建 NSString 的一种方法:-[NSString initWithUTF8String:]

yes. i assume you know the normal approach to make an NSString - here's one method to create an NSString from a utf8 string: -[NSString initWithUTF8String:].

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