代码之家  ›  专栏  ›  技术社区  ›  emenegro

隐藏uitextfield的光标

  •  112
  • emenegro  · 技术社区  · 14 年前

    我正在使用 UITextField 用一个 UIPickerView 为其 inputView

    几乎所有的东西都可以工作,但我有一个问题:当光标处于活动状态时,它仍然在文本字段中闪烁,这是丑陋和不适当的,因为用户不需要输入字段,也不需要键盘。我知道我可以通过设置 editing NO UITextFieldDelegate 对于文本字段上的所有事件处理和黑客(如用按钮替换文本字段)的方法不允许使用这种方法。

    我怎样才能简单地将光标隐藏在 相反?

    13 回复  |  直到 7 年前
        1
  •  252
  •   DiscDev    12 年前

    - (CGRect)caretRectForPosition:(UITextPosition *)position
    {
        return CGRectZero;
    }
    
        2
  •  141
  •   jamone    11 年前

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

        3
  •  80
  •   Antony Raphel    8 年前

    只需清除文本字段的TintColor

    < /代码>
    self.textfield.tingcolor=.clear
    < /代码> 
    
    

    斯威夫特3

    self.textField.tintColor = .clear
    

    enter image description here

        4
  •  19
  •   Nat    10 年前

    - (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
  •  12
  •   ma11hew28    12 年前

    退房 selectedTextRange UITextInput , to which the UITextField 符合。很少!这是面向对象编程的一课。

    隐藏插入符号

    textField.selectedTextRange = nil; // hides 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;
      
        6
  •  10
  •   Rob Allen    10 年前

    UIButton 并重写这些方法

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

    现在按钮,作为 UIResponder ,具有类似于 UITextField 实现非常简单。

        7
  •  4
  •   Rom.    8 年前

    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
      }
    
        8
  •  1
  •   Robert Höglund    14 年前

    由OP提供的答案,从问题主体复制来帮助清理不断增长的未回答问题的尾巴。

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

    - (void)setText:(NSString *)aText {
        [super setText:nil];
    
        if (aText == nil) {
            textLabel_.text = nil;
        }
    
        if (![aText isEqualToString:@""]) {
            textLabel_.text = aText;
        }
    }
    

    有了这个,事情就如预期的那样运转。你知道如何改进它吗?

        9
  •  1
  •   Göktuğ Aral    11 年前

    如果您想隐藏光标,可以很容易地使用它!这对我很有用……

    [[textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"]
    
        10
  •  1
  •   Nat    10 年前

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

    - (CGRect)caretRectForPosition:(UITextPosition *)position {
        return CGRectZero;
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        [UIMenuController sharedMenuController].menuVisible = NO;
        self.selectedTextRange = nil;
    
        return NO;
    }
    
        11
  •  1
  •   Ahmad Al-Attal    7 年前

    将“淡色”设置为“清除颜色”

    textfield.tintColor = [UIColor clearColor];
    

    您还可以从接口生成器设置

        12
  •  0
  •   Mark Beaton    13 年前

    我只是子类 UITextField 和重写 layoutSubviews 如下:

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

    这是一个肮脏的黑客,并可能在未来失败(此时光标将再次可见-你的应用不会崩溃),但它工作。

        13
  •  -1
  •   Awesome-o    11 年前

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

    @interface UITextField (Cursorless)
    
    @property (nonatomic, assign) BOOL cursorless;
    
    @end
    

    然后用旋转的方法旋转 caretRectForPosition: 使用在 CGRectZero 其默认值使用 cursorless .

    这将通过一个下拉类别导致一个简单的界面。这在以下文件中演示。

    只需将它们放入并获得这个简单接口的好处

    输入框 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