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

# textView(_:shouldChangeTextIn:replacementString:)

Sent when a text view needs to determine if text in a specified range should be changed.

```
@MainActor optional func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: 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.

`affectedCharRange`

The range of characters to be replaced.

`replacementString`

The characters that will replace the characters in `affectedCharRange`; `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.

## Discussion

If a delegate implements this method and not its multiple-selection replacement, [`textView(_:shouldChangeTextInRanges:replacementStrings:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:shouldChangeTextInRanges:replacementStrings:)), it is called with an appropriate range and string. If a delegate implements the new method, then this one is ignored.

---

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)