UITextView
A scrollable, multiline text region.
Declaration
@MainActor class UITextViewMentioned in
- Customizing Writing Tools behavior for UIKit views
- Managing viewport layout and attachment reuse in text views
- About app development with UIKit
- Adding tables to attributed strings in UIKit
- Adding Writing Tools support to a custom UIKit view
- Adopting system selection UI in custom text views
- Building a desktop-class iPad app
- Making a view into a drag source
- Making a view into a drop destination
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
fonttextColortextAlignmenttypingAttributeslinkTextAttributesborderStyletextHighlightAttributesdrawTextHighlightBackground(for:origin:)UITextView.BorderStyle
Configuring layout attributes
Formatting special data in text
Managing the editing behavior
isEditableallowsEditingTextAttributestextDidBeginEditingNotificationtextDidChangeNotificationtextDidEndEditingNotification
Working with the selection
Replacing the system input views
Supporting Find and Replace
Getting the Writing Tools configuration
writingToolsBehaviorallowedWritingToolsResultOptionsisWritingToolsActivewritingToolsCoordinatorsubclassForWritingToolsCoordinator
Accessing TextKit Objects
Customizing viewport layout
viewportBounds(for:)textViewportLayoutControllerWillLayout(_:)textViewportLayoutControllerDidLayout(_:)textViewportLayoutControllerReceivedSetNeedsLayout(_:)
Managing attachment view reuse
Supporting state restoration
Structures
UITextView.TextDidBeginEditingMessageUITextView.TextDidChangeMessageUITextView.TextDidEndEditingMessage