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

# textView(_:willChangeSelectionFromCharacterRanges:toCharacterRanges:)

Returns the actual character ranges to select.

```
@MainActor optional func textView(_ textView: NSTextView, willChangeSelectionFromCharacterRanges oldSelectedCharRanges: [NSValue], toCharacterRanges newSelectedCharRanges: [NSValue]) -> [NSValue]
```

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

`oldSelectedCharRanges`

An array containing the original ranges of the selection. This must be a non-`nil`, non-empty array of objects responding to the `NSValue` method `rangeValue`, 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.

`newSelectedCharRanges`

An array containing the proposed character ranges for the new selection. This must be a non-`nil`, non-empty array of objects responding to the `NSValue` method `rangeValue`, 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.

## Return Value

An array containing the actual character ranges for the new selection.

## Discussion

Invoked before an `NSTextView` object finishes changing the selection—that is, when the last argument to a [`setSelectedRange(_:affinity:stillSelecting:)`](/documentation/AppKit/NSTextView/setSelectedRange(_:affinity:stillSelecting:)) or [`setSelectedRanges(_:affinity:stillSelecting:)`](/documentation/AppKit/NSTextView/setSelectedRanges(_: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.

If a delegate implements both this method and [`textView(_:willChangeSelectionFromCharacterRange:toCharacterRange:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:willChangeSelectionFromCharacterRange:toCharacterRange:)), then the latter 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)