<!--
{
  "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/NSTextElementProvider/replaceContents(in:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextElementProvider(im)replaceContentsInRange:withTextElements:"
  },
  "title" : "replaceContents(in:with:)"
}
-->

# replaceContents(in:with:)

Replaces the characters specified by range with the text elements you provide.

```
func replaceContents(in range: NSTextRange, with textElements: [NSTextElement]?)
```

## Parameters

`range`

An [`NSTextRange`](/documentation/UIKit/NSTextRange).

`textElements`

The elements to replace that characters at `range`.

## Discussion

If the edges of `range` aren’t at existing element range boundaries, the method either splits the element if it allows the operation (for example, [`NSTextParagraph`](/documentation/UIKit/NSTextParagraph)), or the adjusts the replacement range.

> Note:
> This method is for use by ``doc://com.apple.uikit/documentation/UIKit/NSTextLayoutManager``.

---

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)