【问题标题】:How to remove lookup & share from textview in Swift 3如何在 Swift 3 中从 textview 中删除查找和共享
【发布时间】:2017-11-03 21:15:36
【问题描述】:

我可以使用这个删除剪切、复制、粘贴、选择、全选

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)
}

但我无法删除查找和分享

谁能建议我如何删除它?

【问题讨论】:

    标签: swift uitextview


    【解决方案1】:

    如果您真的不想允许任何操作,为什么要专门检查每个操作?只需在您的方法中返回 false 。否则,您可以放置​​一个断点并查看您为“操作”而调用的内容并为其添加另一个验证

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        print("BlahTextView::canPerformAction: \(action)")
        return false
    }
    

    结果,您要删除的 2 突出显示:

    BlahTextView::canPerformAction: 剪切: BlahTextView::canPerformAction: 复制: BlahTextView::canPerformAction: 选择: BlahTextView::canPerformAction: 全选: BlahTextView::canPerformAction: 粘贴: BlahTextView::canPerformAction: 删除: 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:)) 谢谢。

    【讨论】:

    • 查找未删除
    • 也试过了。不会删除查找。它在文档中解释说,其他组件可以说是真的,这将覆盖这里所做的事情。 developer.apple.com/documentation/uikit/uiresponder/…
    • 要移除lookup,需要移除(define)action == Selector(("_define:"))
    【解决方案2】:
    // Make sure 
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(select(_:))
        {
            return true
        } else {
            return false
        }
    }
    

    【讨论】:

    • if action == #selector(select(_:)) { return true }else{ false }
    • 使用这个条件
    • 请使用正确的格式,让每个人都能理解。
    【解决方案3】:

    正如您在对一个答案的评论中提到的那样,您只想启用select,那么为什么不比较select 并为它返回true,在其他情况下返回false

    override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(select(_:)) {
            return true
        }
        return false
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多