Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent editing of text in UITextField and hide cursor/caret/magnifying glass while using an inputView

How do I prevent the editing of text in a UITextField along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an inputView with a UIPickerView/UIDatePickerView?

Setting userInteractionEnabled to NO does not work, because it no longer receives any touches and will not present the keyboard.

like image 537
Josh Bernfeld Avatar asked Aug 09 '13 02:08

Josh Bernfeld


2 Answers

Subclass UITextField

//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}

//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
}

In your UITextFieldDelegate

//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}

Now just set your text programmatically from the result of your UIPickerView/UIDatePicker.

like image 152
Josh Bernfeld Avatar answered Oct 14 '22 08:10

Josh Bernfeld


Hiding the cursor is a lot simpler in iOS 7. Still need some trickery to disable the loupe

textField.tintColor = [UIColor clearColor];
like image 15
Chris Wagner Avatar answered Oct 14 '22 06:10

Chris Wagner