Using the iOS device keyboard

In this recipe, we will learn how to set some of the different virtual keyboard styles.

Getting ready

In this recipe, we will discuss the usage of the UITextBox object and how we can display editable text within it.

How to do it...

In order to see how this can be achieved, we need to modify the ViewObjectsExample application that we created in the previous recipe. Perform the following steps to do so:

  1. Open the ViewObjectsExample.xcodeproj project file.
  2. Open the ViewController.m implementation file from the project navigator.
  3. Next, modify the populateTextBox method, as shown in the highlighted lines of the following code snippet:
    -(void)populateTextBox {
      self.txtTextInput.text = @"This is some sample text";
      self.txtTextInput.keyboardType = 
      UIKeyboardTypeNumbersAndPunctuation;
      self.txtTextInput.returnKeyType = UIReturnKeyDone;
      self.txtTextInput.delegate = self;
    }
  4. Then, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear and display the textbox with our populated sample text. Tap into the TextField control to display our specified keyboard type.

How to do it...

How it works…

In the following code snippet, we update the keyboardType property of the TextField control and then specify UIKeyboardTypeNumbersAndPunctuation as the keyboard type to use.

-(void)populateTextBox {
  self.txtTextInput.text = @"This is some sample text";
  self.txtTextInput.keyboardType = 
  UIKeyboardTypeNumbersAndPunctuation;
  self.txtTextInput.returnKeyType = UIReturnKeyDone;
  self.txtTextInput.delegate = self;
}

The keyboardType property accepts an enumeration type named UIKeyboardType. The following table explains some of these types:

Keyboard type

Description

UIKeyboardTypeDefault

The default keyboard for the current input method

UIKeyboardTypeASCIICapable

Displays standard ASCII characters

UIKeyboardTypeNumbersAndPunctuation

Displays numbers and punctuation keyboard

UIKeyboardTypeURL

Displays a keyboard optimized for URL entry

UIKeyboardTypeNumberPad

Displays a numeric keypad designed for PIN entry

UIKeyboardTypePhonePad

Displays a keypad designed for entering telephone numbers

UIKeyboardTypeNamePhonePad

Displays a keypad designed for entering a person's name or phone number

UIKeyboardTypeEmailAddress

Displays a keyboard optimized for specifying e-mail addresses

UIKeyboardTypeDecimalPad

Displays a keyboard with numbers and a decimal point

UIKeyboardTypeTwitter

Displays a keyboard optimized for twitter text entry, with easy access to the @ and # characters

UIKeyboardTypeAlphabet

This has been depreciated, but uses the keyboard that displays standard ASCII characters

Note

If you would like to find out more information on the UIKeyboardType class, you can refer to the Apple Developer documentation, located at http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html.

See also

  • The Adding and customizing views recipe
  • The Obtaining user input through the use of buttons recipe
  • The Displaying and editing text recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset