添加动态自定义 UIMenuItem 到 Copy &在显示之前粘贴菜单

发布于 2024-09-26 23:36:48 字数 781 浏览 2 评论 0原文

我已经成功地将自定义 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 技术交流群。

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

发布评论

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

评论(2

蓝戈者 2024-10-03 23:36:48

在启动时的某个地方:

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObject:testMenuItem];
[testMenuItem release];

在您的 UITextViewUITextField 子类中:

@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(test:)) {
        // Return YES only if it's possible to perform the action at this time
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
- (void)test:(id)sender {
    // Perform the action here
}
@end

At startup somewhere:

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObject:testMenuItem];
[testMenuItem release];

And in your UITextView or UITextField subclass:

@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(test:)) {
        // Return YES only if it's possible to perform the action at this time
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
- (void)test:(id)sender {
    // Perform the action here
}
@end
方圜几里 2024-10-03 23:36:48

如果问题仍然相关,那么您可以使用 UIMenuControllerWillShowMenuNotificationUIMenuControllerDidShowMenuNotification 通知。
请参阅此处的文档

代码示例:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowMenu:) name:UIMenuControllerWillShowMenuNotification object:nil];

If the question is still relevant then you could use the UIMenuControllerWillShowMenuNotification or the UIMenuControllerDidShowMenuNotification notification.
See the documentation here.

Code sample:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowMenu:) name:UIMenuControllerWillShowMenuNotification object:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文