iBooks如何做到这一点? [英] How does iBooks do this?
问题描述
我目前正在uiwebview中显示文字。但是,我想允许用户选择文本并使用所选文本执行某些操作(google it)。 Apple已经使用iBooks完成了这类工作。单击单词时,您可以选择在字典中查找单词。如何使用Webview做同样的事情?
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似乎是我需要看的东西。但我找不到任何有关如何执行此操作的示例代码。我是iPhone开发的新手请帮忙。
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.
推荐答案
从iOS 3.2开始你可以使用 UIMenuController 添加额外的 UIMenuItems 到在UIWebView中选择文本时出现的系统菜单。这是一个简单的例子:
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描述了这是如何工作的在添加自定义编辑菜单项 设备功能编程指南部分。
Apple describes how this works in the Adding Custom Edit Menu Items section of the Device Features Programming Guide.
这篇关于iBooks如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!