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

# setSelectedRange(_:)

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

```
func setSelectedRange(_ charRange: NSRange)
```

## Parameters

`charRange`

The range of characters to select. The range must begin and end on glyph boundaries and not split base glyphs and their nonspacing marks.

## Discussion

Calling this method resets the [`selectionGranularity`](/documentation/AppKit/NSTextView/selectionGranularity) property to [`NSSelectionGranularity.selectByCharacter`](/documentation/AppKit/NSSelectionGranularity/selectByCharacter) and the [`selectionAffinity`](/documentation/AppKit/NSTextView/selectionAffinity) property to [`NSSelectionAffinity.upstream`](/documentation/AppKit/NSSelectionAffinity/upstream).

---

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)