How does iBooks do this?

妖精的绣舞 提交于 2019-12-20 10:57:14

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3197162/how-does-ibooks-do-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!