【问题标题】:Disable Look Up & Share on text selection in uitextfield在 uitextfield 中禁用查找和共享文本选择
【发布时间】:2021-04-04 07:06:22
【问题描述】:

如何仅对 uitextfield 中的文本选择禁用“查找”和“共享”。以下是我尝试过的快速代码

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(delete(_:)) || action == #selector(cut(_:))
        {
            return true
        } else if action == Selector(("_lookup:")) || action == Selector(("_share:")) || action == Selector(("_define:")) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

我只想显示剪切、复制、粘贴和删除,无论是否选择文本。

【问题讨论】:

    标签: ios swift uitextfield


    【解决方案1】:

    下面的代码对我有用,可以禁用“共享”但不能“查找”。如果您能找到有关禁用查找的信息,请更新我。另外,不要忘记在 Storyboard Identity Inspector 中将 textField 的类更改为 CustomUITextField 类

    class CustomUITextField: UITextField {
    
        override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            
            if action.description.contains("_share")
            {
                return false
            }
    
            return super.canPerformAction(action, withSender: sender)
        }
    }
    

    【讨论】:

      【解决方案2】:
      class CustomActionTextField: UITextField {
          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
              if action.description.contains("_share") // block "Share"
                  || action.description.contains("_translate") // block "Translate"
                  || action.description.contains("_define") // block "Look Up" {
                  return false
              }
              return super.canPerformAction(action, withSender: sender)
          }
      }
      

      您可以通过print(action.description)查看更多操作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-10
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多