添加动态自定义 UIMenuItem 到 Copy &在显示之前粘贴菜单
我已经成功地将自定义 UIMenuItem 添加到 Copy & 中。将菜单粘贴到我的 iPhone 应用程序中,甚至将 UITextView 子类化以摆脱标准菜单项。但是,我需要做的是以某种方式捕获菜单将在实际发生之前显示的事实,并将插入点处的单词添加到菜单中。
例如,如果 UITextView 中的文本是“This is a test.”,并且该人触摸了单词“is”,则它会将该单词作为 UIMenuItem 添加到 UIMenuController 中。
重要的是,菜单仅在触摸后立即显示该单词。菜单的下一次调用将显示下一个触摸的单词,等等。触摸菜单中的单词将显示更多详细信息。我已经有代码可以根据 selectedRange 查找触摸的单词。我需要做的就是在菜单显示之前将该单词添加为 UIMenuItem 。另一个不太优雅的解决方案可能是允许用户触摸静态菜单项,然后根据触摸的单词添加并重新显示菜单,并提供不同的选项。
我希望有一种方法可以拦截 UIMenuController ,可能通过子类化它,以便我可以在气球显示之前到达插入点,但仍然能够对其进行更改,方法是更改菜单项列表。
有办法做到这一点吗?有人可以向我展示代码片段或向我指出一些可能对我有帮助的文档吗?谢谢。
我唯一的其他解决方案是以某种方式创建我自己的气球并以某种方式禁用复制和复制。粘贴菜单。我宁愿不必尝试。
I have successfully been able to add a custom UIMenuItem to the Copy & Paste menu in my iPhone app, and even subclassed UITextView to get rid of the standard menu items. However, what I need to do is to somehow capture the fact that the menu is going to be displayed before it actually happens, and add the word at the insertion point into the menu.
For example, if the text in the UITextView is "This is a test.", and the person touched the word "is", it would add that word as a UIMenuItem to the UIMenuController.
It is important that the menu show the word only directly after it has been touched. The next invocation of the menu would show the next word touched, etc. Touching the word in the menu would then show more detail. I already have code that finds the word touched based on selectedRange. All I need to do is to add that word as a UIMenuItem before the menu displays. Another less elegant solution might be to allow the person to touch a static menu item that then adds and redisplays the menu, with different options, based on the word touched.
I am hoping that there is a way to intercept the UIMenuController, possibly by subclassing it, so that I can get to the insertion point before the balloon displays, but still able to effect a change to it, by changing the menu item list.
Is there a way to do this? Can anybody show me a code snippet or point me to some documentation that might help me? Thanks.
My only other solution is to somehow create my own balloon and somehow disable the Copy & Paste menu. I would rather not have to try that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在启动时的某个地方:
在您的
UITextView
或UITextField
子类中:At startup somewhere:
And in your
UITextView
orUITextField
subclass:如果问题仍然相关,那么您可以使用
UIMenuControllerWillShowMenuNotification
或UIMenuControllerDidShowMenuNotification
通知。请参阅此处的文档。
代码示例:
If the question is still relevant then you could use the
UIMenuControllerWillShowMenuNotification
or theUIMenuControllerDidShowMenuNotification
notification.See the documentation here.
Code sample: