Last active
April 30, 2021 14:52
-
-
Save prafullakumar/a7803908072267a632bcf8dc97a5e213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FormTextField: UIViewRepresentable { | |
let placeholder: String | |
@Binding var text: String | |
var returnKeyType: UIReturnKeyType = .next | |
var autocapitalizationType: UITextAutocapitalizationType = .none | |
var keyboardType: UIKeyboardType = .default | |
var tag: Int | |
func makeUIView(context: Context) -> UITextField { | |
let textField = UITextField(frame: .zero) | |
textField.delegate = context.coordinator | |
textField.placeholder = placeholder | |
textField.returnKeyType = returnKeyType | |
textField.autocapitalizationType = autocapitalizationType | |
textField.keyboardType = keyboardType | |
textField.textAlignment = .left | |
textField.tag = tag | |
//Editin listener | |
textField.addTarget(context.coordinator, action: #selector(Coordinator.textFieldDidChange(_:)), for: .editingChanged) | |
return textField | |
} | |
func updateUIView(_ uiView: UITextField, context: Context) { | |
uiView.text = text | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
final class Coordinator: NSObject, UITextFieldDelegate { | |
let formTextField: FormTextField | |
init(_ formTextField: FormTextField) { | |
self.formTextField = formTextField | |
} | |
func textFieldDidBeginEditing(_ textField: UITextField) { | |
//to do | |
} | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
return true | |
} | |
func textFieldDidEndEditing(_ textField: UITextField) { } | |
@objc func textFieldDidChange(_ textField: UITextField) { | |
formTextField.text = textField.text ?? "" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment