我想在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];
}
}
但我无法创建自定义菜单,请指导我。
答案 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