xcode 在 UILabel 中显示重音和其他 UTF-8 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7218374/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:49:25  来源:igfitidea点击:

Displaying accents and other UTF-8 characters in a UILabel

iphoneobjective-cxcodecharacter-encodinguilabel

提问by msgambel

I have a little app which lists the names of certain people from around the world, and some of those names use characters that are not normal ASCIIcharacters, like Dìaz, or Thèr?se for example.

我有一个小应用程序,它列出了来自世界各地的某些人的名字,其中一些名字使用了不正常的ASCII字符,例如 Dìaz 或 Thèr?se。

The strings show up in Xcode just fine, but when I put them in a UILabel, they behave unexpectedly.

字符串在 Xcode 中显示得很好,但是当我将它们放入 a 时UILabel,它们的行为出乎意料。

My question is:Is there a way to set up a UILabelto to take the exact string in Xcode, and display it properly, even if it is a UTF-8character (or any other character encoding for that matter)?

我的问题是:有没有办法UILabel在 Xcode 中设置一个以获取确切字符串并正确显示它,即使它是一个UTF-8字符(或任何其他字符编码)?

回答by omz

UIKit fully supports unicode, your problem is most likely the encoding of the source file. You can set that in the inspector (Xcode 4: ??1) under "Text Settings". Make sure it is UTF-8 as well.

UIKit 完全支持 unicode,您的问题很可能是源文件的编码。您可以在“文本设置”下的检查器(Xcode 4:??1)中进行设置。确保它也是 UTF-8。

Alternative: Use unicode escapes like @"\u2605"(should display ★).

替代方法:使用 unicode 转义符@"\u2605"(应显示 ★)。

回答by Justin

Try to encode the String:

尝试对字符串进行编码:

NSString *s = [NSString stringWithCString:value encoding:NSASCIIStringEncoding];