隐藏UITextField的光标

eme*_*gro 122 cocoa-touch uipickerview uitextfield ios

我使用的是UITextFieldUIPickerView它的inputView,这样当用户点击文本字段,一个选择器被召唤为他们选择一个选项.

几乎所有的作品,但我有一个问题:光标仍然闪烁的文本字段时,它是积极的,这是丑陋和不恰当的,因为用户不希望被输入到现场,并没有跟键盘.我知道我可以通过设置hackily解决这个问题editing,以NO文本字段,并对其进行监视触摸,或通过使用自定义风格的按钮替换它,召唤通过代码选择器.不过,我想使用的UITextFieldDelegate方法对所有的事件处理上的文本字段和黑客如用按钮替换文本字段不允许这种做法.

我怎样才能简单地将光标隐藏在UITextField

小智 264

简单地将UITextField子类化并覆盖caretRectForPosition

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}
Run Code Online (Sandbox Code Playgroud)

  • 比接受的答案简单得多.并且不那么hacky (13认同)
  • 请注意,即使光标被隐藏并且您使用选择器视图,具有外部键盘的用户也可以更改文本字段的值. (4认同)
  • 这应该是公认的解决方案. (2认同)
  • 美丽的!对我来说就像一种魅力。 (2认同)

jam*_*one 148

从iOS 7开始,您现在可以tintColor = [UIColor clearColor]在textField上设置,插入符号将消失.

  • 目前这有效,但我建议不要使用它,因为它将来可能会改变。而是选择“caretRectForPosition:”覆盖解决方案。 (2认同)
  • 现在好了.有时您只需要快速解决方案. (2认同)

old*_*man 87

你可以清除文本字段的tintColor

self.textField.tintColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

self.textField.tintColor = .clear
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 如上所述,清除色泽不会阻止使用外部键盘(iPad Pro)的用户更改文本。 (2认同)

小智 20

您可能还希望阻止用户选择,复制或粘贴任何文本,以便唯一的文本输入来自选择器视图.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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


ma1*_*w28 14

退房财产 selectedTextRange的的协议 UITextInput,对此 UITextField符合.很少!这是面向对象编程的一个教训.

隐藏Caret

要隐藏插入符号,请忽略文本字段的选定文本范围.

textField.selectedTextRange = nil; // hides caret
Run Code Online (Sandbox Code Playgroud)

取消隐藏Caret

以下是两种取消隐藏插入符号的方法.

  1. 将文本字段的选定文本范围设置为文档的末尾.

    UITextPosition *end = textField.endOfDocument;
    textField.selectedTextRange = [textField textRangeFromPosition:end
                                                        toPosition:end];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 要将插入符保持在同一位置,首先,将文本字段的选定文本范围存储到实例变量中.

    _textFieldSelectedTextRange = textField.selectedTextRange;
    textField.selectedTextRange = nil; // hides caret
    
    Run Code Online (Sandbox Code Playgroud)

    然后,当您想要取消隐藏插入符时,只需将文本字段的选定文本范围设置回原来的范围:

    textField.selectedTextRange     = _textFieldSelectedTextRange;
    _textFieldLastSelectedTextRange = nil;
    
    Run Code Online (Sandbox Code Playgroud)

  • 我不在乎提交报告.如果其他人使用你的"解决方案"并且看不到它的工作,我希望他们知道他们并不孤单. (3认同)
  • 这个特殊的解决方案对我的实现不起作用.光标仍然闪烁. (2认同)

Rob*_*und 10

OP提供的答案,从问题机构复制,以帮助清理不断增长的未解决问题的尾部.

我找到了另一个解决方案:子类UIButton并覆盖这些方法

- (UIView *)inputView {
    return inputView_;
}

- (void)setInputView:(UIView *)anInputView {
    if (inputView_ != anInputView) {
        [inputView_ release];
        inputView_ = [anInputView retain];
    }
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

现在,按钮作为一个UIResponder,具有类似的行为,UITextField而且实现非常简单.

  • 这不是一个很好的解决方案.请查看以下答案:http://stackoverflow.com/a/13660503/1103584 (5认同)
  • 为什么这不是一个很好的解决方案?它实现了预期的效果,并且还为以前没有它的类提供了功能.这也不是一个黑客.我觉得这很酷.那有什么不对呢? (2认同)
  • @BreadicalMD我能看到的最大问题是你不能用`UITextFieldDelegate`来处理开始和结束编辑事件.相反 - 除非有办法处理那些我不知道的事件 - 你需要在按钮子类中覆盖`becomeFirstResponder`和`resignFirstResponder`,并可能创建你自己的委托协议,添加一个`delegate`属性,并从上述方法调用委托.这比仅仅覆盖`UITextField`子类中的`caretRectForPosition`要多得多. (2认同)

Rom*_*om. 6

Net的Swift 3版本

  override func caretRect(for position: UITextPosition) -> CGRect {
    return .zero
  }

  override func selectionRects(for range: UITextRange) -> [Any] {
    return []
  }

  override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    return false
  }
Run Code Online (Sandbox Code Playgroud)