TextField("Test", text: $name).textFieldStyle(tapflag ? CustomTextFieldStyle1() : CustomTextStyle2())
do you have an example of your own TextStyle? Please, share it!
UPDATE
you are better to use some parameter with your style and bind it to "parent" View
import SwiftUI
struct ContentView: View {
@State private var email = ""
@State private var editing = false
var body: some View {
TextField("Email", text: self.$email, onEditingChanged: { edit in
self.editing = edit
})
.textFieldStyle(MyTextFieldStyle(focused: $editing)).font(.title).border(Color.blue)
}
}
struct MyTextFieldStyle: TextFieldStyle {
@Binding var focused: Bool
func _body(configuration: TextField<Self._Label>) -> some View {
configuration
.padding(10)
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.stroke(focused ? Color.red : Color.gray, lineWidth: 3)
).padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
the result looks like
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…