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

放肆的年华 提交于 2019-12-12 07:46:24

问题


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.


回答1:


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.




回答2:


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

textField.tintColor = [UIColor clearColor];



回答3:


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"];



回答4:


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/




回答5:


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:.




回答6:


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];
}


来源:https://stackoverflow.com/questions/18139234/prevent-editing-of-text-in-uitextfield-and-hide-cursor-caret-magnifying-glass-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!