Contents

UITextView

A scrollable, multiline text region.

Declaration

@MainActor class UITextView

Mentioned in

Overview

A text view displays multiple lines of text and supports editing, custom styles, and rich formatting. Use it when you need to display or edit a body of text, such as the contents of a document.

For rich text, set the attributedText property to provide per-range style information. You can also use font, textColor, and textAlignment to apply a single style across all text in the view.

Manage the keyboard

When someone taps in an editable text view, it becomes the first responder and the system displays the keyboard. Because the keyboard can obscure parts of your interface, reposition any views that would otherwise be hidden. Some system views, like table views, scroll the first responder into view automatically. If the first responder is at the bottom of the scrolling region, you may still need to resize or reposition the scroll view to keep it visible.

Your app is responsible for dismissing the keyboard. Dismiss it in response to a user action, such as tapping a Done button. To dismiss the keyboard, call resignFirstResponder() on the text view that’s currently the first responder. This ends the editing session and hides the keyboard, with your delegate’s consent.

To customize the keyboard, use the properties from the UITextInputTraits protocol, which text views implement. You can set the keyboard type (ASCII, Numbers, URL, Email, and others) and configure text entry behavior like autocapitalization and autocorrection.

Keyboard notifications

When the system shows or hides the keyboard, it posts notifications that include the keyboard’s size and position. Register for these notifications to reposition or resize views as needed:

For more information about these notifications, see UIWindow.

State preservation

If you assign a value to this view’s restorationIdentifier property, the view preserves the following information:

  • The selected range of text.

  • The editing state of the text view, as reported by the isEditable property.

On the next launch, the view restores these properties. If the saved selection range doesn’t apply to the current text, no text is selected.

For design guidance, see Human Interface Guidelines.

Topics

Initializing the text view

Specifying the text content

Responding to text view changes

Configuring appearance attributes

Configuring layout attributes

Formatting special data in text

Managing the editing behavior

Working with the selection

Replacing the system input views

Supporting Find and Replace

Getting the Writing Tools configuration

Accessing TextKit Objects

Customizing viewport layout

Managing attachment view reuse

Supporting state restoration

Structures

Instance Properties

Instance Methods

See Also

Text views