I know there are other topics on this but I can't seem to find out how to implement it.
I'm trying to limit a UITextField to only 5 Characters
Preferably Alphanumeric and - and . and _
I've seen this code
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool
{
let maxLength = 4
let currentString: NSString = textField.text
let newString: NSString =
currentString.stringByReplacingCharactersInRange(range, withString: string)
return newString.length <= maxLength
}
and
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let length = count(textField.text.utf16) + count(string.utf16) - range.length
return length <= 10
}
I just don't know how to actually implement it or which "textfield" should I swap out for my custom named UITextField
Other solutions posted above produce a retain cycle due to the textfield map. Besides, the
maxLength
property should be nullable if not set instead of artificialInt.max
constructions; and the target will be set multiple times if maxLength is changed.Here an updated solution for Swift4 with a weak map to prevent memory leaks and the other fixes
Just in case, don't forget to guard range size before applying it to the string. Otherwise, you will get crash if the user will do this:
Type maximum length text Insert something (Nothing will be inserted due to length limitation, but iOS doesn't know about it) Undo insertion (You get crash, cause range will be greater than actual string size)
Also, using iOS 13 users can accidentally trigger this by gestures
I suggest you add to your project this
And use it like this:
Otherwise, you will constantly be getting crashed in your app
Dec 2017. Swift 4.
Take care that much of the example code which you will see online regarding this problem is very out of date.
Paste the following into any Swift file in your project. You can name the file anything, for example, "Handy.swift"
This finally fixes one of the stupidest problems in iOS:
Your text fields now have a
.maxLength
.It is completely OK to set that value in storyboard during development, or, set it in code while the app is running.
It's that simple.
Footnote - these days to safely truncate a
String
in swift, you simply.prefix(n)
An even simpler one-off version...
The above fixes all text fields in your project.
If you just want one particular text field to simply be limited to say "4", and that's that...
Phew! That's all there is to it.
(Just BTW, here's a similar very useful tip relating to UITextView, https://stackoverflow.com/a/42333832/294884 )
I think extension is more handy for this. See full answer here
I have something to add to Aladin's answer:
Your view controller should conform to
UITextFieldDelegate
Set the delegate of your textfield: To set the delegate, you can control drag from the textfield to your view controller in the storyboard. I think this is preferable to setting it in code
Implement the method in your view controller :
textField(_:shouldChangeCharactersInRange:replacementString:)
Swift 4, simply use: