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

后端 未结 6 1635
北荒
北荒 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.

    0 讨论(0)
  • 2021-02-13 02:26

    To disable any interaction with textfield except for making it a first responder you can just place a UIButton of the same size right over the textfield. Code for button tap event could be something like this:

    - (IBAction)btnEditPhoneTapped:(id)sender
    {
        if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
    }
    
    0 讨论(0)
  • 2021-02-13 02:28

    To have a UITextField without interaction, but still work with an inputView:

    Subclass UITextField with these methods:

    // Hide the cursor
    - (CGRect)caretRectForPosition:(UITextPosition*)position
    {
        return CGRectZero;
    }
    
    // All touches inside will be ignored
    // and intercepted by the superview
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        return NO;
    }
    

    The last method will single-handedly prevent any editing and the magnifier glass, as you won't be able to tap the UITextField.

    This works great if you're using a textfield in a UITableViewCell for example, and then can toggle firstResponder status through tableView:didSelectRowAtIndexPath:.

    0 讨论(0)
  • 2021-02-13 02:30

    I found the best solution was

    - (CGRect) caretRectForPosition:(UITextPosition*) position
    {
        return CGRectZero;
    }
    
    - (NSArray *)selectionRectsForRange:(UITextRange *)range
    {
        return nil;
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
        {
            returnNO;
        }
    
        return [super canPerformAction:action withSender:sender];
    }
    

    http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

    0 讨论(0)
  • 2021-02-13 02:31

    I hope this will helpful for you.

    Set cursor UIColor -> Empty. In UI, it will be hidden.

    [[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
    
    0 讨论(0)
  • 2021-02-13 02:35

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

    textField.tintColor = [UIColor clearColor];
    
    0 讨论(0)
提交回复
热议问题