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

后端 未结 6 1642
北荒
北荒 2021-02-13 01:40

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 in

6条回答
  •  忘了有多久
    2021-02-13 02:22

    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.

提交回复
热议问题