How does iBooks do this?

最后都变了- 提交于 2019-12-03 00:45:40

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.

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