【问题标题】:Disable cursor and copy/paste in UITextView (swift)在 UITextView 中禁用光标和复制/粘贴(swift)
【发布时间】:2015-02-09 21:36:25
【问题描述】:

我有一个UITextField,我使用UIPickerView 作为它的inputView。用户可以从选择器中选择一个值,然后将所选值填充到我的TextInput 中。
这种安排效果很好,但存在以下问题:

1) 我想禁用仍显示在UITextField 中的光标。
我尝试禁用 UITextField,但它不响应触摸,然后 UIPickerView 不显示 - 这使它无用。
2)由于选择器已显示且键盘未在用户点击文本字段时,用户无法键入任何内容,但仍可以通过长按粘贴从其他位置复制的文本。如何禁用此功能?

我无法在网上找到相关信息。我应该为此使用Label 还是Button 而不是UITextInput

【问题讨论】:

    标签: ios xcode swift uitextfield uipickerview


    【解决方案1】:

    我认为简单的方法是用按钮代替UITextField

    抱歉 - 这不是给 Swift 而是给 Obj-C 但这是个主意:

    要使用UITextField 做你想做的事,你必须继承UITextField 并尝试使用此代码来禁用/隐藏插入符号和输入(复制/粘贴)

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

    无论如何...这是一个“功能性”示例:https://github.com/hackiftekhar/IQDropDownTextField

    【讨论】:

    • 对于任何试图保持 dataDetectorTypes 功能不让用户选择(即复制、全选、查找)围绕可操作数据的其余文本的人来说,这是一个很好的解决方案。谢谢@TonyMkenu
    【解决方案2】:

    就像 TonyMkenu 说的,你需要继承 UITextField 的子类,然后实现他上面的方法。这是为那些不了解 Objective C 的人准备的 Swift:

    override func caretRectForPosition(position: UITextPosition!) -> CGRect {
        return CGRect.zeroRect
    }
    
    override func selectionRectsForRange(range: UITextRange) -> [AnyObject] {
        return []
    }
    
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        // Disable copy, select all, paste
        if action == Selector("copy:") || action == Selector("selectAll:") || action == Selector("paste:") {
            return false
        }
        // Default
        return super.canPerformAction(action, withSender: sender)
    }
    

    【讨论】:

      【解决方案3】:

      SWIFT 4

      创建 TextField.swift 文件并将 import Foundation 更改为导入 UIKit 并在 TextFeild.swift 文件中添加以下代码

      import UIKit
      
      class TextFeild: UITextField {
      
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              // write code here what ever you want to change property for textfeild. 
          }
      
          override func caretRect(for position: UITextPosition) -> CGRect {
              return CGRect.zero
          }
      
          override func selectionRects(for range: UITextRange) -> [Any] {
              return []
          }
      
          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
              if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
                  return false
              }
              // Default
              return super.canPerformAction(action, withSender: sender)
          }
      }
      

      转到 main.storyboard,选择该文本字段并将其自定义类更改为 TextField

      【讨论】:

      • 很好的解决方案。刚刚注意到 selectionRects 应该返回 [UITextSelectionRect]: override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] { return [] }
      【解决方案4】:

      完全愚蠢的 hack,但是如果您在 Interface Builder 属性检查器的 UIView 部分中设置文本字段的色调颜色以匹配背景颜色,则光标将显示为不可见:

      【讨论】:

      • 虽然我赞成你的回答,因为它会隐藏光标,但复制粘贴选项仍然存在。
      • 是的,愚蠢的黑客。现在我们有了深色模式和其他动态主题,也变得更加复杂。
      【解决方案5】:

      UITextField 委托可以实现textFieldShouldBeginEditing 方法。如果该方法总是返回NO,那么光标将永远不会出现,并且不允许长按。

      调用该方法时,可以显示UIPickerView。但是,UIPickerView 不能是 inputView。它需要是标准UIView 的子元素,您可以从底部对其进行动画处理。或者您可以根据需要使用UIViewhidden 属性来隐藏/显示视图。

      【讨论】:

      • 我明白了,所以我使用UIPickerView 作为inputViewUITextView 的方法是错误的。你能给我举个例子,或者类似的东西以正确的方式做吗?
      • 是的,这有问题,因为我仍然使用 Objective-C,而且我尽可能避免自动布局,所以我可以在概念方面提供帮助,但在代码方面却没有那么多。
      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2021-10-11
      相关资源
      最近更新 更多