Wednesday, February 2, 2011

Setting maximum length limit in UITextField in iPhone.

There is no direct method to set the maximum length of input an UITextField should allow. But we can restrict the user to input to a certain length of characters.
Suppose we want a uitextfield only to accept upto 10 characters. Any character (other than enter and delete) should not be allowed to input if the user has already
entered 10 characters. i.e, we want to set a maximum length verifier to the UITextField. In Objective C we can do it as follows.
    We can create our own delegate for the UITextField. In this delegate we can put our logic to verify the input. And also we need to set this delegate as the UITextfield 's
delegate.

Step1.
    Writing the delegate:
    In MaxLengthDelegate.h

    @interface MaxLengthDelegate : NSObject <UITextFieldDelegate>{
        @private
            int max_length; //
      }
      @end

    In MaxLengthDelegate.m implement the following method and also modify the init method to init this with the maximum length user wants.

        - (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered
        {
            //check for enter and delete keys. These should be allowed even after the length is equal to max_length.
            if (([textField.text length] && range.length == 1) || [textEntered isEqualToString:@"\n"]) {
                return YES;
            }
   
            //check the length and return NO if it exceeds the max_length.
            if ([textField.text length] >= max_length) {
                return NO;
            }
   
            return YES;
        }

        //Setting the default max_length to 10.
        - (id) init
        {
            self = [super init];
            if (self != nil) {
                max_length = 10;
            }
            return self;
        }

        //to init delegate with the length.
        - (id) init : (int) maxLength
        {
            self = [super init];
            if (self != nil) {
                max_length = maxLength;
            }
            return self;
        }

Steps 2:
    Set the delegate of the UITextField which length needs to be verified as the maxLengthDelegate. For example, if we want to restrict input length of the UITextField (txtField) to 5, we can do it as:

    MaxLengthDelegate  maxLenDel = [[MaxLengthDelegate alloc] init: 5];
    [txtField setDelegate: maxLenDel];

Thanks

2 comments:

  1. i have created the two files MaxLengthDelegate.h and MaxLengthDelegate.m then i import MaxLengthDelegate.h in my view where is the UITextField and in ViewDidAppear i do this:

    MaxLengthDelegate maxLenDel = [[MaxLengthDelegate alloc] init: 5];
    [txtField setDelegate: maxLenDel];

    and when i compile appear this error:
    variable-sized object may not be initialized

    statically allocated instance of Objective-C class 'MaxLengthDelegate'

    statically allocated instance of Objective-C class 'MaxLengthDelegate'


    incompatible type for argument 1 of 'setDelegate:'

    what i wrong?

    ReplyDelete
  2. Objective-C object instances must be pointers. Initialization should be:
    MaxLengthDelegate *maxLenDel = [[MaxLengthDelegate alloc] init: 5];

    or
    MaxLengthDelegate *maxLenDel;
    maxLenDel = [[MaxLengthDelegate alloc] init: 5];

    ReplyDelete