[英]Allow only Select and Copy from UITextField not working
我希望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
打印输出
割:返回假
复制:返回true
删除:返回假
_promptForReplace:返回false
_transliterateChinese:返回false
_insertDrawing:返回false
_showTextStyleOptions:返回false
_lookup:返回false
_define:返回false
_addShortcut:返回false
_accessibilitySpeak:返回false
_accessibilitySpeakLanguageSelection:返回false
_accessibilityPauseSpeaking:返回false
_share:返回true
makeTextWritingDirectionRightToLeft:返回false
makeTextWritingDirectionLeftToRight:返回false
我如何摆脱paste
并select
?
如果您从不允许输入,听起来您应该确实使用UILabel
并仅在您想要的操作上添加点击/长按手势。
但是,如果您仍然想要这样做...
为了实现粘贴,您需要继承 UITextField
并重写UIPasteConfigurationSupporting的功能,例如
class UnpasteableTextField: UITextField {
override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool {
return false
}
}
至于选择,您可能必须重写UITextInput或UIResponderStandardEditActions中的某些内容 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.