iOS Reference Library Apple Developer
Search

Text Objects and Web Views

The text system in iOS provides a tremendous amount of power while still being very simple to use. The UIKit framework includes several high-level classes for managing the display and input of text. This framework also includes a more advanced class for displaying HTML and JavaScript-based content.

The following sections describe the basic support for text and web content in iOS.

Note: This chapter contains information that used to be in iPhone Application Programming Guide. The chapter in this document has not been updated specifically for iOS 4.0.

Text Views

The UIKit framework provides three primary classes for displaying text content:

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.

Figure 1-1  Text classes in the UICatalog application

Text classes in the UICatalog application

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 UITextFieldDelegateprotocol. 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 UITextFieldDelegate and UITextViewDelegate protocols, see UITextFieldDelegate Protocol Reference and UITextViewDelegate Protocol Reference.

Web View

The UIWebView class lets you integrate what is essentially a miniature web browser into your application’s user interface. The UIWebView class makes full use of the same web technologies used to implement Safari in iOS, including full support for HTML, CSS, and JavaScript content. The class also supports many of the built-in gestures that users are familiar with in Safari. For example, you can double-click and pinch to zoom in and out of the page and you can scroll around the page by dragging your finger.

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 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.

Figure 1-2  A web view

A web view

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.




Last updated: 2010-07-07

Did this document help you? Yes It's good, but... Not helpful...