XCode 中按钮标题中的 Unicode

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

Unicode in button title in XCode

objective-cxcodebuttonunicodetitle

提问by user1157134

I'm trying to display the greek letter pi (unicode \u03C0) on a button as the title. When I try to set the title using drag'n'drop graphical editor, the word "\u03C0" shows. Is there some way to set unicode text in the graphical editor, or do I need to do it programmatically? If I have to do it programmatically, what is the best way to do this?

我试图在按钮上显示希腊字母 pi (unicode \u03C0) 作为标题。当我尝试使用拖放图形编辑器设置标题时,会显示“\u03C0”一词。有什么方法可以在图形编辑器中设置 unicode 文本,还是我需要以编程方式进行设置?如果我必须以编程方式进行,那么最好的方法是什么?

I hope somebody can help me with this, since my google searches have been futile.

我希望有人能帮我解决这个问题,因为我的谷歌搜索是徒劳的。

I use iOS 5.0 XCode version 4.2.1

我使用 iOS 5.0 XCode 版本 4.2.1

回答by rob mayoff

Interface Builder

界面生成器

You can use Interface Builder to create a button with any Unicode character in its title. There are several methods.

您可以使用 Interface Builder 创建标题中包含任何 Unicode 字符的按钮。有几种方法。

  • As Tommy suggested, you can type Option-P to insert a greek character pi.
  • As Tommy also suggested, you can use the Keyboard Viewer from the Input menu to see what characters you can type using the option and control keys. You can also press the on-screen keyboard buttons in the keyboard viewer to insert those characters into the current text field (such as your button's title).
  • You can use the Character ViewerEmoji & Symbols window from bottom of the Edit menu (and possibly also the Input menu) to find other characters. Double-clicking a character in Emoji & Symbols will insert it into the current text field. Clicking the icon in the top right of this window toggles between basic and advanced modes.
  • You can enable the Unicode Hex Input source (System Preferences > Language & Text > Input Sources) and select it from the Input menu. Then you can hold Option and type four hex digits to enter a Unicode character. For example, you can hold Option and type "03c0" to enter the greek character pi.
  • If you find the character somewhere else, like on a web page, you can copy it from the source and then paste it in Interface Builder.
  • 正如汤米所建议的,您可以输入 Option-P 来插入一个希腊字符 pi。
  • 正如汤米还建议的那样,您可以使用输入菜单中的键盘查看器来查看您可以使用选项和控制键键入哪些字符。您还可以按键盘查看器中的屏幕键盘按钮将这些字符插入当前文本字段(例如按钮的标题)。
  • 您可以使用编辑菜单(也可能是输入菜单)底部的字符查看器表情符号和符号窗口来查找其他字符。双击表情符号和符号中的字符会将其插入到当前文本字段中。单击此窗口右上角的图标可在基本模式和高级模式之间切换。
  • 您可以启用 Unicode 十六进制输入源(系统偏好设置 > 语言和文本 > 输入源)并从输入菜单中选择它。然后您可以按住 Option 键并键入四个十六进制数字以输入 Unicode 字符。例如,您可以按住 Option 并键入“03c0”以输入希腊字符 pi。
  • 如果您在其他地方找到该字符,例如在网页上,您可以从源代码中复制它,然后将其粘贴到 Interface Builder 中。

Objective-C

目标-C

If you want to set the button's title in Objective-C, it looks like this:

如果你想在 Objective-C 中设置按钮的标题,它看起来像这样:

self.myButton.titleLabel.text = @"π or \u03c0";

You can type Unicode characters right into your source code using any of the methods I listed above, or you can use a Unicode escape sequence of \ufollowed by four hex digits.

您可以使用我上面列出的任何方法将 Unicode 字符直接输入到您的源代码中,或者您可以使用\u后跟四个十六进制数字的 Unicode 转义序列。

Characters outside of Unicode plane 0 (the Basic Multilingual Plane, or BMP) require more than four hex digits. For them, use \Ufollowed by eight hex digits. Example:

Unicode 平面 0(基本多语言平面,或 BMP)之外的字符需要四个以上的十六进制数字。对于它们,使用\U后跟八个十六进制数字。例子:

self.myButton.titleLabel.text = @" or \U0001f496";

Swift

迅速

If you want to set the button's title in Swift, it looks like this:

如果你想在 Swift 中设置按钮的标题,它看起来像这样:

myButton.titleLabel.text = "π or \u{3c0}"

The Swift unicode escape sequence allows from one to eight hexidecimal digits inside the braces, so it's the same syntax for a character outside the BMP:

Swift unicode 转义序列允许大括号内有 1 到 8 个十六进制数字,因此它与 BMP 之外的字符的语法相同:

myButton.titleLabel.text = " or \u{1f496}"