【问题标题】:How to disable the copy paste functionality in SkyFloatingLabelTextField in swift?如何快速禁用 SkyFloatingLabelTextField 中的复制粘贴功能?
【发布时间】:2021-10-07 06:42:02
【问题描述】:

我正在为 UITextfield 使用 SkyFloatingLabelTextField 类,如何禁用此文本文件上的复制和粘贴功能。

【问题讨论】:

标签: swift xcode uitextfield uitextfielddelegate skyfloatinglabeltextfield


【解决方案1】:

创建一个继承自 SkyFloatingLabelTextField 类的自定义类,然后赋值。

class FloatingTextField: SkyFloatingLabelTextField {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
            action == #selector(UIResponderStandardEditActions.copy(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }
}

如果您想为整个项目和所有文本字段添加此扩展。

extension SkyFloatingLabelTextField {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
            action == #selector(UIResponderStandardEditActions.copy(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }
}

【讨论】:

  • 这也适用于 UItextView@Raja Kishan
  • @ManishKumar 是的
【解决方案2】:

将此技术用于自定义文本字段

   class SkyFloatingLabelTextField: UITextField {
        open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            if action == #selector(UIResponderStandardEditActions.paste(_:)) {
                return false
            }
            return super.canPerformAction(action, withSender: sender)
        }
    }

【讨论】:

  • 已经为 UITextField @Jayesh Patel 分配了一个 SkyfloatingLabelTextField 类
  • 然后将其添加到您的 SkyFloatingLabelTextField 类中。我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 2014-11-28
  • 2021-07-25
  • 2017-11-10
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 2017-03-29
  • 2013-12-15
相关资源
最近更新 更多