Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Last active April 30, 2021 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prafullakumar/a7803908072267a632bcf8dc97a5e213 to your computer and use it in GitHub Desktop.
Save prafullakumar/a7803908072267a632bcf8dc97a5e213 to your computer and use it in GitHub Desktop.
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