我有一个UITextField只显示数值(0-9,.,– ).当用户选择文本字段的内容时,出现“复制”,“粘贴”和“定义”的菜单.由于textfield只显示数值,我不希望出现“define”选项.如何在UITextField中禁用字典“define”选项?
解决方法
Swift – iOS 8
您可以通过子类化UITextField并覆盖canPerformAction:WithSender方法来实现.
class MyTextFieldWithoutDefine: UITextField { override func canPerformAction(action: Selector,withSender sender: AnyObject?) -> Bool { if action == "_define:" { return false } return super.canPerformAction(action,withSender: sender) } }
所有操作列表:
cut: copy: select: selectAll: paste: delete: _promptForReplace: _transliterateChinese: _showTextStyleOptions: _define: _addShortcut: _accessibilitySpeak: _accessibilitySpeakLanguageSelection: _accessibilityPauseSpeaking: makeTextWritingDirectionRightToLeft: makeTextWritingDirectionLeftToRight: