---
title: "characterOffset(of:within:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uitextinput/characteroffset(of:within:)"
---

# characterOffset(of:within:)

Returns the character offset of a position in a document’s text that falls within a specified range.

## Declaration

```swift
optional func characterOffset(of position: UITextPosition, within range: UITextRange) -> Int
```

## Parameters

- `position`: An object that identifies a location in a document’s text.
- `range`: An object that specifies a range of text in a document.

## Return Value

Return Value The number of characters in a document’s text that occur between position and the beginning of range.

## Discussion

Discussion You should implement this method if you don’t have a one-to-one correspondence between UITextPosition objects within the given range and character offsets into a document string.

## See Also

### Reconciling text position and character offset

- [position(within:atCharacterOffset:)](uikit/uitextinput/position(within:atcharacteroffset:).md)
