简体   繁体   English

如何将UTF-8字符添加到NSString?

[英]How to add UTF-8 characters to an 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. 我需要制作化学公式(SO4 ^ 2-),制作下标和上标的最简单方法似乎是添加UTF-8字符,因为KCTSuperscriptAttributeName:属性不起作用。

Is it possible for me to make an nsstring with normal characters and utf-8 characters? 我可以使用普通字符和utf-8字符创建一个nsstring吗?

Thanks 谢谢

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. Justin的答案很好,但我认为你真正想要的是NSAttributedString (链接的文档)或NSMutableAttributedString ,你可以在其中添加上标,下标和NSString本身无法处理的其他字符样式。

Take a look at other NSAttributedString questions here or via Google, like this potentially related question or this one . 在这里或通过Google查看其他NSAttributedString问题,例如这个可能相关的问题 或者这个 问题

Hope this helps you out! 希望这可以帮助你!

According to NSString Reference "NSString is implemented to represent an array of Unicode characters, in other words, a text string." 根据NSString参考 “实现NSString来表示Unicode字符数组,换句话说,是一个文本字符串。”

It would be convenient to write as below: NSString* myStr = @"Any Unicode Character You Want"; 如下编写会很方便:NSString * myStr = @“你想要的任何Unicode字符”;

Just make sure that your default text encoding is unicode. 只需确保您的默认文本编码是unicode。

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:] . 我假设您知道生成NSString的常规方法 - 这是从utf8字符串创建NSString的一种方法: -[NSString initWithUTF8String:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM