Text Objects and Web Views
The following sections describe the basic support for text and web content in iOS.
The UIKit framework provides three primary classes for displaying text content:
UILabeldisplays static text strings
UITextFielddisplays a single line of editable text
UITextViewdisplays multiple lines of editable text
These classes support the display of arbitrarily large amounts of text, although labels and text fields are typically used for relatively small amounts of text. To make the displayed text easier to read on the smaller screens of iOS-based devices, however, these classes do not support the kinds of advanced formatting you might find in desktop operating systems like Mac OS X. All three classes still allow you to specify the font information, including size and styling options, that you might otherwise want, but the font information you specify is applied to all of the text associated with the object.
Figure 1-1 shows examples of the available text classes as they appear on screen. These examples were taken from the UICatalog sample application, which demonstrates many of the views and controls available in UIKit. The image on the left shows several different styles of text fields while the image on the right shows a single text view. The callouts displayed on the gray background are themselves
UILabel objects embedded inside the table cells used to display the different views. There is also a
UILabel object with the text “Left View” at the bottom of the screen on the left.
When working with editable text views, you should always provide a delegate object to manage the editing session. Text views send several different notifications to the delegate to let them know when editing begins, when it ends, and to give them a chance to override some editing actions. For example, the delegate can decide if the current text contains a valid value and prevent the editing session from ending if it does not. When editing does finally end, you also use the delegate to get the resulting text value and update your application’s data model.
Because there are slight differences in their intended usage, the delegate methods for each text view are slightly different. A delegate that supports the
UITextField class implements the methods of the
UITextFieldDelegate protocol. Similarly, a delegate that supports the
UITextView class implements the methods of the
UITextViewDelegate protocol. In both cases, you are not required to implement any of the protocol methods but if you do not, the text field is not going to be of much use to you.
“Managing Text Fields and Text Views” describes the sequence of delegation messages for both text fields and text views and discusses various tasks performed by the delegates of these objects. For more information about the methods of the
UITextViewDelegate protocols, see UITextFieldDelegate Protocol Reference and UITextViewDelegate Protocol Reference.
UIWebView class lets you integrate what is essentially a miniature web browser into your application’s user interface. The
In addition to displaying content, you can also use a web view object to gather input from the user through the use of web forms. Like the other text classes in UIKit, if you have an editable text field on a form in your web page, tapping that field brings up a keyboard so that the user can enter text. Because it is an integral part of the web experience, the web view itself manages the displaying and dismissing of the keyboard for you.
Figure 1-2 shows an example of a
UIWebView object from the UICatalog sample application, which demonstrates many of the views and controls available in UIKit. Because it just displays HTML content, if you want the user to be able to navigate pages much like they would in a web browser, you need to add controls to do so. For example, the web view in the figure occupies the space below the text field containing the target URL and does not contain the text field itself.
A web view provides information about when pages are loaded, and whether there were any load errors, through its associated delegate object. A web delegate is an object that implements one or more methods of the
UIWebViewDelegateprotocol. Your implementations of the delegate methods can respond to failures or perform other tasks related to the loading of a web page.
“Displaying Web Content” describes how to display HTML and other content in a web view. For more information about the methods of the
UIWebViewDelegate protocol, see UIWebViewDelegate Protocol Reference.
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-03-07)