我希望我的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
打印输出
剪切:返回假
复制:返回真值
删除:返回假
_promptForReplace:返回假
_transliterateChinese:返回假
_insertDrawing:返回假
_showTextStyleOptions:返回假
_lookup:返回假
_define:返回假
_addShortcut:返回假
_accessibilitySpeak:返回假
_accessibilitySpeakLanguageSelection:返回假
_accessibilityPauseSpeaking:返回假
_share:返回真值
makeTextWritingDirectionRightToLeft: 返回 false
makeTextWritingDirectionLeftToRight:返回 false
如何摆脱paste
和select
?
如果您从不允许输入,听起来您真的应该使用UILabel
并添加点击/长按手势,仅使用您想要可用的操作。
但是,如果您仍然想这样做...
要实现粘贴效果,您需要子类化UITextField
并覆盖UIPasteConfigurationSupporting的功能,例如
class UnpasteableTextField: UITextField {
override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool {
return false
}
}
就选择而言,您可能必须覆盖UITextInput或UIResponderStandardEditActions中的某些内容。