UIWebView中的自定义菜单控件

时间:2012-02-22 11:40:20

标签: 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];
    }

}

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

2 个答案:

答案 0 :(得分:8)

扩展系统编辑菜单

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

的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];
}

答案 1 :(得分:1)

您需要创建UIWebView的子类,请在WWDC 2011视频中观看:Rich Text Editing in Safari on iOS