5

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

enter image description here

Can anyone please suggest me how to remove this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Mili Shah
  • 1,456
  • 13
  • 21

3 Answers3

10

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.

Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
Fernando Mazzon
  • 3,562
  • 1
  • 19
  • 21
  • look up is not removed – Mili Shah Jun 02 '17 at 13:10
  • Tried this too. Lookup is not removed. It explains in the docs that other components can say true which will override what is done here. https://developer.apple.com/documentation/uikit/uiresponder/1621105-canperformaction – Jeff Muir Feb 13 '18 at 23:35
  • 1
    For removing lookup, you need to remove (define) action == Selector(("_define:")) – Neelam Verma Jun 05 '18 at 17:09
2
// Make sure 
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(select(_:))
    {
        return true
    } else {
        return false
    }
}
karthik
  • 159
  • 9
2

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
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183