<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/AttributedString/replaceSelection(_:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation16AttributedStringV7SwiftUIE16replaceSelection_4withyAD0b4TextG0Vz_xtAA0bC8ProtocolRzlF"
  },
  "title" : "replaceSelection(_:with:)"
}
-->

# replaceSelection(_:with:)

Replace the selection with new attributed content.

```
mutating func replaceSelection(_ selection: inout AttributedTextSelection, with newContent: some AttributedStringProtocol)
```

## Discussion

In the case the selection is an insertion point, the `newContent` gets
inserted at the caret location and the caret is moved to after the
new content.

In the case where the selection spans one or multiple characters, those
characters are removed and the new content is inserted at location of
the first selected character.

---

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)