Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Magnifying Glass in UITextField

Is there a way to prevent the user from moving the cursor in a UITextField? I'd like it to stay at the end of the string.

like image 247
Meltemi Avatar asked May 14 '09 22:05

Meltemi


4 Answers

This is an old question, but I was looking for an answer to the same question, and found a solution.

It is actually quite simple to prevent the user from moving the cursor. Just subclass UITextField and provide the following implementation of caretRectForPosition:

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return [super caretRectForPosition:self.endOfDocument];
}
like image 170
JRV Avatar answered Nov 18 '22 09:11

JRV


NO SUBCLASS needed.

You Could use UITextFieldDelegate. It will make disable magnifying glass & text selection.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.userInteractionEnabled = NO;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.userInteractionEnabled = YES;
}

NB: This is just a bypass.

like image 44
Lal Krishna Avatar answered Nov 18 '22 09:11

Lal Krishna


There's no way to prevent them from moving the cursor. You can, however, prevent them from editing the text except at the end by implementing the

– textField:shouldChangeCharactersInRange:replacementString:

method in your text field's delegate.

Edit: you can also set userInteractionEnabled to NO so that the user can't tap the field. Call becomeFirstResponder manually so that the field gets focus since the user can't tap to focus.

like image 1
Adam Ernst Avatar answered Nov 18 '22 10:11

Adam Ernst


I'd suggest to check the gestureRecognizers property.

You will find a lot of them in the array and might want to either remove them all or to find the ones that triggers the event you want to intercept and remove/replace it.

I used it to remove copy/paste and magnifying glass functionalities from an UITextField

like image 1
dwery Avatar answered Nov 18 '22 09:11

dwery