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

# textView(_:willChangeSelectionFromCharacterRange:toCharacterRange:)

Returns the actual range to select.

```
@MainActor optional func textView(_ textView: NSTextView, willChangeSelectionFromCharacterRange oldSelectedCharRange: NSRange, toCharacterRange newSelectedCharRange: NSRange) -> NSRange
```

## 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.

`oldSelectedCharRange`

The original range of the selection.

`newSelectedCharRange`

The proposed character range for the new selection.

## Return Value

The actual character range for the new selection.

## Discussion

This method is invoked before a text view finishes changing the selection—that is, when the last argument to a [`setSelectedRange(_:affinity:stillSelecting:)`](/documentation/AppKit/NSTextView/setSelectedRange(_:affinity:stillSelecting:)) message is <doc://com.apple.documentation/documentation/Swift/false>.

Non-selectable text views do not process any mouse events. If for some reason it is necessary to disallow user selection change in a text view that handles mouse events, this can be achieved by making the text view selectable but implementing this delegate method to disallow selection changes.

### Special Considerations

In macOS 10.4 and later, if a delegate implements this delegate method and not its multiple-selection replacement, [`textView(_:willChangeSelectionFromCharacterRanges:toCharacterRanges:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:willChangeSelectionFromCharacterRanges:toCharacterRanges:)), then multiple selection is effectively disallowed; attempts to set the selected ranges call the old delegate method with the first subrange, and afterwards only a single selected range is set.

---

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)