<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/NSTextSelectionNavigation/textSelection(for:enclosing:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextSelectionNavigation(im)textSelectionForSelectionGranularity:enclosingTextSelection:"
  },
  "title" : "textSelection(for:enclosing:)"
}
-->

# textSelection(for:enclosing:)

Returns a text selection expanded to the nearest boundaries for the selection granularity and enclosing text selection text ranges you specify.

```
func textSelection(for selectionGranularity: NSTextSelection.Granularity, enclosing textSelection: NSTextSelection) -> NSTextSelection
```

## Parameters

`selectionGranularity`

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

`textSelection`

The text selection that describes the text range of interest.

## Return Value

A new text selection.

---

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)