3

This is my code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"אבגדהוזחטיכלמנסעפצקרשתףץםן"] invertedSet];

    // max charcters
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    if (newLength > 14)
        return NO;

    // allow backspace
    if (range.length > 0 && [string length] == 0) {
        return YES;
    }
    // do not allow . at the beggining
    if (range.location == 0 && [string isEqualToString:@"."]) {
        return NO;
    }
    if ((range.location > 0) && (([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ף"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ץ"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ם"]) || ([[textField.text substringWithRange:NSMakeRange((range.location -1), 1)] isEqualToString:@"ן"])))   
    {
        return NO;
    }

    // set the text field value manually
    NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
    textField.text = newValue;
    // return NO because we're manually setting the value
    return NO;
}

I just want to do when you click on Return the keyboard will disappear. I can't do that. where to add it and how?

Himanshu
  • 31,810
  • 31
  • 111
  • 133
ytpm
  • 4,962
  • 6
  • 56
  • 113

3 Answers3

7

If you want to hide the keyboard after tapping the return key add the following to your code:

if ([string isEqualToString:@"\n"]) {
    [theTextField resignFirstResponder];
    return NO;
}

Hope it helps

Novarg
  • 7,390
  • 3
  • 38
  • 74
  • This helped me out with a different question. I forgot to allow the return key when filtering out UISearchBar text. – Bill Burgess Nov 14 '13 at 14:22
3

in the delegate method of UITextField textFieldShouldReturn use :-

[yourTextField resignFirestResponder];

Also implement the UITextFieldDelegate in your class and set the delegate in viewDidLoad as :-

yourTextField.delegate=self;
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
1

Create an IBAction and connect it with with your TextField view.

-(IBAction)doneEditing:(id)sender
{
     [sender resignFirstResponder];
}

Choose event Did end on Exit while connecting to File's Owner.

Deepjyoti Roy
  • 482
  • 4
  • 15