Unicode in button title in XCode

user1157134 picture user1157134 · Jan 18, 2012 · Viewed 17.9k times · Source

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

Answer

rob mayoff picture rob mayoff · Jan 18, 2012

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}"