Reputation: 3340
I've read here in many questions how to limit the length of my uitextfield programmatically by implementing shouldChangeCharactersInRange
method but isn't there a way to do it in storyboard? it seems, to me,like a bad performance practice to check it with every keyboard click
In every other programing language i know that has an IDE + ui editor the control has a property to set max length same as it has for setting the text, font, border style etc.
Upvotes: 0
Views: 1758
Reputation: 8200
You can't set a maximum character limit by setting something in the storyboard. You need to do it programmatically. And as long as you're just doing character limit checking, there's no noticeable performance impact to this. In fact if you think about it, regardless of whether you implement the check or it was somehow a storyboard option, it would still have to execute some code each time to see if it could add another character. Also this is so insignificant compared to the work involved in redrawing the screen that you would not notice any difference at all.
It's important to note in implementing a character limit check that you also have to handle when the user pastes text in, because this adds many characters at once. So you have to decide on how to handle that, whether you take just the characters up until your limit, or if you don't accept any of them because it exceeds your limit.
You also have to handle the case that the user is adding characters from the beginning or somewhere in the middle. Do you start deleting characters off the end to stay within your limit or not accept new characters anywhere in the string? These are decisions you have to make and account for in your code.
However, as I said before, all of this is so minor a burden on the processor that you won't notice any difference.
Upvotes: 0
Reputation: 20410
There's no way of setting that from the storyboard, as is a method that is called by the delegate, so you need to do it programatically.
Upvotes: 1