From e19f471f5c11d20fba586faea1a2b5f8d3b99e0b Mon Sep 17 00:00:00 2001 From: xavierdonnellon <74732530+xavierdonnellon@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:53:49 -0600 Subject: [PATCH 1/6] Update iTextField+ViewModifiers.swift --- Sources/iTextField/iTextField+ViewModifiers.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/iTextField/iTextField+ViewModifiers.swift b/Sources/iTextField/iTextField+ViewModifiers.swift index a1804b0..0bcf774 100644 --- a/Sources/iTextField/iTextField+ViewModifiers.swift +++ b/Sources/iTextField/iTextField+ViewModifiers.swift @@ -10,6 +10,14 @@ import UIKit @available(iOS 13.0, *) extension iTextField { + + //Sets the maximum amount of characters allowed in this text field. + public func characterLimit(_ limit: Int?) -> iTextField { + var view = self + view.characterLimit = limit + return view + } + /// Modifies the text field’s **font** from a `UIFont` object. 🔠🔡 /// - Parameter font: The desired font 🅰️🆗 /// - Returns: An updated text field using the desired font 💬 From b90211f6823bf623b8a34b1e640a9b80175a72eb Mon Sep 17 00:00:00 2001 From: xavierdonnellon <74732530+xavierdonnellon@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:55:12 -0600 Subject: [PATCH 2/6] Update iTextField+ViewModifiers.swift --- Sources/iTextField/iTextField+ViewModifiers.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/iTextField/iTextField+ViewModifiers.swift b/Sources/iTextField/iTextField+ViewModifiers.swift index 0bcf774..09bb420 100644 --- a/Sources/iTextField/iTextField+ViewModifiers.swift +++ b/Sources/iTextField/iTextField+ViewModifiers.swift @@ -11,7 +11,9 @@ import UIKit @available(iOS 13.0, *) extension iTextField { - //Sets the maximum amount of characters allowed in this text field. + /// Sets the maximum amount of characters allowed in this text field. + /// - Parameter limit: the maximum amount of characters allowed + /// - Returns: An updated text field limited to limit public func characterLimit(_ limit: Int?) -> iTextField { var view = self view.characterLimit = limit From 4cb4089277a0f5b31932f3bde61a4aa5b050ddb7 Mon Sep 17 00:00:00 2001 From: xavierdonnellon <74732530+xavierdonnellon@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:59:54 -0600 Subject: [PATCH 3/6] Update iTextField.swift Add character limit parameter. --- Sources/iTextField/iTextField.swift | 32 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Sources/iTextField/iTextField.swift b/Sources/iTextField/iTextField.swift index 3316c36..a777bbc 100644 --- a/Sources/iTextField/iTextField.swift +++ b/Sources/iTextField/iTextField.swift @@ -33,6 +33,7 @@ public struct iTextField: UIViewRepresentable { var autocapitalization: UITextAutocapitalizationType = .sentences var keyboardType: UIKeyboardType = .default var returnKeyType: UIReturnKeyType = .default + var characterLimit: Int? = nil var isSecure = false var isUserInteractionEnabled = true @@ -165,18 +166,22 @@ public struct iTextField: UIViewRepresentable { } public func makeCoordinator() -> Coordinator { - return Coordinator(text: $text, - isEditing: isEditing, - didBeginEditing: didBeginEditing, - didChange: didChange, - didEndEditing: didEndEditing, - shouldReturn: shouldReturn, - shouldClear: shouldClear) + return Coordinator( + text: $text, + isEditing: isEditing, + characterLimit: characterLimit, + didBeginEditing: didBeginEditing, + didChange: didChange, + didEndEditing: didEndEditing, + shouldReturn: shouldReturn, + shouldClear: shouldClear + ) } public final class Coordinator: NSObject, UITextFieldDelegate { @Binding var text: String @Binding var isEditing: Bool + var characterLimit: Int? = nil var didBeginEditing: () -> Void var didChange: () -> Void @@ -186,6 +191,7 @@ public struct iTextField: UIViewRepresentable { init(text: Binding, isEditing: Binding, + characterLimit: Int?, didBeginEditing: @escaping () -> Void, didChange: @escaping () -> Void, didEndEditing: @escaping () -> Void, @@ -194,6 +200,7 @@ public struct iTextField: UIViewRepresentable { { self._text = text self._isEditing = isEditing + self.characterLimit = characterLimit self.didBeginEditing = didBeginEditing self.didChange = didChange self.didEndEditing = didEndEditing @@ -238,5 +245,16 @@ public struct iTextField: UIViewRepresentable { text = "" return false } + + func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText newText: String) -> Bool { + //if there is a character limit set and new text will be greater than limt, then don't allow the newly proposed edit + if let limit = characterLimit { + if textView.text.count + newText.count > limit { + return false + } + } + + return true + } } } From 7d6739b14ab42c2fba5c038f5ccdd27baeafc0ec Mon Sep 17 00:00:00 2001 From: xavierdonnellon <74732530+xavierdonnellon@users.noreply.github.com> Date: Wed, 6 Jan 2021 13:10:10 -0600 Subject: [PATCH 4/6] Update iTextField.swift --- Sources/iTextField/iTextField.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/iTextField/iTextField.swift b/Sources/iTextField/iTextField.swift index a777bbc..0782b58 100644 --- a/Sources/iTextField/iTextField.swift +++ b/Sources/iTextField/iTextField.swift @@ -166,6 +166,7 @@ public struct iTextField: UIViewRepresentable { } public func makeCoordinator() -> Coordinator { + print("makeCoordinator: \(characterLimit)") return Coordinator( text: $text, isEditing: isEditing, @@ -246,14 +247,14 @@ public struct iTextField: UIViewRepresentable { return false } - func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText newText: String) -> Bool { + public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { //if there is a character limit set and new text will be greater than limt, then don't allow the newly proposed edit - if let limit = characterLimit { - if textView.text.count + newText.count > limit { + if let limit = characterLimit, let text = textField.text { + if text.count + string.count > limit { return false } } - + return true } } From a9d7a315925231c4d6be9b85fc2d3b5498f5a6eb Mon Sep 17 00:00:00 2001 From: xavierdonnellon <74732530+xavierdonnellon@users.noreply.github.com> Date: Wed, 6 Jan 2021 13:11:22 -0600 Subject: [PATCH 5/6] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9c7859c..e013e14 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ Modifier | Description `.keyboardType(_ type: UIKeyboardType)` | Modifies the text field’s **keyboard type**. 📩 `.autocapitalization(_ style: UITextAutocapitalizationType)` | Modifies the text field’s **autocapitalization** style. 🔡 `.returnKeyType(_ type: UIReturnKeyType)` | Modifies the text field’s **return key** type. ✅ +`.characterLimit(_ limit: Int?)` | Sets the maximum amount of characters allowed in this text field. `.isSecure(_ isSecure: Bool)` | Modifies the text field’s **secure entry** settings. 🔒 `.clearsOnBeginEditing(_ shouldClear: Bool)` | Modifies the **clear-on-begin-editing** setting of a text field. ❌ `clearsOnInsertion(_ shouldClear: Bool)` | Modifies the **clear-on-insertion** setting of a text field. 👆 From ee276ec464b85ca3357347de5aa1006d3d7a57cd Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 14 Feb 2021 01:57:23 -0800 Subject: [PATCH 6/6] Removed extraneous print statement --- Sources/iTextField/iTextField.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/iTextField/iTextField.swift b/Sources/iTextField/iTextField.swift index 0782b58..16fca85 100644 --- a/Sources/iTextField/iTextField.swift +++ b/Sources/iTextField/iTextField.swift @@ -166,7 +166,6 @@ public struct iTextField: UIViewRepresentable { } public func makeCoordinator() -> Coordinator { - print("makeCoordinator: \(characterLimit)") return Coordinator( text: $text, isEditing: isEditing,