ios - 仅允许从 UITextField 中选择和复制不起作用

标签 ios swift

我希望我的 UITextField 只允许选择、复制和共享。这是到目前为止的内容。

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    print("\(action.description) returns \(action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == Selector(("_share:")) )" )
    
    return action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == Selector(("_share:"))
}

不幸的是,这不起作用。当我长按或双击时,我会看到以下选项:

 copy, select, select all, paste, share

打印输出

cut: returns false

copy: returns true

delete: returns false

_promptForReplace: returns false

_transliterateChinese: returns false

_insertDrawing: returns false

_showTextStyleOptions: returns false

_lookup: returns false

_define: returns false

_addShortcut: returns false

_accessibilitySpeak: returns false

_accessibilitySpeakLanguageSelection: returns false

_accessibilityPauseSpeaking: returns false

_share: returns true

makeTextWritingDirectionRightToLeft: returns false

makeTextWritingDirectionLeftToRight: returns false

我如何去掉pasteselect

最佳答案

如果您从不允许输入,听起来您真的应该使用 UILabel 并添加点击/长按手势,仅包含您希望可用的操作。

但是如果你还想做...

要实现粘贴,您需要子类化 UITextField 并覆盖 UIPasteConfigurationSupporting的功能例如

class UnpasteableTextField: UITextField {

    override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool {
        return false
    }
}

就选择而言,您可能必须覆盖 UITextInput 中的某些内容或 UIResponderStandardEditActions .

关于ios - 仅允许从 UITextField 中选择和复制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55305321/

相关文章:

ios - 使用 lldb 呈现 ViewController

objective-c - 带有不突出显示的渐变层的 UIButton

ios - 如何在 Swift 中使用 SKPSMTPMessageDelegate 协议(protocol)?

ios - 如何在swift3中完成5个文件下载请求后执行segue

ios - JSON 解码器返回没有与 key CodingKeys 关联的值,例如 IAP 收据中的 "expires_date"

iOS - 尝试实现动画时钟 : Unable to simultaneously satisfy constraints runtime error -noob level 99

ios - 在 UITableView 中包含选定单元格的部分之间滚动

ios - 后台快速 NSTimer

ios - 检测 Sprite 外面/远离 Sprite 的敲击

ios - CAShapeLayer 可以传递给 CGContextDrawLayerAtPoint 吗?