UIWebView中的自定义菜单控件

aar*_*arg 2 objective-c

我想通过使用在UIWebView中创建自定义菜单控件

- (void) setUpCustomMenu 
{

    Class cls1 = NSClassFromString(@"UIMenuController");
    Class cls2 = NSClassFromString(@"UIMenuItem");

    if (cls1 && cls2)

    if ([UIMenuController instancesRespondToSelector:@selector(setMenuItems:)]) 
    {
        UIMenuItem* item1 = [[UIMenuItem alloc] initWithTitle:@"My Menu Item" action:@selector(myMenuAction:)];
        [UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects:item1, nil];;
        [item1 release];
    }

}
Run Code Online (Sandbox Code Playgroud)

但我无法创建自定义菜单,请指导我.

Pea*_*kJi 8

扩展系统编辑菜单

NSMutableArray *extraItems = [[NSMutableArray alloc] init];
UIMenuItem *boldItem = [[UIMenuItem alloc] initWithTitle:@”Bold” 
                                                  action:@selector(bold:)];
[extraItems addObject:boldItem];
[[UIMenuController sharedMenuController].menuItems = extraItems;
Run Code Online (Sandbox Code Playgroud)

UIWebView扩展系统编辑菜单

// For your UIWebView subclass:
 - (void)bold:(id)sender {
    [self stringByEvaluatingJavaScript:@”document.execCommand(‘Bold’)];
}

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(bold:))
      return YES;
    return [super canPerformAction:action
                        withSender:sender];
}
Run Code Online (Sandbox Code Playgroud)

  • - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { BOOL retValue = NO; //ColorTile *theTile = [self colorTileForOrigin:currentSelection]; if (action == @selector(paste:) ) retValue = NO; else if ( action == @selector(cut:) || action == @selector(copy:) ) retValue = NO; else retValue = [super canPerformAction:action withSender:sender]; 返回值;} (2认同)