0

I would like to limit the characters in textField of IOS Application. I tried all the methods given in Stack Overflow, so I am asking this question.

Here I used one Label (secure code) and one textfield (5 characters).

I had given the below code in viewcontroller.m file, but not working.

   myTextField.delegate = self

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

{
   NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

   return !([newString length] > 5);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
kumar Sudheer
  • 705
  • 3
  • 8
  • 28

2 Answers2

7

In ViewController.h, <UITextFieldDelegate> should be added.

In your viewDidLoad, myTextField.delegate = self should be added.

And then, to limit the number of characters:

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

Works for me. Try it.

EDIT:

On the other hand, your code should work perfectly fine as well. Check your code again.

esh
  • 2,842
  • 5
  • 23
  • 39
  • I worked with this code. one issue is raised in ViewController.h file 'UITextFieldDelegate' not found. can U suggest me how to get out of this issue.. – kumar Sudheer Feb 13 '13 at 06:32
  • I added UIKit framework.my ViewContoller.m file is : [super viewDidLoad]; txtsecurecode.delegate = self -(BOOL)txtsecurecode:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; return !([newString length] > 5); } } error is in: -(BOOL) statement is "arithmetic on pointer to interface viewcontroller which is not a constant size in non-fragile abi".Can you help out to solve. – kumar Sudheer Feb 13 '13 at 07:04
  • `-(BOOL)txtsecurecode:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string` Change to `-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string` – esh Feb 13 '13 at 07:06
  • at the point of -(BOOL)textField issue: "use of UnDelacared identifier'textField' " is raised. how to overcome this. – kumar Sudheer Feb 13 '13 at 07:28
  • Okay. First up. Is your textfield linked as an IBOutlet? What is it called? Then put UITextFieldDelegate like this `@interface ViewController : UIViewController `. Next, you seem to have put `myTextField.delegate = self` in viewDidLoad, which is fine. **myTextField** should be your textfield name. Then add the method you have put up in the question. – esh Feb 13 '13 at 07:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24416/discussion-between-sudheer-chekuri-and-blackflam3) – kumar Sudheer Feb 13 '13 at 07:34
0

In swift: add UITextFieldDelegate as an extension. It makes your job easier.

Don't forget to set textField's delegate in viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()

    // set the delegate
    textField.delegate = self
}

extension ViewController:UITextFieldDelegate {
  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if textField.text?.characters.count >= 5 && range.length == 0 {
      return false
    }
    return true
  }
}

You can find a detailed example how to limit textfield by count and width: github source

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88