我可以删除剪切、复制、粘贴、选择、全选
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(selectAll(_:)) || action == #selector(cut(_:))
{
return false
}
return super.canPerformAction(action, withSender: sender)
}
但我无法删除查找和分享
谁能建议我如何删除它?
最佳答案
如果您真的不想允许任何操作,为什么要专门检查每个操作?只需在您的方法中返回 false 即可。否则,您可以放置一个断点并查看您为“操作”调用的内容并为其添加另一个验证
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
print("BlahTextView::canPerformAction: \(action)")
return false
}
结果,您要删除的 2 个突出显示:
BlahTextView::canPerformAction: cut: BlahTextView::canPerformAction: copy: BlahTextView::canPerformAction: select: BlahTextView::canPerformAction: selectAll: BlahTextView::canPerformAction: paste: BlahTextView::canPerformAction: delete: BlahTextView::canPerformAction: _promptForReplace: BlahTextView::canPerformAction: _transliterateChinese: BlahTextView::canPerformAction: _showTextStyleOptions: BlahTextView::canPerformAction: _lookup: BlahTextView::canPerformAction: _define: BlahTextView::canPerformAction: _addShortcut: BlahTextView::canPerformAction: _accessibilitySpeak: BlahTextView::canPerformAction: _accessibilitySpeakLanguageSelection: BlahTextView::canPerformAction: _accessibilityPauseSpeaking: BlahTextView::canPerformAction: _share: BlahTextView::canPerformAction: makeTextWritingDirectionRightToLeft: BlahTextView::canPerformAction: makeTextWritingDirectionLeftToRight:
然后你可以做:
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) ||
action == #selector(paste(_:)) ||
action == #selector(select(_:)) ||
action == #selector(selectAll(_:)) ||
action == #selector(cut(_:)) ||
action == Selector(("_lookup:")) ||
action == Selector(("_share:")) ||
action == Selector(("_define:"))
{
return false
}
return super.canPerformAction(action, withSender: sender)
}
需要备用语法,因为这些方法未公开声明,如果您使用 #selector(share(:)) 示例,您将收到编译器错误。
查找 - 请使用 ((_define:)) 谢谢。
关于swift - 如何从 Swift 3 中的 TextView 中删除查找和共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329280/