Working with Text, Keyboards, and Buttons in iOS 7 App Development
In the preceding hour, you explored views and view controllers and created a simple application that accepted user input and generated output when a button was pushed. This hour expands on these basic building blocks. In this hour, we create an application that uses multiple different input and output techniques. You learn how to implement and use editable text fields, text views, and graphical buttons, and how to configure the onscreen keyboard.
This is quite a bit of material to cover in an hour, but the concepts are very similar, and you’ll quickly get the hang of these new elements.
Basic User Input and Output
iOS gives us many different ways of displaying information to a user and collecting feedback. There are so many ways, in fact, that we’re going to be spending the next several hours working through the tools that the iOS software development kit (SDK) provides for interacting with your users, starting with the basics.
One of the most common interactions you’ll have with your users is detecting and reacting to the touch of a button (UIButton). Buttons, as you may recall, are elements of a view that respond to an event that the user triggers in the interface, usually a Touch Up Inside event to indicate that the user’s finger was on a button and then released it. Once an event is detected, it can trigger an action (IBAction) within a corresponding view controller.
Buttons are used for everything from providing preset answers to questions to triggering motions within a game. Although the default iOS button style is minimalist, buttons can take on many different forms through the use of images. Figure 7.1 shows an example of a fancy button with gradients.
Figure 7.1 Buttons can be simple, fancy (like this one), or set to any arbitrary image.
Text Fields and Views
Another common input mechanism is a text field. Text fields (UITextField) give users space to enter any information they want into a single line in the application; these are similar to the form fields in a web form. When users enter data into a field, you can constrain their input to numbers or text by using different iOS keyboards, something we do later this hour. You can also enable editing of styles within the text, such as underlining and bold. Text fields, like buttons, can respond to events but are often implemented as passive interface elements, meaning that their contents (provided through the text property) can be read at any time by the view controller.
Similar to the text field is the text view (UITextView). The difference is a text view can present a scrollable and editable block of text for the user to either read or modify. These should be used in cases where more than a few words of input are required. Figure 7.2 shows examples of a text field and text view.
Figure 7.2 Text fields and text views provide a means for entering text using a device’s virtual keyboard.
The final interface feature that we’re going to be using here and throughout this book is the label (UILabel). Labels are used to display strings within a view by setting their text property.
The text within a label can be controlled via a wide range of label attributes, such as font and text size, alignment, and color. As you’ll see, labels are useful both for static text in a view and for presenting dynamic output that you generate in your code.
Now that you have basic insight into the input and output tools we’ll be using in this hour, let’s go ahead and get started with our project: a simple substitution-style story generator.