Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

objective c - Unicode in button title in XCode

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Interface Builder

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

  • 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 Viewer Emoji & 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.

Objective-C

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

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 u followed by four hex digits.

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

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

Swift

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

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...