Contents

UIFindInteraction

An interaction that provides text finding and replacing operations using a system find panel.

Declaration

@MainActor class UIFindInteraction

Mentioned in

Overview

Use a find interaction to add text-finding capabilities to your view.

When invoking the interaction, the system displays a find panel for entering a string to search, with buttons for navigating between results. When supporting replacement, this also includes a field for the text replacing matches. The find panel presents as a part of the onscreen keyboard when visible, or inline as part of the receiver’s view hierarchy.

Input from a hardware keyboard triggers a find interaction using standard system shortcuts such as Command+F for find, Command+G for find next, and Command+Shift+G for find previous. Adding the interaction also enables these commands in the menu bar on macOS when your view becomes first responder. You can also trigger the interaction by calling the presentFindNavigator(showingReplace:) method on the interaction object, providing access to find and replace operations programatically.

Some classes, such as UITextView, WKWebView, and PDFView, support built-in find interactions. To enable the interaction on these views, set isFindInteractionEnabled to true.

textView.isFindInteractionEnabled = true

To add find and replace operations to other views:

  1. Create the find interaction object, passing a delegate into the default initializer.

  2. Add the interaction by calling the addInteraction(_:) method on the view.

  3. Provide a UIFindSession object to manage the search performed on the content displayed in the view. You return this object through the findInteraction(_:sessionFor:) method on the delegate.

The following example creates a find interaction for a view that presents searchable content for a document.

let document = Document(string: "")
lazy var interactionView = InteractionView(document: document)

lazy var findInteraction = UIFindInteraction(sessionDelegate: self)

override func viewDidLoad() {
    super.viewDidLoad()

    // Add the find interaction.
    interactionView.addInteraction(findInteraction)
}

func findInteraction(_ interaction: UIFindInteraction, sessionFor view: UIView) -> UIFindSession? {
    // Return a searchable object.
    return UITextSearchingFindSession(searchableObject: document)
}

Implement the UITextSearching protocol on the class that encapsulates the searchable content for your view to use an instance of UITextSearchingFindSession as the session object. Alternatively, you can subclass UIFindSession when you want to manage the details of the session using a custom class.

Topics

Creating a find interaction

Managing find interactions

Configuring the find panel

Managing the search

See Also

Related Documentation

Find and replace