Last active
October 25, 2021 21:04
-
-
Save AnshumanSingh1605/a557ab8c84a5f12c788493d090b8b080 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
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 👎