<!--
{
  "availability" : [
    "iOS: 3.2.0 -",
    "iPadOS: 3.2.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UITextInput/characterOffset(of:within:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)UITextInput(im)characterOffsetOfPosition:withinRange:"
  },
  "title" : "characterOffset(of:within:)"
}
-->

# characterOffset(of:within:)

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

```
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

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

## Discussion

You should implement this method if you don’t have a one-to-one correspondence between [`UITextPosition`](/documentation/UIKit/UITextPosition) objects within the given range and character offsets into a document string.

---

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)