问题
I can remove cut, copy, paste, select, select all using this
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)
}
But I can't remove lookup & Share
Can anyone please suggest me how to remove this?
回答1:
// Make sure
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(select(_:))
{
return true
} else {
return false
}
}
回答2:
If you really don't want to allow any actions, why do you check for each of them specifically? Just return false in your method. Otherwise, you can place a breakpoint and see what you're getting called with for "action" and add another validation for it
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
print("BlahTextView::canPerformAction: \(action)")
return false
}
And the result, with the 2 you want removed highlighted:
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:
And then you can do:
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)
}
The alternate syntax is required because those methods are not publicly declared and you'll get a compiler error if you use #selector(share(:)) for example.
for lookup - please use ((_define:)) Thanks.
回答3:
As you mentioned in comment of one answer that you want to just enabled select
then why don't you compare select
and return true
for it and false
in other case.
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(select(_:)) {
return true
}
return false
}
来源:https://stackoverflow.com/questions/44329280/how-to-remove-lookup-share-from-textview-in-swift-3