Skip to content

Instantly share code, notes, and snippets.

@AnshumanSingh1605
Last active October 25, 2021 21:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnshumanSingh1605/a557ab8c84a5f12c788493d090b8b080 to your computer and use it in GitHub Desktop.
Save AnshumanSingh1605/a557ab8c84a5f12c788493d090b8b080 to your computer and use it in GitHub Desktop.
import SwiftUI
import UIKit
protocol ASTextFieldDelegate {
func didBeginEditing(textField : UITextField)
func didEndEditing(textField : UITextField)
func didLeftItemtapped(_ button : UIButton)
}
extension ASTextFieldDelegate {
func didBeginEditing(textField : UITextField) {}
func didEndEditing(textField : UITextField) {}
func didLeftItemtapped(_ button : UIButton) {}
}
struct ASTextField : UIViewRepresentable {
var rightItem : UIImage?
var leftItem : UIImage?
var isSecuredEntry = false
var handleLeftTap : (() -> ()) = { }
private let textField = UITextField()
var delegate : ASTextFieldDelegate?
@Binding var text : String?
func makeUIView(context: UIViewRepresentableContext<ASTextField>) -> UITextField {
textField.isSecureTextEntry = isSecuredEntry
textField.text = text
if let rightimg = rightItem {
let button = UIButton()
button.setImage(rightimg, for: .normal)
button.addTarget(context.coordinator, action: #selector(context.coordinator.handleLeftTap(_:)), for: .touchUpInside)
textField.rightView = button
textField.rightViewMode = .always
}
if let leftImg = leftItem {
let imgView = UIImageView()
imgView.image = leftImg
textField.leftView = imgView
textField.leftViewMode = .always
}
return textField
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<ASTextField>) {
DispatchQueue.main.async {
self.text = uiView.text
}
}
func makeCoordinator() -> ASTextField.Coordinator {
Coordinator(self, isPassword : self.isSecuredEntry)
}
final class Coordinator : NSObject , UITextFieldDelegate {
var parent : ASTextField
private var isPasswordField : Bool
init(_ parent : ASTextField , isPassword : Bool) {
self.parent = parent
self.isPasswordField = isPassword
}
@objc func handleLeftTap(_ button : UIButton) {
if isPasswordField {
self.parent.textField.isSecureTextEntry = !self.parent.textField.isSecureTextEntry
} else {
self.parent.handleLeftTap()
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.parent.delegate?.didEndEditing(textField: textField)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.parent.delegate?.didBeginEditing(textField: textField)
}
}
}
@AnshumanSingh1605
Copy link
Author

AnshumanSingh1605 commented May 13, 2020

This is UIKit TextField conversion into SwiftUI , with handling of both left view and rightView. Here the user can even access the delegate methods of the textField in the View file , just by inheriting the ASTextFieldDelegate.

Please refer below snippet to implement it in the view 👎

struct ContentView : View , ASTextFieldDelegate {

let leftImage = UIImage(systemName: "calendar")
let rightImage = UIImage(systemName: "eye")
let rightImage1 = UIImage(systemName: "trash")

@State var text : String? = "with simple binding"
@State var text1 : String? = "with closure for right item"
@State var text2 : String? = "for secure entry"

var body: some View {
    VStack {
        Spacer()
        ASTextField(text: $text)
        
        Spacer()
        ASTextField(rightItem: rightImage1, leftItem: leftImage, handleLeftTap: {
            print("right icon tapped.....")
        }, delegate: self, text: $text1)
        
        Spacer()
        ASTextField(rightItem: rightImage, leftItem: leftImage, isSecuredEntry: true, delegate: self, text: $text2)
        Spacer()
    }
  }
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment