Set UITextField Maximum Length in iphone | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Set UITextField Maximum Length in iphone

Wednesday

iPhone SDK: Set Max Character length TextField:
                                                                       The UITextField class has no max length property, it's simple to get this functionality by setting the text field's delegate

Step 1: Implement the UITextFieldDelegate protocol and tag into your text field(s).

Step 2: Add the following line in to top of your .m file

#define MAX_LENGTH 10


Step 3: Implement the following method textField:shouldChangeCharactersInRange:replacementString

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; // return NO to not change text
    }
    else
    {
     return YES;
    }
}

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets