Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting backspace in UITextField the right way

Attention! This question is not a duplicate of this or this question! This is a new question to make things more clear.

So I have followed all the instructions of the posts above and I have this UITextField that becomes empty and moves to a new position every time the user hits return button. Moreover, I trace the input of the textView and I create a label of that text in the position the TextView was, like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField.text.length > 5) {
        CGRect labelFrame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 0, 0);
        UILabel *label = [[UILabel alloc] initWithFrame: labelFrame];
        label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
        [label setText:textField.text];
        [label setTextColor: [BSFunctions getColorFromHex:@"3f3f3f"]];
        label.backgroundColor =[UIColor lightGrayColor];
        [label sizeToFit];
        [labelsArray addObject:label];
        [self.view addSubview: label];

        CGRect newTextFieldFrame = CGRectMake(labelFrame.origin.x + label.frame.size.width + 5, labelFrame.origin.y, 320, 30);
        NSLog(@"Rect is %@", NSStringFromCGRect(newTextFieldFrame));
        textField.frame = newTextFieldFrame;
        textField.text = @"\u200B";
    }    
    return YES;
}

I set the text in UITextField text to be @"\u200B" and I then want to detect the backspace button on it like this:

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

    if ([string isEqualToString:@""]) {
        NSLog(@"backspace button pressed");

        if (labelsArray.count > 0) {
            UILabel *labelToDelete = [labelsArray lastObject];
            CGRect labelPosition = labelToDelete.frame;
            CGRect oldPosition = textField.frame;
            textField.frame = CGRectMake(labelPosition.origin.x, labelPosition.origin.y, oldPosition.size.width, oldPosition.size.height);
            [labelToDelete removeFromSuperview];
            [labelsArray removeLastObject];
            textField.text = @"\u200B";
        }
    }

    return YES;
}

But the problem is, it works only once and then, even with the special character added at the beginning of the textField it doesn't work. What is possibly wrong?

like image 819
Sergey Grischyov Avatar asked Feb 08 '13 12:02

Sergey Grischyov


1 Answers

After setting

textField.text = @"\u200B";

in textField:shouldChangeCharactersInRange:replacementString:

You will have to add

return NO;

otherwise it will continue to the return YES and replace it with a @"" and then when pressing the backspace once again nothing will be changed and the delegate method will not be called.

It is possible that I misunderstood your goal here, but I hope this helps.

like image 79
Johan Andersson Avatar answered Nov 10 '22 03:11

Johan Andersson