Class

UIText​View

The UIText​View class implements the behavior for a scrollable, multiline text region. The class supports the display of text using custom style information 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.

Overview

This class supports multiple text styles through use of the attributed​Text property. (Styled text is not supported in versions of iOS earlier than iOS 6.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, text​Color, and text​Alignment properties to set style attributes, but those properties apply to all of the text in the text view. It’s recommended that you use a text view—and not a UIWeb​View object—to display both plain and rich text in your app.

For information about basic view behaviors, see View Programming Guide for iOS.

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 resign​First​Responder() 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 UIText​Input​Traits 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.

State Preservation

In iOS 6 and later, if you assign a value to this view’s restoration​Identifier property, it preserves the following information:

  • The selected range of text, as reported by the selected​Range property.

  • The editing state of the text view, as reported by the is​Editable property.

During the next launch cycle, the view attempts to restore these properties to their saved values. If the selection range cannot be applied to the text in the restored view, no text is selected. For more information about how state preservation and restoration works, see App Programming Guide for iOS.

For more information about appearance and behavior configuration, see Text Views.

Symbols

Initialization

init(frame:​ CGRect, text​Container:​ NSText​Container?)

Creates a new text view with the specified text container.

Configuring the Text Attributes

var text:​ String!

The text displayed by the text view.

var attributed​Text:​ NSAttributed​String!

The styled text displayed by the text view.

var font:​ UIFont?

The font of the text.

var text​Color:​ UIColor?

The color of the text.

var is​Editable:​ Bool

A Boolean value indicating whether the receiver is editable.

var allows​Editing​Text​Attributes:​ Bool

A Boolean value indicating whether the text view allows the user to edit style information.

var data​Detector​Types:​ UIData​Detector​Types

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

var text​Alignment:​ NSText​Alignment

The technique to use for aligning the text.

var typing​Attributes:​ [String :​ Any]

The attributes to apply to new text being entered by the user.

var text​Container​Inset:​ UIEdge​Insets

The inset of the text container's layout area within the text view's content area.

Working with the Selection

var selected​Range:​ NSRange

The current selection range of the receiver.

func scroll​Range​To​Visible(NSRange)

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

var clears​On​Insertion:​ Bool

A Boolean value indicating whether inserting text replaces the previous contents.

var is​Selectable:​ Bool

A Boolean value indicating whether the receiver is selectable.

Accessing the Delegate

Replacing the System Input Views

var input​View:​ UIView?

The custom input view to display when the text view becomes the first responder.

var input​Accessory​View:​ UIView?

The custom accessory view to display when the text view becomes the first responder

Accessing Text Kit Objects

var layout​Manager:​ NSLayout​Manager

The layout manager that lays out text for the receiver’s text container.

var text​Container:​ NSText​Container

The text container object defining the area in which text is displayed in this text view.

var text​Storage:​ NSText​Storage

The text storage object holding the text displayed in this text view.

Notifications

static let UIText​View​Text​Did​Begin​Editing:​ NSNotification.Name

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

static let UIText​View​Text​Did​Change:​ NSNotification.Name

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

static let UIText​View​Text​Did​End​Editing:​ NSNotification.Name

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