10.6. Keyboard Properties

When Steve Jobs introduced the iPhone in one of his most intently anticipated keynote speeches, he expressed a vision for a device that could successfully redefine the user's experience as it saw fit—not just the buttons on an application, but the ability to create an entirely new user interface based on the specific needs of an application. Jobs's hatred for physical buttons clearly included physical keyboards, because Apple has found a use for nearly a dozen different "virtual" keyboard styles on the iPhone, and has provided an elegant interface to define them based on what kind of input is needed.

When a user taps a text field, the runtime automatically raises the keyboard for the user to type. This allows the developer to worry only about displaying text fields instead of managing the keyboard. Apple has elegantly designed its framework so that the text field has all authority over the keyboard's behavior, instead of the keyboard object itself. When a keyboard appears, it automatically adapts itself to match the behavior defined in the text field.

Think of this approach as the driver settings in a car. Each driver has a special way she likes her vehicle configured: seat height, mirrors, and pedal positions. If someone else wanted to drive, you wouldn't think about installing a new driver's seat just to switch drivers. Instead, the car automatically reconfigures the seat for the new driver. Think of the text field as the driver, and the keyboard as the car seat that gets reconfigured.

For preferences tables and other such views consisting of many different text fields, you can define various keyboard behaviors so that each cell will have its own style. All of the properties described in this section are available within most text objects—namely, those that import UITextInputTraits. You can find these traits in UI Kit's UITextInputTraits.h prototype.

10.6.1. Keyboard Style

The UI Kit framework supports eight different keyboard styles. You can assign a different style for each text field.

Set the style using the text object's keyboardType method:

textView.keyboardType = UIKeyboardTypePhonePad;

The following keyboard styles are supported.

Style Description
UIKeyboardTypeDefault Default keyboard: all characters available
UIKeyboardTypeASCIICapable Default keyboard with ASCII support
UIKeyboardTypeNumbersAndPunctuation Standard phone pad, supporting + * # symbols
UIKeyboardTypeURL URL keyboard with .COM button; supports only URI characters
UIKeyboardTypeNumberPad Number pad for numeric entry
UIKeyboardTypePhonePad Phone pad for phone number entry
UIKeyboardTypeNamePhonePad Phone pad also supporting entering name
UIKeyboardTypeEmailAddress Keyboard for email address entry

Both the keyboard and phone pad layouts are the same size, so no additional window changes are needed to switch between the two.

10.6.2. Keyboard Appearance

In addition to the keyboard type, you can also adjust the keyboard's appearance by setting the keyboardAppearance property:

textView.keyboardAppearance = UIKeyboardAppearanceDefault;

The following keyboard appearances are available.

Style Description
UIKeyboardAppearanceDefault Default appearance; light gray
UIKeyboardAppearanceAlert Dark gray/graphite

10.6.3. Return Key

For keyboards with a return key, you can assign the key various styles using the text object's returnKeyType property:

textView.returnKeyType = UIReturnKeyGo;

The following styles are supported.

Style Description
UIReturnKeyDefault Default: gray button labeled Return
UIReturnKeyGo Blue button labeled Go
UIReturnKeyGoogle Blue button labeled Google, used for searches
UIReturnKeyJoin Blue button labeled Join
UIReturnKeyNext Gray button labeled Next
UIReturnKeyRoute Blue button labeled Route
UIReturnKeySearch Blue button labeled Search
UIReturnKeySend Blue button labeled Send
UIReturnKeyYahoo Blue button labeled Yahoo!, used for searches
UIReturnKeyDone Blue button labeled Done
UIReturnKeyEmergencyCall Emergency call button

10.6.4. Autocapitalization

Keyboards can automatically capitalize the first letter of a new line or sentence. To toggle this, set the text object's autocapitalizationType property:

textView.autocapitalizationType = UITextAutocapitalizationTypeNone;

The following autocapitalization types are supported:

  • UITextAutocapitalizationTypeNone

  • UITextAutocapitalizationTypeWords

  • UITextAutocapitalizationTypeSentences

  • UITextAutocapitalizationTypeAllCharacters

10.6.5. Autocorrection

When entering text, the text view and keyboard objects work together to present possible corrections to typing mistakes. This is based on an internal dictionary of commonly mistyped words, combined with a typing cache that keeps track of your own typing. The iPhone generates the dictionary in /private/var/mobile/Library/Keyboard/dynamic-text.dat.

Autocorrection is enabled by default, but you can toggle it using the text view's autocorrectionType property:

textView.autocorrectionType = UITextAutocorrectionTypeDefault ;

The following autocorrection types are available:


10.6.6. Secure Text Entry

When typing passwords or other private data into a text window, the information shouldn't be cached in the iPhone. Turning on secure text entry disables autocorrection and word caching features for the text field. To activate secure text mode, set the secureTextEntry property:

textView.secureTextEntry = YES;