tags:

views:

337

answers:

3

Hi,

I have a UITextField for which I get input from some buttons that are part of the UI (so the input does not come form the phone's virtual keyboard). I want to limit that number of characters. I tried using the shouldChangeCharactersInRange, but it only works if the input comes form the virtual keyboard, and it doesn't work if the input comes from the buttons on the UI. Is there anyway I can solve this without programmatically having to count the characters in the text field every time I want to update it?

Thank you, Mihai Fonoage

A: 

Check the length of the string property on the textField before accepting changes.

David Sowsy
A: 

There is a textRectForBounds Property

Jaba
I do believe it is easier do check programmatically then to use this method. I would have to manipulate the rect after it is returned.
Mihai Fonoage
+1  A: 

Since you are programmatically making changes to the UITextField once the user clicks the relevant buttons on the UI, you should keep track of the number of characters entered using a counter. Increase the value of the counter every time a relevant UI button is touched. Update the textfield only if the counter value is <= maximum allowed no. of characters.

NP Compete
Apparently this is the only way. I was hoping that maybe some method form the UITextFieldDelegate can help, but it is not the case.
Mihai Fonoage
The relevant UITextFieldDelegate methods will be fired only when the actual textfield is touched and is in focus i.e., it is active. However, in your case user touched other buttons and application updates the textfield based on that. You can set the focus to the textfield when buttons are clicked and then fire off the relevant delegate methods, but I feel that maybe a roundabout way of achieving what you want.
NP Compete