How to detect when TextEditor is focused (tapped)

Viewed 210

I have a custom TextField where I detect when the user taps on it to change the look of the field, but I tried to add the same look for the TextEditor, but it doesn't have an onEditingChanged option.

This is my text field:

struct CustomTextField : View {
    @Binding var text : String
    @State private var isEditing = false
    @State var isDisabled = false
    
    init(text : Binding<String>, isDisabled : Bool = false ){
        self._text = text
        self.isDisabled = isDisabled
    }
    
    var body: some View {
        TextField("", text: $text, onEditingChanged: {
            isEditing = $0
        })
        .padding(8)
        .clipShape(RoundedRectangle(cornerRadius: 4))
        .background (
            RoundedRectangle(cornerRadius: 4)
                .stroke(isEditing ? Color("Accent") : Color("Gray"), lineWidth: isDisabled ? 0 : 1)

        )
    }
}

I thought of adding a tap gesture recognizer, but it will only detect when I click, not when I leave the text editor.

Is there a way to detect the focus state of a TextEditor?

0 Answers
Related