English 中文(简体)
How does iBooks do this?
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签