【问题标题】:Prevent editing of text in UITextField and hide cursor/caret/magnifying glass while using an inputView防止在 UITextField 中编辑文本并在使用 inputView 时隐藏光标/插入符号/放大镜
【发布时间】:2013-08-09 02:19:40
【问题描述】:

如何防止编辑 UITextField 中的文本以及隐藏光标/插入符号/放大镜,但仍然显示键盘,因为我正在使用带有 UIPickerView/UIDatePickerViewinputView ?

userInteractionEnabled 设置为NO 不起作用,因为它不再接收任何触摸并且不会显示键盘。

【问题讨论】:

    标签: ios objective-c uitextfield inputview


    【解决方案1】:

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

    在您的 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;
    }
    

    现在只需根据 UIPickerView/UIDatePicker 的结果以编程方式设置文本。

    【讨论】:

    • 没有手势识别器 (iOS 8): (lldb) po [textField gestureRecognizers] -> nil
    【解决方案2】:

    在 iOS 7 中隐藏光标要简单得多。仍然需要一些技巧来禁用放大镜

    textField.tintColor = [UIColor clearColor];
    

    【讨论】:

    • 仍然不知道为什么从storyboard设置tintColor不生效,虽然代码可以。
    【解决方案3】:

    希望对你有所帮助。

    设置光标 UIColor -> 空。在 UI 中,它将被隐藏。

    [[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
    

    【讨论】:

    • 这是官方的方式吗?我认为这是对苹果私有 API 的操纵。
    • 这对我来说看起来很可疑!使用 Walapu 的答案。
    【解决方案4】:

    我发现最好的解决方案是

    - (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/

    【讨论】:

    • 这行得通,但您仍然可以单击并输入文本字段,除非您还按照 Walapu 的回答实现 textField:shouldChangeCharactersInRange:replacementString:
    【解决方案5】:

    在没有交互的情况下拥有UITextField,但仍使用inputView

    使用这些方法子类化 UITextField:

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

    最后一种方法将单独阻止任何编辑和放大镜,因为您将无法点击UITextField

    例如,如果您在 UITableViewCell 中使用文本字段,这非常有用,然后可以通过 tableView:didSelectRowAtIndexPath: 切换 firstResponder 状态。

    【讨论】:

      【解决方案6】:

      要禁用与文本字段的任何交互,除了使其成为第一响应者之外,您只需在文本字段上放置一个相同大小的 UIButton。按钮点击事件的代码可能是这样的:

      - (IBAction)btnEditPhoneTapped:(id)sender
      {
          if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        相关资源
        最近更新 更多