无法在UITextView的UIMenuController中禁用默认的UIMenuItems [英] Cannot Disable Default UIMenuItems in UIMenuController in UITextView
问题描述
我正在尝试配置UIMenuController的菜单项以实现类似于Medium的iOS功能的功能:
I'm trying to configure UIMenuController's menu items for a functionality similar to Medium's iOS feature:
有许多线程专用于此特定任务,但是尽管有成千上万的视图和不同的结果,包括它不能为足够多的人工作...似乎没有解决方案对于UITextView始终有效.
There are a variety of threads devoted to this specific task, but despite tens of thousands of views and varied results, including it not working for a significant enough number of people... it doesn't seem like there is a solution that works consistently for UITextView.
我已经能够添加一个自定义菜单选项"printToConsole",但是我无法禁用Apple的标准菜单项,例如剪切,复制,粘贴, B I U,等等:
I have been able to add a custom menu option "printToConsole", but I can't disable Apple's standard menu items like cut, copy, paste, B I U, etc:
共识似乎是我应该重写canPerformAction以禁用这些默认菜单项,但这似乎不起作用:
The consensus seems to be that I should override canPerformAction to disable these default menu items as such, but that doesn't seem to be working:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
print("canPerformAction being called")
if action == #selector(cut(_:)) {
return false
}
if action == #selector(copy(_:)) {
return false
}
if action == #selector(select(_:)) {
return false
}
if action == #selector(paste(_:)) {
return false
}
if action == #selector(replacementObject(for:)) {
return false
}
if action == #selector(selectAll(_:)) {
return false
}
if action == #selector(printToConsole) {
return true
}
return super.canPerformAction(action, withSender: sender)
}
这是我相关代码的其余部分:
This is the remainder of my relevant code:
func addCustomMenu() {
let consolePrintAction = UIMenuItem(title: "Print To Console", action: #selector(printToConsole))
UIMenuController.shared.menuItems = [consolePrintAction]
UIMenuController.shared.update()
}
@objc func printToConsole() {
if let range = articleTextView.selectedTextRange, let selectedText = articleTextView.text(in: range) {
print(selectedText)
}
}
在我的viewDidLoad中:
And in my viewDidLoad:
articleTextView.delegate = self
addCustomMenu()
我也将viewController设置为也符合UITextViewDelegate.有人建议,如果您只是将TextView子类化,那么它将以某种方式起作用.我一直无法解决这个问题,因此,如果这确实是答案,那么有人可以提供示例吗?
I've set my viewController to conform to UITextViewDelegate as well. Some are suggesting that if you simply subclass the TextView this will work somehow. I haven't been able to get that to work, so if that is truly the answer, can someone provide an example?
同样,我知道这似乎是重复的,但是上述解决方案似乎已停止使用iOS更新.
Again, I know this may seem like a duplicate, but the above solution appears to have stopped working with an update of iOS.
谢谢.
推荐答案
要赞扬@gaurav对此问题的回答,这一定使我摆脱了对SO的追捕:https://stackoverflow.com/a/46470592/7134142
Going to give credit to @gaurav for his answer on this one, which must have escaped me in my hunt on SO: https://stackoverflow.com/a/46470592/7134142
关键代码段是这样,它扩展了UITextView,而不是对其进行子类化:
The key piece of code is this, which is extending UITextView, rather than subclassing it:
extension UITextView {
open override func canPerformAction(_ action: Selector, withSender
sender: Any?) -> Bool {
return false
}
在我的视图控制器中覆盖canPerformAction是不必要的,并且上面的代码仍然允许您添加自定义菜单项.这就是我最终得到的:
Overriding canPerformAction in my view controller was unnecessary, and the above code still allows you to add your custom menu items. This is what I ended up with:
这篇关于无法在UITextView的UIMenuController中禁用默认的UIMenuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!