Fay007
Fay007

Reputation: 2917

How to limit a UITextField for a limited character in ios

I have added delegate UITextFieldDelegate in .h file

Then in .m file under viewDidLoad:

[self textField:self.comment shouldChangeTextInRange:NSMakeRange(0,10) replacementText:@""];

then added the function:

- (BOOL)textField:(UITextField *)textField shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

   if(textField.text.length >= 10)
   {
       return NO;
   }

    return YES;

}

My goal is to limit the keyboard input to 10 character for the textfield?? Whats i am doing wrong or how to achieve that ??

Upvotes: 2

Views: 775

Answers (7)

Sucharu Hasija
Sucharu Hasija

Reputation: 1126

public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if(range.length + range.location > textField.text?.characters.count)
    {
        return false
    }

    let newLength = (textField.text?.characters.count)! + string.characters.count - range.length;
    return newLength <= self.maximumCharacterAllowed
}

Upvotes: 0

PSS
PSS

Reputation: 316

This works correctly with backspace and copy & paste

#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;
}

Upvotes: 2

Imran
Imran

Reputation: 2543

Use this Code

#define KMAX_CHAR_LENGTH 20
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length >= KMAX_CHAR_LENGTH && range.length == 0) {
        return NO; // do not change text
    } else {
        return YES; // Change Text
    }
}

Upvotes: 0

Uma Madhavi
Uma Madhavi

Reputation: 4917

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

    if (textField == self.textField)
    {
        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        return (newLength > 10) ? NO : YES;
    }
    return YES;
}

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27438

Remove or comment this line [self textField:self.comment shouldChangeTextInRange:NSMakeRange(0,10) replacementText:@""]; in viewDidLoad.

Add yourtextfield.delegate = self; in viewDidLoad

and implement below delegate method,

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

    //allow back space
    if (range.length == 0 && [string length] == 0) {
        return YES;
    }

    if(textField.text.length >= 10)
    {
        return NO;
    }
    else{
        return YES;
    }
}

Hope, this will help :)

Upvotes: 1

user_1989
user_1989

Reputation: 375

Firstly, you shouldn't call the methods "shouldChangeCharactersInRange: explicitly. This delegate method will get called when you type in text in your self.comment TextFiled, provided you have assigned the delegate by saying,

self.comment.delegate = self.

You can either assigned the delegate in your xib or in the .m file.

Code that you have added in the shouldChangeCharactersInRange method is correct to mask the input to 10 characters.

Hope this will help. Thank you.

Upvotes: 1

Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

use this code :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // Prevent crashing undo bug – see note below.
    if(range.length + range.location > textField.text.length)
    {
        return NO;
    }

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return newLength <= 25;
}

Upvotes: 0

Related Questions