---
title: "textRange(for:enclosing:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/nstextselectiondatasource/textrange(for:enclosing:)"
---

# textRange(for:enclosing:)

Returns a text range that corresponds to selection granularity of the enclosing location.

## Declaration

```swift
func textRange(for selectionGranularity: NSTextSelection.Granularity, enclosing location: any NSTextLocation) -> NSTextRange?
```

## Parameters

- `selectionGranularity`: One of the possible doc://com.apple.uikit/documentation/UIKit/NSTextSelection/Granularity-swift.enum options.
- `location`: A location that encloses the text range of interest.

## Return Value

Return Value Returns the text range of the section, or nil when documentRange.isEmpty true.

## See Also

### Finding specific content in the selection

- [location(_:offsetBy:)](uikit/nstextselectiondatasource/location(_:offsetby:).md)
- [lineFragmentRange(for:inContainerAt:)](uikit/nstextselectiondatasource/linefragmentrange(for:incontainerat:).md)
- [offset(from:to:)](uikit/nstextselectiondatasource/offset(from:to:).md)
