<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextViewDelegate/textView(_:shouldChangeTextInRanges:replacementStrings:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextViewDelegate(im)textView:shouldChangeTextInRanges:replacementStrings:"
  },
  "title" : "textView(_:shouldChangeTextInRanges:replacementStrings:)"
}
-->

# textView(_:shouldChangeTextInRanges:replacementStrings:)

Sent when a text view needs to determine if text in an array of specified ranges should be changed.

```
@MainActor optional func textView(_ textView: NSTextView, shouldChangeTextInRanges affectedRanges: [NSValue], replacementStrings: [String]?) -> Bool
```

## Parameters

`textView`

The text view sending the message. This is the first text view in a series shared by a layout manager, not necessarily the text view displaying the selected text.

`affectedRanges`

The array of ranges of characters to be replaced. This array must be a non-nil, non-empty array of objects responding to the NSValue `rangeValue` method, and in addition its elements must be sorted, non-overlapping, non-contiguous, and (except for the case of a single range) have non-zero-length.

`replacementStrings`

The array of strings that will replace the characters in `affectedRanges`, one string for each range; `nil` if only text attributes are being changed.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> to allow the replacement, or <doc://com.apple.documentation/documentation/Swift/false> to reject the change.

---

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)