Dev
Dev

Reputation: 3945

UITextField cursor back movement

I want to prevent cursor back movement in UITextField. or I need to prevent entering text between two characters which is already entered. How can I do this?

Upvotes: 1

Views: 635

Answers (3)

akashivskyy
akashivskyy

Reputation: 45180

There is no property or method to prevent the user from using the magnifying glass on a particular text field. You can try to block all the characters inside the string:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(range.location < textField.text.length) return NO; else return YES;
}

After some research, I found this question: Disable Magnifying Glass in UITextField. The author of the accepted answer says there's a way to do it:

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.

But, as I found out, it doesn't work, since setUserInteractionEnabled:NO prevents the text field from becoming the first responder. After the second research I found another question: Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO. MaxGabriel user claims he found the way to do it:

What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.

Stack Overflow is a huge database, a little bit of research before asking a question may help you in the future.

Upvotes: 1

Alexey Kozhevnikov
Alexey Kozhevnikov

Reputation: 4259

You cannot prohibit caret moving, but you can use UITextFieldDelegate's method textField:shouldChangeCharactersInRange:replacementString: for testing if text was typed inside already typed string, if it's not then change text in textfield manually and move caret to the end with the help of selectedTextRange property of UITextInput protocol.

Upvotes: 0

Sumanth
Sumanth

Reputation: 4921

For stopping entering text in UiTextField, you should use uitextfield delegate method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
//Set tag for a textfield
    if(textField.tag==10)
    {
        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        return (newLength < [textField.text length]) ? NO : YES;
    }
}

Modify the logic as per your requirement. For preventing entering text between two characters Use NSRange and textfield text of range is less than textfield text length then return No then text will not be entered in UITextField

Upvotes: 0

Related Questions