WHAT YOU WILL LEARN IN THIS CHAPTER
One of the controversial aspects of the iPhone is the multitouch keyboard that allows users to input data into their iPhone. Critics of the iPhone have long lamented its lack of a physical keyboard for data entry, whereas ardent iPhone supporters swear by its ease of use. Although many mobile platforms have long experimented with virtual keyboards, none is as successful as the iPhone in its implementation.
What made the iPhone keyboard so powerful is its intelligence in tracking what you type, followed by suggestions for the word you are typing, and automatically correcting the spelling and inserting punctuation for you. What's more, the keyboard knows when to appear at the right time — it appears when you tap a TextField view, and it goes away automatically when you tap a non-input view. Also, it lets you input data in different languages.
For iPhone application programmers, the key concern is how to integrate the keyboard into your application. How do you make the keyboard go away when it is no longer needed? And how do you ensure that the view the user is currently interacting with is not blocked by the keyboard? In this chapter, you learn about various ways to deal with the keyboard programmatically.
In iPhone programming, the view that is most commonly associated with the keyboard is the TextField view. When a TextField view is tapped (or clicked, if you are using the Simulator), the keyboard is automatically displayed. The data that the user clicks on the keyboard is then inserted into the TextField view.
In the previous section, you saw how the TextField view was used for capturing user inputs. To understand more about the input behaviors, go to Interface Builder, select the TextField view, and view its Attributes Inspector window (choose Tools
The Text Input Traits section contains several items for you to use to configure how the keyboard handles the text entered.
The Capitalize item allows you to capitalize the words, sentences, or all characters of the data entered via the keyboard.
The Correction item lets you indicate whether you want the keyboard to provide suggestions for words that are not spelled correctly. You can also choose the Default option so that it defaults to the user's global text correction settings.
The Keyboard item allows you to choose the different types of keyboard for entering different types of data.
Figure 5-4 shows (from left to right) the keyboard configured with the following Keyboard types: Email Address, Phone Pad, and Number Pad.
The Appearance item lets you choose how the keyboard should appear.
Figure 5-5 shows the keyboard in Default (top) and Alert (bottom) view.
The Return Key item (see Figure 5-6) allows you to show different types of Return key in your keyboard.
Figure 5-7 shows the keyboard set with the Google key serving as the Return key.
Finally, the Auto-Enable Return Key check box indicates that if no input is entered for a field, the Return key will be disabled (grayed out). It will be enabled again if at least one character is entered. The Secure check box indicates if the input will be masked (see Figure 5-8). This is usually used for password input.
In the earlier part of this chapter, you saw that the keyboard in the iPhone automatically appears when you select a TextField view. However, making the keyboard go away requires additional effort on the part of the programmer. In this section, you will see how you can programmatically dismiss the keyboard when you are done with the typing. The first technique is demonstrated in the following Try It Out.
The First Responder in a view always refers to the current view that the user is interacting with. In the example here, when you click the TextField view, it becomes the First Responder and activates the keyboard automatically.
So far, things have been pretty straightforward. You now know that you can dismiss a keyboard by simply making a TextField resign its First-Responder status. Doing so requires that you handle the Did End on Exit
event, which is fired whenever the user clicks the Return key on the keyboard. However, the Return key is shown only when you display it using a non-numeric keyboard type. If you display a numeric keyboard, for example, the Return key is no longer available. In this case, you would have trouble getting the Did End On Exit
event to fire.
Therefore, the following Try It Out teaches another technique for dismissing the keyboard, regardless of the type of keyboard displayed.
Sometimes you might want to straightaway set a TextField view as the active view and display the keyboard without waiting for the user to do so. In such cases, you can use the becomeFirstResponder
method of a view. The following code shows that the TextField view will be the First Responder as soon as the View is loaded:
- (void)viewDidLoad {
[textField becomeFirstResponder];
[super viewDidLoad];
}
Up to this point, you have seen the various ways to hide the keyboard after you are done using it. However, there is one problem that you need to note. When the keyboard appears, it takes up a signification portion of the screen. If your TextField view is located at the bottom of the screen, it would be covered by the keyboard. As a programmer, it is your duty to ensure that the view is relocated to a visible portion of the screen. Surprisingly, this is not taken care of by the SDK; you have to do the hard work yourself.
The current keyboard in the iPhone takes up 216 pixels in height.
Before you learn how to relocate the views on a screen when the keyboard appears, it is important for you to understand a few key concepts related to the keyboard:
You need to be able to programmatically know when a keyboard is visible or hidden. To do so, your application needs to register for the following notifications — UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
.
You also need to know when and which TextField view is currently being edited so that you can relocate it to a visible portion of the screen. You can know these two pieces of information through the two delegate protocols — textFieldDidBeginEditing:
and textFieldDidEndEditing:
— available in the UITextFieldDelegate
delegate.
Confused? Worry not; the following Try It Out makes it all clear.
In this chapter, you learned the various techniques to deal with the keyboard in your iPhone application. In particular, you have seen:
How to make the keyboard go away when you are done with the data entry
How to detect the presence or absence of the keyboard
How to ensure that views are not blocked by the keyboard
EXERCISES
How do you hide the keyboard for a UITextField
object?
How do you detect whether the keyboard is visible or not?
How do you get the size of the keyboard?
WHAT YOU HAVE LEARNED IN THIS CHAPTER
TOPIC | KEY CONCEPTS |
---|---|
Making the keyboard go away | Use the |
Display the different types of keyboard displayed | Modify the keyboard type by changing the Text Input Traits of a |
Handling the return key of the keyboard | You either handle the |
Detecting when the keyboard appears or hides | Register for the two notifications — UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. |
Detecting which | Implement the |
Detecting which | Implement the |