<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextView",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextView"
  },
  "title" : "NSTextView"
}
-->

# NSTextView

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

```
class NSTextView
```

## Overview

The [`NSTextView`](/documentation/AppKit/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.

> Note:
> If you need only to implement a simple editable text field, see ``doc://com.apple.appkit/documentation/AppKit/NSTextField``.

[`NSTextView`](/documentation/AppKit/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`](/documentation/AppKit/NSTextView) is a subclass of the [`NSText`](/documentation/AppKit/NSText) class — which declares the most general Cocoa interface to the text system — [`NSTextView`](/documentation/AppKit/NSTextView) adds major features beyond the capabilities of [`NSText`](/documentation/AppKit/NSText). You can also do more powerful and more creative text manipulation (such as displaying text in a circle) using [`NSTextStorage`](/documentation/AppKit/NSTextStorage), [`NSTextLayoutManager`](/documentation/AppKit/NSTextLayoutManager), [`NSTextContainer`](/documentation/AppKit/NSTextContainer), and related classes.

You’re more likely to use the [`NSTextView`](/documentation/AppKit/NSTextView) class than [`NSText`](/documentation/AppKit/NSText). It’s also important to remember that [`NSTextView`](/documentation/AppKit/NSTextView) conforms to a large number of protocols, the methods of which are available to instances of the [`NSTextView`](/documentation/AppKit/NSTextView) class.

[`NSTextView`](/documentation/AppKit/NSTextView) communicates with its delegate through methods declared both by the [`NSTextViewDelegate`](/documentation/AppKit/NSTextViewDelegate) and by its superclass’s protocol, [`NSTextDelegate`](/documentation/AppKit/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`](/documentation/AppKit/NSLayoutManager). The text view also switches to this compatibility mode when it encounters text content that’s not yet supported, such as [`NSTextTable`](/documentation/AppKit/NSTextTable).

### About Delegate Methods

The `NSTextView` class communicates with its delegate through methods declared both by the [`NSTextViewDelegate`](/documentation/AppKit/NSTextViewDelegate) and by its superclass’s protocol, [`NSTextDelegate`](/documentation/AppKit/NSTextDelegate). All delegation messages come from the first text view.

### Becoming the first responder

When the system invokes [`becomeFirstResponder()`](/documentation/AppKit/NSResponder/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(_:)`](/documentation/AppKit/NSWindow/makeFirstResponder(_:)) method. Never invoke a text view’s [`becomeFirstResponder()`](/documentation/AppKit/NSResponder/becomeFirstResponder()) method directly.

### Resigning as first responder

When the system invokes [`resignFirstResponder()`](/documentation/AppKit/NSResponder/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 <doc://com.apple.documentation/documentation/Swift/true> with no further action. Otherwise, it sends a [`textShouldEndEditing(_:)`](/documentation/AppKit/NSTextDelegate/textShouldEndEditing(_:)) message to its delegate (if any). If the delegate returns <doc://com.apple.documentation/documentation/Swift/false>, the text view returns <doc://com.apple.documentation/documentation/Swift/false>. If the delegate returns <doc://com.apple.documentation/documentation/Swift/true>, the text view hides the selection highlighting and posts an [`didEndEditingNotification`](/documentation/AppKit/NSText/didEndEditingNotification) to the default notification center and then returns <doc://com.apple.documentation/documentation/Swift/true>.

## Topics

### Creating a text view

[`init(frame: NSRect, textContainer: NSTextContainer?)`](/documentation/AppKit/NSTextView/init(frame:textContainer:))

Initializes a text view.

[`init(frame: NSRect)`](/documentation/AppKit/NSTextView/init(frame:))

Initializes a text view.

[`convenience init(usingTextLayoutManager: Bool)`](/documentation/AppKit/NSTextView/init(usingTextLayoutManager:))

[`+ (instancetype) textViewUsingTextLayoutManager:(BOOL) usingTextLayoutManager;`](/documentation/AppKit/NSTextView/textViewUsingTextLayoutManager:)

[`init?(coder: NSCoder)`](/documentation/AppKit/NSTextView/init(coder:))

Initializes a text view with data in an unarchiver.

### Managing the text view’s content

[`var delegate: (any NSTextViewDelegate)?`](/documentation/AppKit/NSTextView/delegate)

The delegate for all text views sharing the receiver’s layout manager.

[`protocol NSTextViewDelegate`](/documentation/AppKit/NSTextViewDelegate)

A set of optional methods that text view delegates can use to manage selection, set text attributes, work with the spell checker, and more.

### Registering services information

[`class func registerForServices()`](/documentation/AppKit/NSTextView/registerForServices())

Registers send and return types for the Services facility.

### Accessing text system objects

[`class var stronglyReferencesTextStorage: Bool`](/documentation/AppKit/NSTextView/stronglyReferencesTextStorage)

A Boolean value that indicates whether instances of the class operate in the object ownership policy.

[`class func fieldEditor() -> Self`](/documentation/AppKit/NSTextView/fieldEditor())

[`var textContainer: NSTextContainer?`](/documentation/AppKit/NSTextView/textContainer)

The receiver’s text container.

[`func replaceTextContainer(NSTextContainer)`](/documentation/AppKit/NSTextView/replaceTextContainer(_:))

Replaces the text container for the group of text system objects containing the receiver, keeping the association between the receiver and its layout manager intact.

[`var textContainerInset: NSSize`](/documentation/AppKit/NSTextView/textContainerInset)

The empty space the receiver leaves around its associated text container.

[`var textContainerOrigin: NSPoint`](/documentation/AppKit/NSTextView/textContainerOrigin)

The origin of the receiver’s text container.

[`func invalidateTextContainerOrigin()`](/documentation/AppKit/NSTextView/invalidateTextContainerOrigin())

Invalidates the calculated origin of the text container.

[`var textLayoutManager: NSTextLayoutManager?`](/documentation/AppKit/NSTextView/textLayoutManager)

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

[`var layoutManager: NSLayoutManager?`](/documentation/AppKit/NSTextView/layoutManager)

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

[`var textContentStorage: NSTextContentStorage?`](/documentation/AppKit/NSTextView/textContentStorage)

The receiver’s text storage object.

[`var textStorage: NSTextStorage?`](/documentation/AppKit/NSTextView/textStorage)

The receiver’s text storage object.

### Setting graphics attributes

[`var backgroundColor: NSColor`](/documentation/AppKit/NSTextView/backgroundColor)

The receiver’s background color.

[`var drawsBackground: Bool`](/documentation/AppKit/NSTextView/drawsBackground)

A Boolean value that indicates whether the receiver draws its background.

[`var allowsDocumentBackgroundColorChange: Bool`](/documentation/AppKit/NSTextView/allowsDocumentBackgroundColorChange)

A Boolean value that indicates whether the receiver allows its background color to change.

[`func changeDocumentBackgroundColor(Any?)`](/documentation/AppKit/NSTextView/changeDocumentBackgroundColor(_:))

An action method used to set the background color.

### Controlling text display

[`func setNeedsDisplay(NSRect, avoidAdditionalLayout: Bool)`](/documentation/AppKit/NSTextView/setNeedsDisplay(_:avoidAdditionalLayout:))

Marks the receiver as requiring display.

[`var shouldDrawInsertionPoint: Bool`](/documentation/AppKit/NSTextView/shouldDrawInsertionPoint)

A Boolean value that determines whether the receiver should draw its insertion point.

[`func drawInsertionPoint(in: NSRect, color: NSColor, turnedOn: Bool)`](/documentation/AppKit/NSTextView/drawInsertionPoint(in:color:turnedOn:))

Draws or erases the insertion point.

[`func drawBackground(in: NSRect)`](/documentation/AppKit/NSTextView/drawBackground(in:))

Draws the background of the text view.

[`func setConstrainedFrameSize(NSSize)`](/documentation/AppKit/NSTextView/setConstrainedFrameSize(_:))

Attempts to set the frame size as if by user action.

[`func cleanUpAfterDragOperation()`](/documentation/AppKit/NSTextView/cleanUpAfterDragOperation())

Releases the drag information still existing after the dragging session has completed.

[`func showFindIndicator(for: NSRange)`](/documentation/AppKit/NSTextView/showFindIndicator(for:))

Causes a temporary highlighting effect to appear around the visible portion (or portions) of the specified range.

[`class func scrollableDocumentContentTextView() -> NSScrollView`](/documentation/AppKit/NSTextView/scrollableDocumentContentTextView())

[`class func scrollablePlainDocumentContentTextView() -> NSScrollView`](/documentation/AppKit/NSTextView/scrollablePlainDocumentContentTextView())

[`class func scrollableTextView() -> NSScrollView`](/documentation/AppKit/NSTextView/scrollableTextView())

### Inserting text

[`var allowedInputSourceLocales: [String]?`](/documentation/AppKit/NSTextView/allowedInputSourceLocales)

An array of locale identifiers representing input sources that are allowed to be enabled when the receiver has the keyboard focus.

[`func insertText(Any)`](/documentation/AppKit/NSTextView/insertText(_:))

Inserts `aString` into the receiver’s text at the insertion point if there is one, otherwise replacing the selection.

### Setting behavioral attributes

[`var allowsUndo: Bool`](/documentation/AppKit/NSTextView/allowsUndo)

A Boolean value that indicates whether the receiver allows undo.

[`var isEditable: Bool`](/documentation/AppKit/NSTextView/isEditable)

A Boolean value that controls whether the text views sharing the receiver’s layout manager allow the user to edit text.

[`var isSelectable: Bool`](/documentation/AppKit/NSTextView/isSelectable)

A Boolean value that controls whether the text views sharing the receiver’s layout manager allow the user to select text.

[`var isFieldEditor: Bool`](/documentation/AppKit/NSTextView/isFieldEditor)

A Boolean value that controls whether the text views sharing the receiver’s layout manager behave as field editors.

[`var isRichText: Bool`](/documentation/AppKit/NSTextView/isRichText)

A Boolean value that controls whether the text views sharing the receiver’s layout manager allow the user to apply attributes to specific ranges of text.

[`var importsGraphics: Bool`](/documentation/AppKit/NSTextView/importsGraphics)

A Boolean value that controls whether the text views sharing the receiver’s layout manager allow the user to import files by dragging.

[`func setBaseWritingDirection(NSWritingDirection, range: NSRange)`](/documentation/AppKit/NSTextView/setBaseWritingDirection(_:range:))

Sets the base writing direction of a range of text.

[`- (void) toggleBaseWritingDirection:(id) sender;`](/documentation/AppKit/NSTextView/toggleBaseWritingDirection:)

Changes the base writing direction of a paragraph between left-to-right and right-to-left.

[`var defaultParagraphStyle: NSParagraphStyle?`](/documentation/AppKit/NSTextView/defaultParagraphStyle)

The receiver’s default paragraph style.

[`func outline(Any?)`](/documentation/AppKit/NSTextView/outline(_:))

Adds the outline attribute to the selected text attributes if absent; removes the attribute if present.

[`var allowsImageEditing: Bool`](/documentation/AppKit/NSTextView/allowsImageEditing)

Indicates whether image attachments should permit editing of their images.

[`var isAutomaticQuoteSubstitutionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticQuoteSubstitutionEnabled)

A Boolean value that enables and disables automatic quotation mark substitution.

[`func toggleAutomaticQuoteSubstitution(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticQuoteSubstitution(_:))

Changes the state of automatic quotation mark substitution from enabled to disabled and vice versa.

[`var isAutomaticLinkDetectionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticLinkDetectionEnabled)

A Boolean value that enables or disables automatic link detection.

[`func toggleAutomaticLinkDetection(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticLinkDetection(_:))

Changes the state of automatic link detection from enabled to disabled and vice versa.

[`var displaysLinkToolTips: Bool`](/documentation/AppKit/NSTextView/displaysLinkToolTips)

A Boolean value that indicates whether the text view automatically supplies the destination of a link as a tooltip for text that has a link attribute.

[`var isAutomaticTextCompletionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticTextCompletionEnabled)

A Boolean value that indicates whether the text view supplies autocompletion suggestions as the user types.

[`func toggleAutomaticTextCompletion(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticTextCompletion(_:))

[`var usesAdaptiveColorMappingForDarkAppearance: Bool`](/documentation/AppKit/NSTextView/usesAdaptiveColorMappingForDarkAppearance)

A Boolean value that indicates whether the framework should use adaptive color mapping for dark appearance.

[`var usesRolloverButtonForSelection: Bool`](/documentation/AppKit/NSTextView/usesRolloverButtonForSelection)

### Using text formatting controls

[`var usesRuler: Bool`](/documentation/AppKit/NSTextView/usesRuler)

A Boolean value that controls whether the text views sharing the receiver’s layout manager use a ruler.

[`var isRulerVisible: Bool`](/documentation/AppKit/NSTextView/isRulerVisible)

A Boolean value that controls whether the scroll view enclosing text views sharing the receiver’s layout manager displays the ruler.

[`var usesInspectorBar: Bool`](/documentation/AppKit/NSTextView/usesInspectorBar)

A Boolean value that indicates whether this text view uses the inspector bar.

### Managing the selection

[`var selectedRanges: [NSValue]`](/documentation/AppKit/NSTextView/selectedRanges)

An array containing the ranges of characters selected in the receiver’s layout manager.

[`func setSelectedRange(NSRange)`](/documentation/AppKit/NSTextView/setSelectedRange(_:))

Selects the specified range of characters in response to user action.

[`func setSelectedRange(NSRange, affinity: NSSelectionAffinity, stillSelecting: Bool)`](/documentation/AppKit/NSTextView/setSelectedRange(_:affinity:stillSelecting:))

Sets the selection to a range of characters in response to user action.

[`func setSelectedRanges([NSValue], affinity: NSSelectionAffinity, stillSelecting: Bool)`](/documentation/AppKit/NSTextView/setSelectedRanges(_:affinity:stillSelecting:))

Sets the selection to the characters in an array of ranges in response to user action.

[`var selectionAffinity: NSSelectionAffinity`](/documentation/AppKit/NSTextView/selectionAffinity)

The preferred direction of selection.

[`var selectionGranularity: NSSelectionGranularity`](/documentation/AppKit/NSTextView/selectionGranularity)

The selection granularity for subsequent extension of a selection.

[`var insertionPointColor: NSColor!`](/documentation/AppKit/NSTextView/insertionPointColor)

The color of the insertion point.

[`func updateInsertionPointStateAndRestartTimer(Bool)`](/documentation/AppKit/NSTextView/updateInsertionPointStateAndRestartTimer(_:))

Updates the insertion point’s location and optionally restarts the blinking cursor timer.

[`var selectedTextAttributes: [NSAttributedString.Key : Any]`](/documentation/AppKit/NSTextView/selectedTextAttributes)

The attributes used to indicate the selection.

[`var markedTextAttributes: [NSAttributedString.Key : Any]?`](/documentation/AppKit/NSTextView/markedTextAttributes)

The attributes used to draw marked text.

[`var linkTextAttributes: [NSAttributedString.Key : Any]?`](/documentation/AppKit/NSTextView/linkTextAttributes)

The attributes used to draw the onscreen presentation of link text.

[`func characterIndexForInsertion(at: NSPoint) -> Int`](/documentation/AppKit/NSTextView/characterIndexForInsertion(at:))

Returns a character index appropriate for placing a zero-length selection for an insertion point associated with the mouse at the given point.

[`func updateCandidates()`](/documentation/AppKit/NSTextView/updateCandidates())

### Managing the pasteboard

[`func preferredPasteboardType(from: [NSPasteboard.PasteboardType], restrictedToTypesFrom: [NSPasteboard.PasteboardType]?) -> NSPasteboard.PasteboardType?`](/documentation/AppKit/NSTextView/preferredPasteboardType(from:restrictedToTypesFrom:))

Returns whatever type on the pasteboard would be most preferred for copying data.

[`func readSelection(from: NSPasteboard) -> Bool`](/documentation/AppKit/NSTextView/readSelection(from:))

Reads the text view’s preferred type of data from the specified pasteboard.

[`func readSelection(from: NSPasteboard, type: NSPasteboard.PasteboardType) -> Bool`](/documentation/AppKit/NSTextView/readSelection(from:type:))

Reads data of the given type from the specified pasteboard.

[`var readablePasteboardTypes: [NSPasteboard.PasteboardType]`](/documentation/AppKit/NSTextView/readablePasteboardTypes)

The types this text view can read immediately from the pasteboard.

[`var writablePasteboardTypes: [NSPasteboard.PasteboardType]`](/documentation/AppKit/NSTextView/writablePasteboardTypes)

The pasteboard types that can be provided from the current selection.

[`func writeSelection(to: NSPasteboard, type: NSPasteboard.PasteboardType) -> Bool`](/documentation/AppKit/NSTextView/writeSelection(to:type:))

Writes the current selection to the specified pasteboard using the given type.

[`func writeSelection(to: NSPasteboard, types: [NSPasteboard.PasteboardType]) -> Bool`](/documentation/AppKit/NSTextView/writeSelection(to:types:))

Writes the current selection to the specified pasteboard under each given type.

[`func validRequestor(forSendType: NSPasteboard.PasteboardType?, returnType: NSPasteboard.PasteboardType?) -> Any?`](/documentation/AppKit/NSTextView/validRequestor(forSendType:returnType:))

Returns `self` if the text view can provide and accept the specified data types, or `nil` if it can’t.

### Setting text attributes

[`func alignJustified(Any?)`](/documentation/AppKit/NSTextView/alignJustified(_:))

Applies full justification to selected paragraphs (or all text, if the receiver is a plain text object).

[`func changeAttributes(Any?)`](/documentation/AppKit/NSTextView/changeAttributes(_:))

Changes the attributes of the current selection.

[`func changeColor(Any?)`](/documentation/AppKit/NSTextView/changeColor(_:))

Sets the color of the selected text.

[`func setAlignment(NSTextAlignment, range: NSRange)`](/documentation/AppKit/NSTextView/setAlignment(_:range:))

Sets the alignment of the paragraphs containing characters in the specified range.

[`var typingAttributes: [NSAttributedString.Key : Any]`](/documentation/AppKit/NSTextView/typingAttributes)

The receiver’s typing attributes.

[`func useStandardKerning(Any?)`](/documentation/AppKit/NSTextView/useStandardKerning(_:))

Set the receiver to use pair kerning data for the glyphs in its selection, or for all glyphs if the receiver is a plain text view.

[`func lowerBaseline(Any?)`](/documentation/AppKit/NSTextView/lowerBaseline(_:))

Lowers the baseline offset of selected text by 1 point, or of all text if the receiver is a plain text view.

[`func raiseBaseline(Any?)`](/documentation/AppKit/NSTextView/raiseBaseline(_:))

Raises the baseline offset of selected text by 1 point, or of all text if the receiver is a plain text view.

[`func turnOffKerning(Any?)`](/documentation/AppKit/NSTextView/turnOffKerning(_:))

Sets the receiver to use nominal glyph spacing for the glyphs in its selection, or for all glyphs if the receiver is a plain text view.

[`func loosenKerning(Any?)`](/documentation/AppKit/NSTextView/loosenKerning(_:))

Increases the space between glyphs in the receiver’s selection, or in all text if the receiver is a plain text view.

[`func tightenKerning(Any?)`](/documentation/AppKit/NSTextView/tightenKerning(_:))

Decreases the space between glyphs in the receiver’s selection, or for all glyphs if the receiver is a plain text view.

[`func useStandardLigatures(Any?)`](/documentation/AppKit/NSTextView/useStandardLigatures(_:))

Sets the receiver to use the standard ligatures available for the fonts and languages used when setting text, for the glyphs in the selection if the receiver is a rich text view, or for all glyphs if it’s a plain text view.

[`func turnOffLigatures(Any?)`](/documentation/AppKit/NSTextView/turnOffLigatures(_:))

Sets the receiver to use only required ligatures when setting text, for the glyphs in the selection if the receiver is a rich text view, or for all glyphs if it’s a plain text view.

[`func useAllLigatures(Any?)`](/documentation/AppKit/NSTextView/useAllLigatures(_:))

Sets the receiver to use all ligatures available for the fonts and languages used when setting text, for the glyphs in the selection if the receiver is a rich text view, or for all glyphs if it’s a plain text view.

[`func toggleTraditionalCharacterShape(Any?)`](/documentation/AppKit/NSTextView/toggleTraditionalCharacterShape(_:))

Toggles the `NSCharacterShapeAttributeName` attribute at the current selection.

### Clicking and pasting

[`func clicked(onLink: Any, at: Int)`](/documentation/AppKit/NSTextView/clicked(onLink:at:))

Causes the text view to act as if the user clicked on some text with the given link as the value of a link attribute associated with the text.

[`func pasteAsPlainText(Any?)`](/documentation/AppKit/NSTextView/pasteAsPlainText(_:))

Inserts the contents of the pasteboard into the receiver’s text as plain text.

[`func pasteAsRichText(Any?)`](/documentation/AppKit/NSTextView/pasteAsRichText(_:))

This action method inserts the contents of the pasteboard into the receiver’s text as rich text, maintaining its attributes.

### Supporting undo

[`func breakUndoCoalescing()`](/documentation/AppKit/NSTextView/breakUndoCoalescing())

Informs the receiver that it should begin coalescing successive typing operations in a new undo grouping.

[`var isCoalescingUndo: Bool`](/documentation/AppKit/NSTextView/isCoalescingUndo)

A Boolean value that indicates whether undo coalescing is in progress.

### Customizing subclass behaviors

[`func updateFontPanel()`](/documentation/AppKit/NSTextView/updateFontPanel())

Updates the Font panel to contain the font attributes of the selection.

[`func updateRuler()`](/documentation/AppKit/NSTextView/updateRuler())

Updates the ruler view in the receiver’s enclosing scroll view to reflect the selection’s paragraph and marker attributes.

[`var acceptableDragTypes: [NSPasteboard.PasteboardType]`](/documentation/AppKit/NSTextView/acceptableDragTypes)

The data types that the receiver accepts as the destination view of a dragging operation.

[`func updateDragTypeRegistration()`](/documentation/AppKit/NSTextView/updateDragTypeRegistration())

Updates the acceptable drag types of all text views associated with the receiver’s layout manager.

[`func selectionRange(forProposedRange: NSRange, granularity: NSSelectionGranularity) -> NSRange`](/documentation/AppKit/NSTextView/selectionRange(forProposedRange:granularity:))

Returns an adjusted selected range based on the selection granularity.

[`var rangeForUserCharacterAttributeChange: NSRange`](/documentation/AppKit/NSTextView/rangeForUserCharacterAttributeChange)

The range of characters affected by an action method that changes character (not paragraph) attributes.

[`var rangesForUserCharacterAttributeChange: [NSValue]?`](/documentation/AppKit/NSTextView/rangesForUserCharacterAttributeChange)

An array containing the ranges of characters affected by an action method that changes character (not paragraph) attributes.

[`var rangeForUserParagraphAttributeChange: NSRange`](/documentation/AppKit/NSTextView/rangeForUserParagraphAttributeChange)

The range of characters affected by an action method that changes paragraph (not character) attributes.

[`var rangesForUserParagraphAttributeChange: [NSValue]?`](/documentation/AppKit/NSTextView/rangesForUserParagraphAttributeChange)

An array containing the ranges of characters affected by a method that changes paragraph (not character) attributes.

[`var rangeForUserTextChange: NSRange`](/documentation/AppKit/NSTextView/rangeForUserTextChange)

The range of characters affected by a method that changes characters (as opposed to attributes).

[`var rangesForUserTextChange: [NSValue]?`](/documentation/AppKit/NSTextView/rangesForUserTextChange)

An array containing the ranges of characters affected by a method that changes characters (as opposed to attributes).

[`func shouldChangeText(in: NSRange, replacementString: String?) -> Bool`](/documentation/AppKit/NSTextView/shouldChangeText(in:replacementString:))

Initiates a series of delegate messages (and general notifications) to determine whether modifications can be made to the characters and attributes of the receiver’s text.

[`func shouldChangeText(inRanges: [NSValue], replacementStrings: [String]?) -> Bool`](/documentation/AppKit/NSTextView/shouldChangeText(inRanges:replacementStrings:))

Initiates a series of delegate messages (and general notifications) to determine whether modifications can be made to the characters and attributes of the receiver’s text.

[`func didChangeText()`](/documentation/AppKit/NSTextView/didChangeText())

Sends out necessary notifications when a text change completes.

[`var smartInsertDeleteEnabled: Bool`](/documentation/AppKit/NSTextView/smartInsertDeleteEnabled)

A Boolean value that controls whether the receiver inserts or deletes space around selected words so as to preserve proper spacing and punctuation.

[`func smartDeleteRange(forProposedRange: NSRange) -> NSRange`](/documentation/AppKit/NSTextView/smartDeleteRange(forProposedRange:))

Returns an extended range that includes adjacent whitespace that should be deleted along with the proposed range in order to preserve proper spacing and punctuation.

[`func smartInsert(afterStringFor: String, replacing: NSRange) -> String?`](/documentation/AppKit/NSTextView/smartInsert(afterStringFor:replacing:))

Returns any whitespace that needs to be added after the string to preserve proper spacing and punctuation when the string replaces the characters in the specified range.

[`func smartInsert(beforeStringFor: String, replacing: NSRange) -> String?`](/documentation/AppKit/NSTextView/smartInsert(beforeStringFor:replacing:))

Returns any whitespace that needs to be added before the string to preserve proper spacing and punctuation when the string replaces the characters in the specified range.

[`func smartInsert(for: String, replacing: NSRange, before: AutoreleasingUnsafeMutablePointer<NSString?>?, after: AutoreleasingUnsafeMutablePointer<NSString?>?)`](/documentation/AppKit/NSTextView/smartInsert(for:replacing:before:after:))

Determines whether whitespace needs to be added around the string to preserve proper spacing and punctuation when it replaces the characters in the specified range.

[`func toggleSmartInsertDelete(Any?)`](/documentation/AppKit/NSTextView/toggleSmartInsertDelete(_:))

Changes the state of smart insert and delete from enabled to disabled and vice versa.

### Working with the spelling checker

[`var isContinuousSpellCheckingEnabled: Bool`](/documentation/AppKit/NSTextView/isContinuousSpellCheckingEnabled)

A Boolean value that indicates whether the receiver has continuous spell checking enabled.

[`var spellCheckerDocumentTag: Int`](/documentation/AppKit/NSTextView/spellCheckerDocumentTag)

A tag identifying the text view’s text as a document for the spell checker server.

[`func toggleContinuousSpellChecking(Any?)`](/documentation/AppKit/NSTextView/toggleContinuousSpellChecking(_:))

Toggles whether continuous spell checking is enabled for the receiver.

[`var isGrammarCheckingEnabled: Bool`](/documentation/AppKit/NSTextView/isGrammarCheckingEnabled)

Enables and disables grammar checking.

[`func toggleGrammarChecking(Any?)`](/documentation/AppKit/NSTextView/toggleGrammarChecking(_:))

Changes the state of grammar checking from enabled to disabled and vice versa.

[`func setSpellingState(Int, range: NSRange)`](/documentation/AppKit/NSTextView/setSpellingState(_:range:))

Sets the spelling state, which controls the display of the spelling and grammar indicators on the given text range.

### Working with the sharing service picker

[`func orderFrontSharingServicePicker(Any?)`](/documentation/AppKit/NSTextView/orderFrontSharingServicePicker(_:))

Creates and displays a new instance of the sharing service picker.

### Supporting the ruler view

[`func rulerView(NSRulerView, didMove: NSRulerMarker)`](/documentation/AppKit/NSTextView/rulerView(_:didMove:))

Modifies the paragraph style of the paragraphs containing the selection to record the new location of the marker.

[`func rulerView(NSRulerView, willMove: NSRulerMarker, toLocation: CGFloat) -> CGFloat`](/documentation/AppKit/NSTextView/rulerView(_:willMove:toLocation:))

Returns a potentially modified location to which the marker should be moved.

[`func rulerView(NSRulerView, shouldMove: NSRulerMarker) -> Bool`](/documentation/AppKit/NSTextView/rulerView(_:shouldMove:))

Returns whether the marker should be moved.

[`func rulerView(NSRulerView, didRemove: NSRulerMarker)`](/documentation/AppKit/NSTextView/rulerView(_:didRemove:))

Modifies the paragraph style of the paragraphs containing the selection—if possible—by removing the specified marker.

[`func rulerView(NSRulerView, shouldRemove: NSRulerMarker) -> Bool`](/documentation/AppKit/NSTextView/rulerView(_:shouldRemove:))

Returns whether the marker should be removed.

[`func rulerView(NSRulerView, didAdd: NSRulerMarker)`](/documentation/AppKit/NSTextView/rulerView(_:didAdd:))

Modifies the paragraph style of the paragraphs containing the selection to accommodate a new marker.

[`func rulerView(NSRulerView, shouldAdd: NSRulerMarker) -> Bool`](/documentation/AppKit/NSTextView/rulerView(_:shouldAdd:))

Returns whether a new marker can be added.

[`func rulerView(NSRulerView, willAdd: NSRulerMarker, atLocation: CGFloat) -> CGFloat`](/documentation/AppKit/NSTextView/rulerView(_:willAdd:atLocation:))

Returns a potentially modified location to which the marker should be added.

[`func rulerView(NSRulerView, handleMouseDownWith: NSEvent)`](/documentation/AppKit/NSTextView/rulerView(_:handleMouseDownWith:))

Adds a left tab marker to the ruler at the location clicked.

### Dragging

[`func dragImageForSelection(with: NSEvent, origin: NSPointPointer?) -> NSImage?`](/documentation/AppKit/NSTextView/dragImageForSelection(with:origin:))

Returns an appropriate drag image for the drag initiated by the specified event.

[`func dragOperation(for: any NSDraggingInfo, type: NSPasteboard.PasteboardType) -> NSDragOperation`](/documentation/AppKit/NSTextView/dragOperation(for:type:))

Returns the type of drag operation that should be performed if the image were released now.

[`func dragSelection(with: NSEvent, offset: NSSize, slideBack: Bool) -> Bool`](/documentation/AppKit/NSTextView/dragSelection(with:offset:slideBack:))

Begins dragging the current selected text range.

[`var acceptsGlyphInfo: Bool`](/documentation/AppKit/NSTextView/acceptsGlyphInfo)

A Boolean value that indicates whether the receiver accepts the glyph info attribute.

### Speaking text

[`func startSpeaking(Any?)`](/documentation/AppKit/NSTextView/startSpeaking(_:))

Speaks the selected text, or all text if no selection.

[`func stopSpeaking(Any?)`](/documentation/AppKit/NSTextView/stopSpeaking(_:))

Stops the speaking of text.

### Working with panels

[`var usesFontPanel: Bool`](/documentation/AppKit/NSTextView/usesFontPanel)

A Boolean value that controls whether the text views sharing the receiver’s layout manager use the Font panel and Font menu.

[`var usesFindPanel: Bool`](/documentation/AppKit/NSTextView/usesFindPanel)

A Boolean value that indicates whether the receiver allows for a find panel.

[`func performFindPanelAction(Any?)`](/documentation/AppKit/NSTextView/performFindPanelAction(_:))

Performs a find panel action specified by the sender’s tag.

[`func orderFrontLinkPanel(Any?)`](/documentation/AppKit/NSTextView/orderFrontLinkPanel(_:))

Brings forward a panel allowing the user to manipulate links in the text view.

[`func orderFrontListPanel(Any?)`](/documentation/AppKit/NSTextView/orderFrontListPanel(_:))

Brings forward a panel allowing the user to manipulate text lists in the text view.

[`func orderFrontSpacingPanel(Any?)`](/documentation/AppKit/NSTextView/orderFrontSpacingPanel(_:))

Brings forward a panel allowing the user to manipulate text line heights, interline spacing, and paragraph spacing, in the text view.

[`func orderFrontTablePanel(Any?)`](/documentation/AppKit/NSTextView/orderFrontTablePanel(_:))

Brings forward a panel allowing the user to manipulate text tables in the text view.

[`func orderFrontSubstitutionsPanel(Any?)`](/documentation/AppKit/NSTextView/orderFrontSubstitutionsPanel(_:))

Brings forward a panel allowing the user to specify string substitutions in the text view.

### Performing text completion

[`func complete(Any?)`](/documentation/AppKit/NSTextView/complete(_:))

Invokes completion in a text view.

[`func completions(forPartialWordRange: NSRange, indexOfSelectedItem: UnsafeMutablePointer<Int>) -> [String]?`](/documentation/AppKit/NSTextView/completions(forPartialWordRange:indexOfSelectedItem:))

Returns an array of potential completions, in the order to be presented, representing possible word completions available from a partial word.

[`func insertCompletion(String, forPartialWordRange: NSRange, movement: Int, isFinal: Bool)`](/documentation/AppKit/NSTextView/insertCompletion(_:forPartialWordRange:movement:isFinal:))

Inserts the selected completion into the text at the appropriate location.

[`var rangeForUserCompletion: NSRange`](/documentation/AppKit/NSTextView/rangeForUserCompletion)

The partial range from the most recent beginning of a word up to the insertion point.

### Checking and substituting text

[`func checkTextInDocument(Any?)`](/documentation/AppKit/NSTextView/checkTextInDocument(_:))

Performs the default text checking on the entire document.

[`func checkTextInSelection(Any?)`](/documentation/AppKit/NSTextView/checkTextInSelection(_:))

Performs the default text checking on the current selection.

[`func checkText(in: NSRange, types: NSTextCheckingTypes, options: [NSSpellChecker.OptionKey : Any])`](/documentation/AppKit/NSTextView/checkText(in:types:options:))

Check and replace the text in the range using the specified checking types and options.

[`func handleTextCheckingResults([NSTextCheckingResult], forRange: NSRange, types: NSTextCheckingTypes, options: [NSSpellChecker.OptionKey : Any], orthography: NSOrthography, wordCount: Int)`](/documentation/AppKit/NSTextView/handleTextCheckingResults(_:forRange:types:options:orthography:wordCount:))

Handles the text checking results returned by the text view

[`var enabledTextCheckingTypes: NSTextCheckingTypes`](/documentation/AppKit/NSTextView/enabledTextCheckingTypes)

The default text checking types.

[`var isAutomaticDashSubstitutionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticDashSubstitutionEnabled)

A Boolean value that indicates whether automatic dash substitution is enabled.

[`func toggleAutomaticDashSubstitution(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticDashSubstitution(_:))

Toggles the state of the automatic dash substitution.

[`var isAutomaticDataDetectionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticDataDetectionEnabled)

A Boolean value that indicates whether automatic data detection is enabled.

[`func toggleAutomaticDataDetection(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticDataDetection(_:))

Toggles the state of the automatic data detection.

[`var isAutomaticSpellingCorrectionEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticSpellingCorrectionEnabled)

A Boolean value that indicates whether automatic spelling correction is enabled.

[`func toggleAutomaticSpellingCorrection(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticSpellingCorrection(_:))

Toggles the state of the automatic spelling correction.

[`var isAutomaticTextReplacementEnabled: Bool`](/documentation/AppKit/NSTextView/isAutomaticTextReplacementEnabled)

A Boolean value that indicates whether automatic text replacement is enabled.

[`func toggleAutomaticTextReplacement(Any?)`](/documentation/AppKit/NSTextView/toggleAutomaticTextReplacement(_:))

Toggles the state of the automatic text replacement.

[`func performValidatedReplacement(in: NSRange, with: NSAttributedString) -> Bool`](/documentation/AppKit/NSTextView/performValidatedReplacement(in:with:))

Replaces text in the range you specify with the attributed string you provide.

### Getting the writing tools status

[`var isWritingToolsActive: Bool`](/documentation/AppKit/NSTextView/isWritingToolsActive)

### Supporting QuickLook

[`func updateQuickLookPreviewPanel()`](/documentation/AppKit/NSTextView/updateQuickLookPreviewPanel())

Notifies the QuickLook panel that an update may be required.

[`func toggleQuickLookPreviewPanel(Any?)`](/documentation/AppKit/NSTextView/toggleQuickLookPreviewPanel(_:))

An action message that toggles the visibility state of the Quick Look preview panel.

[`func quickLookPreviewableItems(inRanges: [NSValue]) -> [any QLPreviewItem]`](/documentation/AppKit/NSTextView/quickLookPreviewableItems(inRanges:))

Returns an array of URLs for items that can be displayed by QuickLook in the specified ranges.

### Changing layout orientation

[`func changeLayoutOrientation(Any?)`](/documentation/AppKit/NSTextView/changeLayoutOrientation(_:))

An action method that sets the layout orientation of the text.

[`func setLayoutOrientation(NSLayoutManager.TextLayoutOrientation)`](/documentation/AppKit/NSTextView/setLayoutOrientation(_:))

Changes the receiver’s layout orientation and invalidates the contents.

### Using the Find Bar

[`var usesFindBar: Bool`](/documentation/AppKit/NSTextView/usesFindBar)

A Boolean value that indicates whether to use the find bar for this text view.

[`var isIncrementalSearchingEnabled: Bool`](/documentation/AppKit/NSTextView/isIncrementalSearchingEnabled)

A Boolean value that indicates whether incremental searching is enabled.

### Constants

[`enum NSSelectionGranularity`](/documentation/AppKit/NSSelectionGranularity)

These constants specify how much the text view extends the selection when the user drags the mouse. They’re used by [`selectionGranularity`](/documentation/AppKit/NSTextView/selectionGranularity), and [`selectionRange(forProposedRange:granularity:)`](/documentation/AppKit/NSTextView/selectionRange(forProposedRange:granularity:)):

[`enum NSSelectionAffinity`](/documentation/AppKit/NSSelectionAffinity)

These constants specify the preferred direction of selection. They’re used by [`selectionAffinity`](/documentation/AppKit/NSTextView/selectionAffinity) and [`setSelectedRange(_:affinity:stillSelecting:)`](/documentation/AppKit/NSTextView/setSelectedRange(_:affinity:stillSelecting:)).

[`enum NSFindPanelAction`](/documentation/AppKit/NSFindPanelAction)

These constants define the tags for [`performFindPanelAction(_:)`](/documentation/AppKit/NSTextView/performFindPanelAction(_:)).

[Input Sources Locale Identifiers](/documentation/AppKit/input-sources-locale-identifiers)

Locale identifiers represent the input sources available.

[Find Panel Search Metadata](/documentation/AppKit/find-panel-search-metadata)

In addition to communicating search strings via the find pasteboard, the standard Find panel for `NSTextView` also communicates search option metadata, including case sensitivity and substring matching options. This metadata is stored in a property list as the [`findPanelSearchOptions`](/documentation/AppKit/NSPasteboard/PasteboardType/findPanelSearchOptions) value on the global find pasteboard. As such, third party applications may store additional keys in this property list to communicate additional metadata as desired to support the various search options common to many third-party applications’ Find panels.

[`enum NSFindPanelSubstringMatchType`](/documentation/AppKit/NSFindPanelSubstringMatchType)

The type of substring matching used by the Find panel.

### Notifications

`NSTextView` posts the following notifications as well as those declared by its superclasses, particularly [`class NSText`](/documentation/AppKit/NSText)

The most general programmatic interface for objects that manage text.. See the Notifications section in the [`class NSText`](/documentation/AppKit/NSText)

The most general programmatic interface for objects that manage text. class specification for those other notifications.

[`class let didChangeSelectionNotification: NSNotification.Name`](/documentation/AppKit/NSTextView/didChangeSelectionNotification)

Posted when the selected range of characters changes.

[`class let willChangeNotifyingTextViewNotification: NSNotification.Name`](/documentation/AppKit/NSTextView/willChangeNotifyingTextViewNotification)

Posted when a new text view is established as the text view that sends notifications.

[`class let didChangeTypingAttributesNotification: NSNotification.Name`](/documentation/AppKit/NSTextView/didChangeTypingAttributesNotification)

Posted when there is a change in the typing attributes within a text view.

[`class let didSwitchToNSLayoutManagerNotification: NSNotification.Name`](/documentation/AppKit/NSTextView/didSwitchToNSLayoutManagerNotification)

Posted by the framework after switching to using the compatibility mode layout manager.

[`class let willSwitchToNSLayoutManagerNotification: NSNotification.Name`](/documentation/AppKit/NSTextView/willSwitchToNSLayoutManagerNotification)

Posted by the framework before switching to the compatibility mode layout manager.

### Interacting with the Touch Bar

[`var allowsCharacterPickerTouchBarItem: Bool`](/documentation/AppKit/NSTextView/allowsCharacterPickerTouchBarItem)

[`var candidateListTouchBarItem: NSCandidateListTouchBarItem<AnyObject>?`](/documentation/AppKit/NSTextView/candidateListTouchBarItem)

[`func updateTextTouchBarItems()`](/documentation/AppKit/NSTextView/updateTextTouchBarItems())

[`func updateTouchBarItemIdentifiers()`](/documentation/AppKit/NSTextView/updateTouchBarItemIdentifiers())

### Structures

[`struct DidBeginEditingMessage`](/documentation/AppKit/NSTextView/DidBeginEditingMessage)

[`struct DidChangeMessage`](/documentation/AppKit/NSTextView/DidChangeMessage)

[`struct DidChangeSelectionMessage`](/documentation/AppKit/NSTextView/DidChangeSelectionMessage)

[`struct DidChangeTypingAttributesMessage`](/documentation/AppKit/NSTextView/DidChangeTypingAttributesMessage)

[`struct DidEndEditingMessage`](/documentation/AppKit/NSTextView/DidEndEditingMessage)

[`struct WillChangeNotifyingTextViewMessage`](/documentation/AppKit/NSTextView/WillChangeNotifyingTextViewMessage)

### Instance Properties

[`var allowedWritingToolsResultOptions: NSWritingToolsResultOptions`](/documentation/AppKit/NSTextView/allowedWritingToolsResultOptions)

[`var inlinePredictionType: NSTextInputTraitType`](/documentation/AppKit/NSTextView/inlinePredictionType)

[`var mathExpressionCompletionType: NSTextInputTraitType`](/documentation/AppKit/NSTextView/mathExpressionCompletionType)

[`var textHighlightAttributes: [NSAttributedString.Key : Any]`](/documentation/AppKit/NSTextView/textHighlightAttributes)

************************* Text Highlight  support **************************

[`var writingToolsBehavior: NSWritingToolsBehavior`](/documentation/AppKit/NSTextView/writingToolsBehavior)

### Instance Methods

[`func drawTextHighlightBackground(for: NSTextRange, origin: NSPoint)`](/documentation/AppKit/NSTextView/drawTextHighlightBackground(for:origin:))

[`func highlight(Any?)`](/documentation/AppKit/NSTextView/highlight(_:))

An action for toggling `NSTextHighlightStyleAttributeName` in the receiver’s selected range.
The sender should be a menu item with a `representedObject` of type (`NSTextHighlightColorScheme`).

[`func register(NSTextAttachmentViewProviderReusePolicy, forTextAttachmentViewProviderType: AnyClass)`](/documentation/AppKit/NSTextView/register(_:forTextAttachmentViewProviderType:))

Register the NSTextAttachmentViewProviderReusePolicy for all instances of a particular subclass of NSTextAttachmentViewProvider.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`NSTextInput`](/documentation/AppKit/NSTextInput)

[`NSAccessibilityNavigableStaticText`](/documentation/AppKit/NSAccessibilityNavigableStaticText)

[`NSUserInterfaceItemIdentification`](/documentation/AppKit/NSUserInterfaceItemIdentification)

[`NSColorChanging`](/documentation/AppKit/NSColorChanging)

[`NSAccessibilityProtocol`](/documentation/AppKit/NSAccessibilityProtocol)

[`NSDraggingDestination`](/documentation/AppKit/NSDraggingDestination)

[`NSAccessibilityStaticText`](/documentation/AppKit/NSAccessibilityStaticText)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSCandidateListTouchBarItemDelegate`](/documentation/AppKit/NSCandidateListTouchBarItemDelegate)

[`NSTextInputClient`](/documentation/AppKit/NSTextInputClient)

[`NSUserActivityRestoring`](/documentation/AppKit/NSUserActivityRestoring)

[`NSIgnoreMisspelledWords`](/documentation/AppKit/NSIgnoreMisspelledWords)

[`NSDraggingSource`](/documentation/AppKit/NSDraggingSource)

[`NSTextLayoutOrientationProvider`](/documentation/AppKit/NSTextLayoutOrientationProvider)

[`NSStandardKeyBindingResponding`](/documentation/AppKit/NSStandardKeyBindingResponding)

[`Hashable`](/documentation/Swift/Hashable)

[`NSAccessibilityElementProtocol`](/documentation/AppKit/NSAccessibilityElementProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSTextViewportLayoutControllerDelegate`](/documentation/AppKit/NSTextViewportLayoutControllerDelegate)

[`NSAppearanceCustomization`](/documentation/AppKit/NSAppearanceCustomization)

[`NSMenuItemValidation`](/documentation/AppKit/NSMenuItemValidation)

[`Copyable`](/documentation/Swift/Copyable)

[`NSAnimatablePropertyContainer`](/documentation/AppKit/NSAnimatablePropertyContainer)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Escapable`](/documentation/Swift/Escapable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSTouchBarDelegate`](/documentation/AppKit/NSTouchBarDelegate)

[`NSTextContent`](/documentation/AppKit/NSTextContent)

[`NSTouchBarProvider`](/documentation/AppKit/NSTouchBarProvider)

[`NSChangeSpelling`](/documentation/AppKit/NSChangeSpelling)

[`NSUserInterfaceValidations`](/documentation/AppKit/NSUserInterfaceValidations)

[`NSCoding`](/documentation/Foundation/NSCoding)

### Inherits From

[`NSText`](/documentation/AppKit/NSText)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)