How does iBooks do this?

后端 未结 2 1580
Happy的楠姐
Happy的楠姐 2021-02-04 20:44

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 th

相关标签:
2条回答
  • 2021-02-04 21:27

    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.

    0 讨论(0)
  • 2021-02-04 21:41

    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.

    0 讨论(0)
提交回复
热议问题