@@ -59,6 +59,20 @@ public struct ResponsiveTextField {
5959 /// If this is not set, the textfield delegate will indicate that the return key is not handled.
6060 var handleReturn : ( ( ) -> Void ) ?
6161
62+ /// A callback function that will be called when the user deletes backwards.
63+ ///
64+ /// Takes a single argument - a `String` - which will be the current text when the user
65+ /// hits the delete key (but before any deletion occurs).
66+ ///
67+ /// If this is an empty string, it indicates that the user tapped delete inside an empty field.
68+ var handleDelete : ( ( String ) -> Void ) ?
69+
70+ /// A callback function that can be used to control whether or not text should change.
71+ ///
72+ /// Takes two `String` arguments - the text prior to the change and the new text if
73+ /// the change is permitted.
74+ ///
75+ /// Return `true` to allow the change or `false` to prevent the change.
6276 var shouldChange : ( ( String , String ) -> Bool ) ?
6377
6478 public init (
@@ -68,6 +82,7 @@ public struct ResponsiveTextField {
6882 isSecure: Bool = false ,
6983 configuration: Configuration = . empty,
7084 handleReturn: ( ( ) -> Void ) ? = nil ,
85+ handleDelete: ( ( String ) -> Void ) ? = nil ,
7186 shouldChange: ( ( String , String ) -> Bool ) ? = nil
7287 ) {
7388 self . placeholder = placeholder
@@ -76,6 +91,7 @@ public struct ResponsiveTextField {
7691 self . isSecure = isSecure
7792 self . configuration = configuration
7893 self . handleReturn = handleReturn
94+ self . handleDelete = handleDelete
7995 self . shouldChange = shouldChange
8096 }
8197}
@@ -84,9 +100,10 @@ public struct ResponsiveTextField {
84100
85101extension ResponsiveTextField : UIViewRepresentable {
86102 public func makeUIView( context: Context ) -> UITextField {
87- let textField = UITextField ( )
103+ let textField = DeleteHandlingTextField ( )
88104 configuration. configure ( textField)
89105 // This stops the text field from expanding if the text overflows the frame width
106+ textField. handleDelete = handleDelete
90107 textField. setContentCompressionResistancePriority ( . defaultLow, for: . horizontal)
91108 textField. placeholder = placeholder
92109 textField. text = text. wrappedValue
@@ -181,6 +198,15 @@ extension ResponsiveTextField: UIViewRepresentable {
181198 }
182199}
183200
201+ private class DeleteHandlingTextField : UITextField {
202+ var handleDelete : ( ( String ) -> Void ) ?
203+
204+ override func deleteBackward( ) {
205+ handleDelete ? ( text ?? " " )
206+ super. deleteBackward ( )
207+ }
208+ }
209+
184210// MARK: - TextField Configurations
185211
186212extension ResponsiveTextField {
0 commit comments