<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextSelectionNavigation/textSelection(for:enclosing:inContainerAt:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextSelectionNavigation(im)textSelectionForSelectionGranularity:enclosingPoint:inContainerAtLocation:"
  },
  "title" : "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.

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

## Parameters

`selectionGranularity`

One of the available [`NSTextSelection.Granularity`](/documentation/AppKit/NSTextSelection/Granularity-swift.enum) options.

`point`

The point that encloses the text.

`location`

An [`NSTextLocation`](/documentation/AppKit/NSTextLocation) that describes the container.

## Return Value

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

---

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)