<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSText/replaceCharacters(in:withRTFD:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSText(im)replaceCharactersInRange:withRTFD:"
  },
  "title" : "replaceCharacters(in:withRTFD:)"
}
-->

# replaceCharacters(in:withRTFD:)

Replaces the characters in the given range with RTFD text interpreted from the given RTFD data.

```
func replaceCharacters(in range: NSRange, withRTFD rtfdData: Data)
```

## Parameters

`range`

The range of characters to be replaced.

`rtfdData`

The RTFD data from which to derive the replacement string.

## Discussion

This method applies only to rich text objects.

This method does not include undo support by default. Clients must invoke [`shouldChangeText(inRanges:replacementStrings:)`](/documentation/AppKit/NSTextView/shouldChangeText(inRanges:replacementStrings:)) or [`shouldChangeText(in:replacementString:)`](/documentation/AppKit/NSTextView/shouldChangeText(in:replacementString:)) to include this method in an undoable action.

This method is designed for transferring text from out-of-process sources such as the pasteboard. In most cases, programmatic modification of the text is best done by operating on the text storage directly, using the general methods of <doc://com.apple.documentation/documentation/Foundation/NSMutableAttributedString>.

---

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)