iBooks 是如何做到这一点的?

发布于 2024-09-08 16:40:50 字数 210 浏览 3 评论 0原文

我目前正在 uiwebview 中显示文本。但是,我想允许用户选择文本并使用所选文本执行某些操作(谷歌搜索)。苹果公司已经在 iBooks 上做了类似的事情。当您单击某个单词时,您可以选择在字典中查找该单词。我怎样才能用 Webview 做同样的事情?

UIMenuController 似乎是我需要看的。但我找不到任何有关如何执行此操作的示例代码。我是 iPhone 开发新手,请帮忙。

I am currently displaying text in a uiwebview. However, I would like to allow a user to select text and perform some action with the selected text (google it). Apple has done this sort of thing with iBooks. When you click on a word you can choose to look up the word in a dictionary. How can I do the same thing with Webview?

UIMenuController seems to be what I need to look at. But I couldn't find any sample code on how to do this. I'm new to iPhone development please help.

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

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

发布评论

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

评论(2

孤寂小茶 2024-09-15 16:40:50

从 iOS 3.2 开始,您可以使用 UIMenuController 来添加额外的 UIMenuItems 到系统菜单在 UIWebView 中选择文本时出现。这是一个简单的示例:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIMenuItem *defineItem = [[[UIMenuItem alloc] initWithTitle:@"Define" action:@selector(defineSelection:)] autorelease];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:defineItem]];
}

- (void)defineSelection:(id)sender {
    NSString *selection = [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
    // Do something with the selection
}

Apple 在 添加自定义编辑菜单项部分。

Starting in iOS 3.2 you can use UIMenuController to add additional UIMenuItems to the system menu that appears when selecting text in a UIWebView. Here's a simple example:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIMenuItem *defineItem = [[[UIMenuItem alloc] initWithTitle:@"Define" action:@selector(defineSelection:)] autorelease];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:defineItem]];
}

- (void)defineSelection:(id)sender {
    NSString *selection = [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
    // Do something with the selection
}

Apple describes how this works in the Adding Custom Edit Menu Items section of the Device Features Programming Guide.

丶视觉 2024-09-15 16:40:50

This recent blog post from a developer would seem to suggest that at least in the iPad iOS branch, some iBooks functionality is based on private APIs.

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