Detect when a user stopped / paused writing in TextField - SwiftUI

708 Views Asked by At

I want to get notified when the user is done writing text in a TextField in SwiftUI.

The senario in as follows:

-the user enters text in a TextField.

-when the user wrote a string that is longer then 3 characters and paused or finished writing I want the app to get notified so I can work with that String.

tried .onEditingChanged but it will call the action with each change and not only when the user finished editing.

1

There are 1 best solutions below

0
On

Try below code, this way you can check the real time value entered by user in a textfield.

   struct MyFormView: View {
    @State var typingText: String = "" {
        willSet {
            if typingText.count == 3 {
                print("User now entering 4th character")
            }
        }
        didSet {
            print(typingText)
        }
    }

    var body: some View {
        let bindText = Binding<String>(
            get:{self.typingText},
            set:{self.typingText = $0})
    return VStack(alignment: .leading, spacing: 5) {
        Text("Name")
        TextField("Enter Name", text: bindText)
            .keyboardType(.default)
            .frame(height: 44)
        }.padding(.all, CGFloat(10))
    }
}