macosswiftuisearchtext-editor

SwiftUI TextEditor View not searchable on macOS?


First time using TextEditor in a macOS project.

According to this:
https://www.hackingwithswift.com/quick-start/swiftui/how-to-let-users-find-and-replace-text
my code should give me built-in Search / Search& Replace, but when I run and hit cmd-F, all I hear is the system "Tok" sound and do nothing.

struct ContentView: View {
    @State var text = "blah blach blach"

    var body: some View {
        NavigationStack {
            TextEditor(text: $text)
                .navigationTitle("Edit Bio")
        }
    }
}

Am I missing something? Already tried this on Apple Silicon and Intel Macs both running Sonoma, same result and building on Xcode 15.3.


Solution

  • The article you linked talks about iOS, where find & replace is indeed built into TextEditor without needing any extra code.

    On macOS, you can add TextEditingCommands to your scene to enable this:

    @main
    struct FooApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
            .commands {
                TextEditingCommands()
            }
        }
    }