<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextInputClient/setMarkedText(_:selectedRange:replacementRange:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextInputClient(im)setMarkedText:selectedRange:replacementRange:"
  },
  "title" : "setMarkedText(_:selectedRange:replacementRange:)"
}
-->

# setMarkedText(_:selectedRange:replacementRange:)

Replaces a specified range in the receiver’s text storage with the given string and sets the selection.

```
func setMarkedText(_ string: Any, selectedRange: NSRange, replacementRange: NSRange)
```

## Parameters

`string`

The string to insert. Can be either an `NSString` or `NSAttributedString` instance.

`selectedRange`

The range to set as the selection, computed from the beginning of the inserted string.

`replacementRange`

The range to replace, computed from the beginning of the marked text.

## Discussion

If there is no marked text, the current selection is replaced. If there is no selection, the string is inserted at the insertion point.

When `aString` is an `NSString` object, the receiver is expected to render the marked text with distinguishing appearance (for example, `NSTextView` renders with [`markedTextAttributes`](/documentation/AppKit/NSTextView/markedTextAttributes)).

---

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)