Give A SwiftUI Text Editor Find And Replace
A notes editor already knows how to select text, handle composition, and maintain undo. Adding a second custom string-replacement engine just to support Find can put those behaviors at risk.
For a standard SwiftUI TextEditor, start with the system find interface. The interesting work is its placement, permissions, and interaction with your document state.
Attach the navigator to the editor
Apple’s findNavigator modifier presents the find and replace interface for supported text editors. This small view accepts its document and presentation state from its owner:
import SwiftUI
struct FindableEditor: View {
@Binding var text: String
@Binding var isFindPresented: Bool
var allowsReplacement = true
var body: some View {
TextEditor(text: $text)
.replaceDisabled(!allowsReplacement)
.findNavigator(isPresented: $isFindPresented)
.toolbar {
Button("Find", systemImage: "magnifyingglass") {
isFindPresented = true
}
.keyboardShortcut("f", modifiers: .command)
}
}
}
The owner supplies both bindings and keeps them stable while the editor is onscreen. The toolbar action opens the navigator, and Command-F provides a hardware-keyboard entry point.
Attach the modifier to the intended editor. In a screen with several editable regions, putting a find navigator on an unrelated ancestor can make scope and focus harder to reason about. Test which editor owns the command rather than assuming visual proximity decides it.
Replacement permission is narrower than editing permission
The example follows Apple’s replacement modifier ordering and places replaceDisabled before findNavigator in the modifier chain so the navigator receives the replacement policy. A searchable document may allow finding without offering bulk replacement.
That flag does not make the entire TextEditor read-only. If the document itself cannot be edited, enforce that with the appropriate editor/view behavior and model permissions. Hiding a replacement button is not an authorization boundary.
Likewise, a normal app search field serves a different purpose from finding text inside this document. Use the find interface for editor content and reserve searchable navigation for searching the surrounding collection.
Preserve the document’s identity
Opening Find should not recreate the editor with a new ID or replace its text with a freshly loaded server value. Those transitions can disturb selection, insertion point, marked text, and undo history.
Keep presentation state separate from document data. Changing isFindPresented should not trigger an autosave unless the text actually changes. If your persistence layer debounces edits, feed it document revisions rather than every view update.
The same rule matters during resize or a sidebar transition: retaining the document does not automatically retain an editor recreated under a different identity. Exercise the actual layout transition with the navigator open and a selection active.
Do not implement Replace All with stale indices
If a future custom editor genuinely needs its own replacement behavior, snapshot the source text and calculate ranges against that exact version. Swift String indices are not stable offsets into a later edited string.
Do not take integer ranges from one representation and apply them directly to another. Unicode grapheme clusters and UTF-16 ranges have different units, and replacing earlier ranges can shift later ones. The system editor avoids making your app maintain a parallel selection and replacement model for the standard case.
For document-specific transforms beyond ordinary find/replace, make the operation explicit, validate it against the current revision, and integrate it with the editor’s undo behavior.
Verify more than the happy-path match
Test no matches, repeated matches, multiline text, emoji and combining characters, and replacement text longer than the match. Confirm that undo restores the intended edit and that cancelling Find leaves the document intact.
Also test hardware keyboard focus, VoiceOver navigation, large Dynamic Type, and a compact presentation. A toolbar icon appearing correctly does not establish that the entire find interaction is usable.
September 2026 review: the complete view type-checked for iOS 17 using SDK 27.0. The system find UI, replacement grouping, and assistive-technology behavior were not run in an interactive simulator for this fixture.