WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to let users find and replace text

Paul Hudson    @twostraws   

Updated for Xcode 14.2

New in iOS 16

SwiftUI’s TextEditor comes with built-in support to let the user search for text, or search and replace depending on their needs. It works out of the box for users who have a keyboard attached, so a view like this will work immediately:

struct ContentView: View {
    @State private var bio = "Describe yourself."

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

Download this as an Xcode project

To try that out, tap to activate the TextEditor, then press Cmd+F to active search, or press Option+Cmd+F to activate search and replace.

Note: Find and replace works only with TextEditor, not with TextField.

For users without a hardware keyboard, you can programmatically show the find interface using the findNavigator() modifier. For example, this toggles search using a toolbar button:

struct ContentView: View {
    @State private var bio = "Describe yourself."
    @State private var isShowingFindNavigator = false

    var body: some View {
        NavigationStack {
            TextEditor(text: $bio)
                .findNavigator(isPresented: $isShowingFindNavigator)
                .toolbar {
                    Button("Toggle Search") {
                        isShowingFindNavigator.toggle()
                    }
                }
                .navigationTitle("Edit Bio")
        }
    }
}

Download this as an Xcode project

Tip: Passing true to findNavigator() when no TextEditor is currently accepting input will make the system attempt to find and activate one automatically. If there is more than one to choose from the system will pick one for you.

If you explicitly want a view to opt out of search and/or replace, use one or both of findDisabled() and replaceDisabled(), like this:

struct ContentView: View {
    @State private var bio = "Describe yourself."

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

Download this as an Xcode project

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.