---
title: "textSelection(for:enclosing:inContainerAt:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nstextselectionnavigation/textselection(for:enclosing:incontainerat:)"
---

# textSelection(for:enclosing:inContainerAt:)

Returns a text selection that expands to the nearest boundaries for selection granularity and an enclosing point you specify.

## Declaration

```swift
func textSelection(for selectionGranularity: NSTextSelection.Granularity, enclosing point: CGPoint, inContainerAt location: any NSTextLocation) -> NSTextSelection?
```

## Parameters

- `selectionGranularity`: One of the available doc://com.apple.appkit/documentation/AppKit/NSTextSelection/Granularity-swift.enum options.
- `point`: The point that encloses the text.
- `location`: An doc://com.apple.appkit/documentation/AppKit/NSTextLocation that describes the container.

## Return Value

Return Value A new NSTextSelection, or nil if the text selection is not found.

## See Also

### Selection characteristics

- [allowsNonContiguousRanges](appkit/nstextselectionnavigation/allowsnoncontiguousranges.md)
- [rotatesCoordinateSystemForLayoutOrientation](appkit/nstextselectionnavigation/rotatescoordinatesystemforlayoutorientation.md)
- [NSTextSelectionNavigation.Modifier](appkit/nstextselectionnavigation/modifier.md)
- [NSTextSelectionNavigation.Destination](appkit/nstextselectionnavigation/destination.md)
- [NSTextSelectionNavigation.Direction](appkit/nstextselectionnavigation/direction.md)
