1

I have three UITextField's which I want to limit in length. The first and second text field to 24 characters and the third text field to 32 characters.

I found this solution for limiting the characters in a UITextField which works very fine:

#define MAXLENGTH 10

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSUInteger oldLength = [textField.text length];
    NSUInteger replacementLength = [string length];
    NSUInteger rangeLength = range.length;

    NSUInteger newLength = oldLength - rangeLength + replacementLength;

    BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;

    return newLength <= MAXLENGTH || returnKey;
}

But this solution suggests, that every text-field is limited to same length (i.e. 10 characters as defined at the top of the code snippet). I was wondering if there is a chance to define some kind of property for each of my text-fields separately in the Interface-Builder of XCode which allows me to read out in the code -- so that I could replace "MAXLENGTH" with something like "[textField valueForKey:@"maxLength"]" where "maxLength" would be defined visually in the XCode Interface-Builder...

Any ideas?

Community
  • 1
  • 1
salocinx
  • 3,715
  • 8
  • 61
  • 110
  • 1
    You could subclass the textfield, add an IBInspectable value that represents the max length, and then assign your custom class as the textfield in IB. – luk2302 Jun 18 '15 at 15:16
  • 1
    And a quite messy idea :D use the tag of the textfield as max length, e.g use the tag 9015 as max length 15, and 9030 as 30, etc... ;) – luk2302 Jun 18 '15 at 15:21
  • 3
    Despite what the answer below says, it is actually possible to define the max length in Interface Builder using the tag field of each text view. Views can be loaded by tag (which would't work in this case if more than one has the same tag value) or their associated tag value can be queried if restored by other means. However the tag field is intended to identify views not to store associated data for them. But by using tags it is possible to use them to store the max data in IB. I'm not saying you should do that nor am I saying rmaddy's answer is wrong, just saying its possible so you know. – Gruntcakes Jun 18 '15 at 15:42
  • @luk2302 and Gruntcakes: Thank you very much for your fast replies! I will think about mis-using the tag property. But I already know, that when I take my project two months later out of the box again, I will not remember that dirty hack ;-p – salocinx Jun 18 '15 at 15:52

1 Answers1

1

There's no way to define the max length for each text field through Interface Builder but there is a simple solution in your code.

Instead of using a #define for the max length, calculate the max length based on the textField parameter.

Lets say you have three outlets for your text fields named field1, field2, and field3. Then you can do something like this:

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger maxLength = 24; // for field1 and field2
    if (textField == self.field3) {
        maxLength = 32;
    }

    // and now the rest of the processing
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thanks a lot for your help - probably I will go this way. the code may look worse, but I will remember at least how it works when I look at it two months later ;-) – salocinx Jun 18 '15 at 15:54