iPhone OS Reference Library Apple Developer Connection spyglass button

UITextView Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iPhone OS 2.0 and later.
Companion guide
Declared in
UITextView.h
Related sample code

Overview

The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using a custom font, color, and alignment and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.

This class does not support multiple styles for text. The font, color, and text alignment attributes you specify always apply to the entire contents of the text view. To display more complex styling in your application, you need to use a UIWebView object and render your content using HTML.

Managing the Keyboard

When the user taps in an editable text view, that text view becomes the first responder and automatically asks the system to display the associated keyboard. Because the appearance of the keyboard has the potential to obscure portions of your user interface, it is up to you to make sure that does not happen by repositioning any views that might be obscured. Some system views, like table views, help you by scrolling the first responder into view automatically. If the first responder is at the bottom of the scrolling region, however, you may still need to resize or reposition the scroll view itself to ensure the first responder is visible.

It is your application’s responsibility to dismiss the keyboard at the time of your choosing. You might dismiss the keyboard in response to a specific user action, such as the user tapping a particular button in your user interface. To dismiss the keyboard, send the resignFirstResponder message to the text view that is currently the first responder. Doing so causes the text view object to end the current editing session (with the delegate object’s consent) and hide the keyboard.

The appearance of the keyboard itself can be customized using the properties provided by the UITextInputTraits protocol. Text view objects implement this protocol and support the properties it defines. You can use these properties to specify the type of keyboard (ASCII, Numbers, URL, Email, and others) to display. You can also configure the basic text entry behavior of the keyboard, such as whether it supports automatic capitalization and correction of the text.

Keyboard Notifications

When the system shows or hides the keyboard, it posts several keyboard notifications. These notifications contain information about the keyboard, including its size, which you can use for calculations that involve repositioning or resizing views. Registering for these notifications is the only way to get some types of information about the keyboard. The system delivers the following notifications for keyboard-related events:

For more information about these notifications, see their descriptions in UIWindow Class Reference.

Tasks

Configuring the Text Attributes

Working with the Selection

Accessing the Delegate

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

dataDetectorTypes

The types of data converted to clickable URLs in the text view.

@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
Discussion

You can use this property to specify the types of data (phone numbers, http links, and so on) that should be automatically converted to clickable URLs in the text view. When clicked, the text view opens the application responsible for handling the URL type and passes it the URL.

Availability
Declared In
UITextView.h

delegate

The receiver’s delegate.

@property(nonatomic, assign) id<UITextViewDelegate> delegate
Discussion

A text view delegate responds to editing-related messages from the text view. You can use the delegate to track changes to the text itself and to the current selection.

For information about the methods implemented by the delegate, see UITextViewDelegate Protocol Reference.

Availability
Declared In
UITextView.h

editable

A Boolean value indicating whether the receiver is editable.

@property(nonatomic, getter=isEditable) BOOL editable
Discussion

The default value of this property is YES.

Availability
Related Sample Code
Declared In
UITextView.h

font

The font of the text.

@property(nonatomic, retain) UIFont *font
Discussion

This property applies to the entire text string. The default font is a 17-point Helvetica plain font.

Note: You can get information about the fonts available on the system using the methods of the UIFont class.

Availability
Declared In
UITextView.h

selectedRange

The current selection range of the receiver.

@property(nonatomic) NSRange selectedRange
Discussion

In iPhone OS 2.2 and earlier, the length of the selection range is always 0, indicating that the selection is actually an insertion point. In iPhone OS 3.0 and later, the length of the selection range may be non-zero.

Availability
Declared In
UITextView.h

text

The text displayed by the text view.

@property(nonatomic, copy) NSString *text
Availability
Related Sample Code
Declared In
UITextView.h

textAlignment

The technique to use for aligning the text.

@property(nonatomic) UITextAlignment textAlignment
Discussion

This property applies to the entire text string. The default value of this property is UITextAlignmentLeft.

Availability
Declared In
UITextView.h

textColor

The color of the text.

@property(nonatomic, retain) UIColor *textColor
Discussion

This property applies to the entire text string. The default text color is black.

Availability
See Also
Declared In
UITextView.h

Instance Methods

hasText

Returns a Boolean value indicating whether the text view currently contains any text.

- (BOOL)hasText

Return Value

YES if the receiver contains text or NO if it does not.

Availability
  • Available in iPhone OS 2.0 and later.
Declared In
UITextView.h

scrollRangeToVisible:

Scrolls the receiver until the text in the specified range is visible.

- (void)scrollRangeToVisible:(NSRange)range

Parameters
range

The range of text to scroll into view.

Availability
  • Available in iPhone OS 2.0 and later.
Declared In
UITextView.h

Notifications

UITextViewTextDidBeginEditingNotification

Notifies observers that an editing session began in a text view. The affected view is stored in the object parameter of the notification. The userInfo dictionary is not used.

Availability
Declared In
UITextView.h

UITextViewTextDidChangeNotification

Notifies observers that the text in a text view changed. The affected view is stored in the object parameter of the notification. The userInfo dictionary is not used.

Availability
Declared In
UITextView.h

UITextViewTextDidEndEditingNotification

Notifies observers that the editing session ended for a text view. The affected view is stored in the object parameter of the notification. The userInfo dictionary is not used.

Availability
Declared In
UITextView.h


Last updated: 2009-07-23

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