---
title: "setSelectedRange(_:affinity:stillSelecting:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nstextview/setselectedrange(_:affinity:stillselecting:)"
---

# setSelectedRange(_:affinity:stillSelecting:)

Sets the selection to a range of characters in response to user action.

## Declaration

```swift
func setSelectedRange(_ charRange: NSRange, affinity: NSSelectionAffinity, stillSelecting stillSelectingFlag: Bool)
```

## Parameters

- `charRange`: The range of characters to select. This range must begin and end on glyph boundaries and not split base glyphs and their nonspacing marks.
- `affinity`: The selection affinity for the selection. See doc://com.apple.appkit/documentation/AppKit/NSTextView/selectionAffinity for more information about how affinities work.
- `stillSelectingFlag`: doc://com.apple.documentation/documentation/Swift/true to behave appropriately for a continuing selection where the user is still dragging the mouse, doc://com.apple.documentation/documentation/Swift/false otherwise. If doc://com.apple.documentation/documentation/Swift/true, the receiver doesn’t send notifications or remove the marking from its marked text. If doc://com.apple.documentation/documentation/Swift/false, the receiver posts an doc://com.apple.appkit/documentation/AppKit/NSTextView/didChangeSelectionNotification to the default notification center and removes the marking from marked text if the new selection is greater than the marked region.

## Discussion

Discussion This method resets the selection granularity to NSSelectByCharacter. Special Considerations In macOS 10.4 and later, if there are multiple selections, this method acts on the first selected subrange.

## See Also

### Managing the selection

- [selectedRanges](appkit/nstextview/selectedranges.md)
- [setSelectedRange(_:)](appkit/nstextview/setselectedrange(_:).md)
- [setSelectedRanges(_:affinity:stillSelecting:)](appkit/nstextview/setselectedranges(_:affinity:stillselecting:).md)
- [selectionAffinity](appkit/nstextview/selectionaffinity.md)
- [selectionGranularity](appkit/nstextview/selectiongranularity.md)
- [insertionPointColor](appkit/nstextview/insertionpointcolor.md)
- [updateInsertionPointStateAndRestartTimer(_:)](appkit/nstextview/updateinsertionpointstateandrestarttimer(_:).md)
- [selectedTextAttributes](appkit/nstextview/selectedtextattributes.md)
- [markedTextAttributes](appkit/nstextview/markedtextattributes.md)
- [linkTextAttributes](appkit/nstextview/linktextattributes.md)
- [characterIndexForInsertion(at:)](appkit/nstextview/characterindexforinsertion(at:).md)
- [updateCandidates()](appkit/nstextview/updatecandidates().md)
