Skip to content

Commit b9475b5

Browse files
committed
added way to limit textfield content length
1 parent b7730b1 commit b9475b5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

iOSFormUtils/FormInput.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ open class FormInput: UITextField {
3232
open var formInputDelegate: FormInputDelegate!
3333
fileprivate var inputAccessory: UIView!
3434
fileprivate var validationHandler: ValidatedFormInput!
35+
fileprivate var limit: Int!
3536
open var validationDelegate: ValidatedFormInputDelegate!
3637
open var validationDataSource: ValidatedFormInputDataSource!
3738

@@ -61,6 +62,13 @@ open class FormInput: UITextField {
6162
}
6263

6364
// MARK: Public own methods
65+
66+
/**
67+
Applies a custom char limit to the field
68+
*/
69+
open func setCustomCharLimit(limit: Int?) {
70+
self.limit = limit
71+
}
6472

6573
/**
6674
Stops the current edition.
@@ -94,5 +102,13 @@ extension FormInput: UITextFieldDelegate {
94102

95103
return true;
96104
}
105+
106+
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
107+
if let _ = limit {
108+
return textField.text!.characters.count < limit
109+
}
110+
111+
return true
112+
}
97113
}
98114

0 commit comments

Comments
 (0)