隐藏UITextField的光标

时间:2010-09-13 10:53:13

标签: ios cocoa-touch uitextfield uipickerview

我使用UITextField UIPickerView作为其inputView,以便当用户点按文字字段时,会召唤一个选择器,让他们从中选择一个选项。< / p>

几乎所有东西都可以工作,但是我有一个问题:当文本字段处于活动状态时,光标仍会在文本字段中闪烁,这是丑陋和不合适的,因为不希望用户键入字段并且不显示键盘。我知道我可以通过在文本字段上设置editingNO并跟踪其上的触摸,或者用自定义样式的按钮替换它,并通过代码召唤选择器来解决这个问题。但是,我想对文本字段中的所有事件处理使用UITextFieldDelegate方法,并且使用按钮替换文本字段等黑客不允许这种方法。

如何简单地将光标隐藏在UITextField上?

14 个答案:

答案 0 :(得分:253)

简单地继承UITextField并覆盖caretRectForPosition

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}

答案 1 :(得分:145)

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

答案 2 :(得分:82)

您可以清除文本字段的tintColor

self.textField.tintColor = [UIColor clearColor];

Swift 3.0

self.textField.tintColor = .clear

enter image description here

答案 3 :(得分: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];
}

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

答案 4 :(得分:12)

查看协议 selectedTextRange属性 UITextInput UITextField符合。很少!这是面向对象编程的一个教训。

Hide Caret

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

textField.selectedTextRange = nil; // hides caret

取消隐藏Caret

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

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

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

    _textFieldSelectedTextRange = textField.selectedTextRange;
    textField.selectedTextRange = nil; // hides caret
    

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

    textField.selectedTextRange     = _textFieldSelectedTextRange;
    _textFieldLastSelectedTextRange = nil;
    

答案 5 :(得分:10)

OP提供的答案,从问题正文中复制,以帮助清理不断增长的未解答问题。

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

- (UIView *)inputView {
    return inputView_;
}

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

- (BOOL)canBecomeFirstResponder {
    return YES;
}

现在,作为UIResponder的按钮具有与UITextField类似的行为,并且实现非常简单。

答案 6 :(得分:5)

Swift 3版本的Net帖子

  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
  }

答案 7 :(得分:1)

OP提供的答案,从问题正文中复制,以帮助清理不断增长的未解答问题。

我认为我有正确的解决方案,但如果可以改进则欢迎:)好吧,我创建了UITextField的子类并覆盖了返回边界的CGRect的方法

-(CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectZero;
}

问题?由于rect为零,因此文本不显示。但是我添加了一个UILabel作为控件的子视图并覆盖了setText方法,因此,当我们像往常一样输入文本时,文本字段文本为nil并且是显示文本的标签

- (void)setText:(NSString *)aText {
    [super setText:nil];

    if (aText == nil) {
        textLabel_.text = nil;
    }

    if (![aText isEqualToString:@""]) {
        textLabel_.text = aText;
    }
}

有了这个,事情按预期工作。你有什么方法可以改进吗?

答案 8 :(得分:1)

如果你想隐藏光标,你可以轻松使用它!它对我有用..

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

答案 9 :(得分:1)

要同时禁用光标和菜单我使用这两种方法的子类:

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [UIMenuController sharedMenuController].menuVisible = NO;
    self.selectedTextRange = nil;

    return NO;
}

答案 10 :(得分:1)

将tintColor设置为Clear Color

textfield.tintColor = [UIColor clearColor];

您也可以从界面构建器

进行设置

答案 11 :(得分:0)

我只是将UITextField子类化,并覆盖layoutSubviews,如下所示:

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIView *v in self.subviews)
    {
        if ([[[v class] description] rangeOfString:@"UITextSelectionView"].location != NSNotFound)
        {
            v.hidden = YES;
        }
    }
}

这是一个肮脏的黑客,将来可能会失败(此时光标将再次可见 - 您的应用程序不会崩溃),但它可以正常工作。

答案 12 :(得分:0)

如果您喜欢更简洁 = 更少代码,请使用界面构建器:

Clear color

(属性检查器,视图部分。)

答案 13 :(得分:-1)

您可以通过相关对象在类别中向BOOL cursorless添加UITextField属性。

@interface UITextField (Cursorless)

@property (nonatomic, assign) BOOL cursorless;

@end

然后使用方法调整来调用caretRectForPosition:,方法是使用CGRectZerocursorless及其默认值之间切换。

这通过插入类别导致简单的界面。这在以下文件中进行了演示。

只需将它们放入并获得此简单界面的好处

UITextField类别: https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.h https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.m

方法调整: https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.h https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.m

相关问题