如何在UITEXTVIEW中禁用共享和查找

发布于 2025-02-04 18:20:56 字数 1543 浏览 2 评论 0原文

我试图隐藏文本菜单,除了复制和两个原始菜单。

我使用以下代码来抑制几乎所有内容...但是不知何故,我无法隐藏查找share(以及spell)和会说,添加了6月10日)。

  override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if  action == Selector(("_lookup:")) ||
        action == Selector(("_share:"))
    {
        return false
    } else  if
        action == #selector(UIResponderStandardEditActions.copy(_:)) ||
            action == #selector(copyAll(_:))  ||
            action == #selector(lookUpWord (_:))
    {
        return true
    } else {
    
    return false
    }
}

我调试了代码,并目睹了当查找共享进入if-Statement时,该函数确实会返回false。但是,以某种方式没有在菜单上反映出来。

我如何确保禁用查找,<代码>共享,spellspeak

-----更新----

修改为扩展 但是某种程度上仍然无法按照我想要的方式工作。菜单在此扩展中没有反映代码...

   extension UITextView {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if  action == Selector(("_lookup:")) ||
                action == Selector(("_share:"))
        {
            return false
        } else  if
            action == #selector(UIResponderStandardEditActions.copy(_:)) ||
                action == #selector(ViewController.copyAll(_:))  ||
                action == #selector(ViewController.lookUpWord (_:))
        {
            return true
        } else {
            
            return false
        }
    }
}

I am trying to hide the text menus except for copy and two original menus.

I used the following code to suppress pretty much everything... but somehow, I can not hide look up and share (as well as Spell and Speak, added Jun 10).

  override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if  action == Selector(("_lookup:")) ||
        action == Selector(("_share:"))
    {
        return false
    } else  if
        action == #selector(UIResponderStandardEditActions.copy(_:)) ||
            action == #selector(copyAll(_:))  ||
            action == #selector(lookUpWord (_:))
    {
        return true
    } else {
    
    return false
    }
}

I debugged the code and witnessed that the function does return false when lookup and share went into the if-statement. But, somehow this is not reflected on the menu.

How can I make sure to disable look up, share, Spell, and Speak?

----- updated ----

Modified as extension
But somehow still not working in the way I want. The menu does not reflect the code in this extension...

   extension UITextView {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if  action == Selector(("_lookup:")) ||
                action == Selector(("_share:"))
        {
            return false
        } else  if
            action == #selector(UIResponderStandardEditActions.copy(_:)) ||
                action == #selector(ViewController.copyAll(_:))  ||
                action == #selector(ViewController.lookUpWord (_:))
        {
            return true
        } else {
            
            return false
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2025-02-11 18:20:56
  1. 您可以使用所需的自定义实现来创建UITEXTVIEW的子类。

  2. 要禁用查找,您需要使用selector((“ _define:”))

这是一个禁用共享&amp;的示例。 查找选项:

class CustomTextView: UITextView {
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        let canPerformAction = super.canPerformAction(action, withSender: sender)
        
        let shareAction = NSSelectorFromString("_share:")
        let lookUpAction = NSSelectorFromString("_define:")
        
        if canPerformAction && action == shareAction {
            // Set true/false based on your requirement
            return false
        }
        
        if canPerformAction && action == lookUpAction {
            return false
        }
        
        return canPerformAction
    }
}

并将其用作:

class YourViewController: UIViewController {
    
    @IBOutlet weak var yourTextView: CustomTextView!
}

编辑:

,如果要找到特定操作的选择器,请尝试以下方式:

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

选择行动将打印相关的动作:

textview :: canperformaction:_accessibilityspeakLanguagesElection:
textview :: canperformaction:_accessibilitypausespeaking:
textView :: canperformaction:复制:

或者您可以放置​​一个断点,然后查看所呼叫的“动作”。

  1. You can create subclass of UITextView with the custom implementation you want.

  2. To disable lookup, you need to use Selector (("_define:")).

Here is an example which disables Share & Lookup option :

class CustomTextView: UITextView {
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        let canPerformAction = super.canPerformAction(action, withSender: sender)
        
        let shareAction = NSSelectorFromString("_share:")
        let lookUpAction = NSSelectorFromString("_define:")
        
        if canPerformAction && action == shareAction {
            // Set true/false based on your requirement
            return false
        }
        
        if canPerformAction && action == lookUpAction {
            return false
        }
        
        return canPerformAction
    }
}

And use this as:

class YourViewController: UIViewController {
    
    @IBOutlet weak var yourTextView: CustomTextView!
}

Edit:

And if you want to find the Selector for the Specific actions, Try this way:

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

Select the action and this will print the associated action:

TextView::canPerformAction:_accessibilitySpeakLanguageSelection:
TextView::canPerformAction: _accessibilityPauseSpeaking:
TextView::canPerformAction: copy:

Or you can place a breakpoint and see what you're getting called with for "action".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文