我有一个UITextField
,它只显示数字值(0-9,。, - )。当用户选择文本字段的内容时,出现具有“复制”,“粘贴”和“定义”的菜单。由于文本字段只显示数字值,因此我不想显示“定义”选项。如何禁用UITextField
中的字典“define”选项?在UITextField中禁用“定义”
编辑: 我已经解决了这一点,下面贴
我有一个UITextField
,它只显示数字值(0-9,。, - )。当用户选择文本字段的内容时,出现具有“复制”,“粘贴”和“定义”的菜单。由于文本字段只显示数字值,因此我不想显示“定义”选项。如何禁用UITextField
中的字典“define”选项?在UITextField中禁用“定义”
编辑: 我已经解决了这一点,下面贴
解决试试这个:
UITextField* textField = //...;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
我将“校正”属性设置为“否”,并在代码中尝试了上述建议,但仍然获得了“定义”菜单选项。 – amirfl
你可以分享你设置属性为“否”的代码吗? – Stunner
它在xib中。我也试过你的建议,并直接在代码中将autocorrectionType设置为UITextAutocorrectionTypeNo。我使用了: UITextField * unitValue =(UITextField *)[cell viewWithTag:1001]; unitValue。autocorrectionType = UITextAutocorrectionTypeNo; – amirfl
在问题留言区域的解决方案并没有为我工作程序(iOS 8),我得到误差:
action == @selector(defineSelection:)
我能够消除由specifing的选项,我从编辑菜单中的“定义”要在菜单中包括:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(copy:) ||
action == @selector(selectAll:)) {
return true;
}
return false;
}
更完整的答案为:How to disable copy paste option from UITextField programmatically (谢谢哔叽-K)
斯威夫特 - 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:
非常感谢您列出所有可用的操作。在iOS 9.3中使用Obj-C进行测试 – Laszlo
不客气! :) –
东西[这里](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInputTraits_Protocol/Reference/UITextInputTraits。 HTML#// apple_ref/OCC/INTF/UITextInputTraits)? –
我找不到解决方案。任何细节? – amirfl
我不明白为什么这个问题“被视为不是真正的问题”。无论如何,我找到了解决方案。子类UITextField和overided“ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender”。通过将“否”返回给if(action == @selector(defineSelection :)),“定义”选项已从UIMenuController中删除。 – amirfl