Contents

NSTextView

A view that draws text and handles user interactions with that text.

Declaration

class NSTextView

Mentioned in

Overview

The NSTextView class is the front-end class to the AppKit text system. The class draws the text managed by the back-end components and handles user events to select and modify its text, in addition to supporting rich text, attachments, input management, and key binding, and marked text attributes.

NSTextView is the principal means to obtain a text object that caters to almost all needs for displaying and managing text at the user interface level. While NSTextView is a subclass of the NSText class — which declares the most general Cocoa interface to the text system — NSTextView adds major features beyond the capabilities of NSText. You can also do more powerful and more creative text manipulation (such as displaying text in a circle) using NSTextStorage, NSTextLayoutManager, NSTextContainer, and related classes.

You’re more likely to use the NSTextView class than NSText. It’s also important to remember that NSTextView conforms to a large number of protocols, the methods of which are available to instances of the NSTextView class.

NSTextView communicates with its delegate through methods declared both by the NSTextViewDelegate and by its superclass’s protocol, NSTextDelegate. All delegation messages come from the first text view.

In macOS 12 and later, if you explicitly call the layoutManager property on a text view or text container, the framework reverts to a compatibility mode that uses NSLayoutManager. The text view also switches to this compatibility mode when it encounters text content that’s not yet supported, such as NSTextTable.

About Delegate Methods

The NSTextView class communicates with its delegate through methods declared both by the NSTextViewDelegate and by its superclass’s protocol, NSTextDelegate. All delegation messages come from the first text view.

Becoming the first responder

When the system invokes becomeFirstResponder() on a text view, if the previous first responder was not a text view on the same layout manager as the receiving text view, the receiving text view draws the selection and updates the insertion point if necessary.

To make a text view the first responder, call the containing window’s makeFirstResponder(_:) method. Never invoke a text view’s becomeFirstResponder() method directly.

Resigning as first responder

When the system invokes resignFirstResponder() on a text view, if the object that will become the new first responder is a text view attached to the same layout manager as the receiver, the receiving text view returns true with no further action. Otherwise, it sends a textShouldEndEditing(_:) message to its delegate (if any). If the delegate returns false, the text view returns false. If the delegate returns true, the text view hides the selection highlighting and posts an didEndEditingNotification to the default notification center and then returns true.

Topics

Creating a text view

Managing the text view’s content

Registering services information

Accessing text system objects

Setting graphics attributes

Controlling text display

Inserting text

Setting behavioral attributes

Using text formatting controls

Managing the selection

Managing the pasteboard

Setting text attributes

Clicking and pasting

Supporting undo

Customizing subclass behaviors

Working with the spelling checker

Working with the sharing service picker

Supporting the ruler view

Dragging

Speaking text

Working with panels

Performing text completion

Checking and substituting text

Getting the writing tools status

Supporting QuickLook

Changing layout orientation

Using the Find Bar

Constants

Notifications

Interacting with the Touch Bar

Instance Properties

Instance Methods

See Also

Text views