Question

I'm working on a very small editor in iOS. It only has one view, one button and a textView. When the button gets pressed, a custom UIMenuController pops up with 3 options: toggle bold, toggle italics and toggle cursive.

This is working very well, however, if I press the button when the UITextView is the first responder, it also shows the two default menu items, named 'select' and 'select all'.

I want to get rid of them but I'm not sure how to do this. This is the code that gets called when the button is pressed:

- (IBAction)settingsPressed:(id)sender
{
    UIMenuController *sharedController = [UIMenuController sharedMenuController];

    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:bold ? @"Bold off" : @"Bold on" action:@selector(toggleBold:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:italics ? @"Italics off" : @"Italics on" action:@selector(toggleCursive:)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:underline ? @"Underline off" : @"Underline on" action:@selector(toggleUnderline:)];

    NSArray *menuItems = @[menuItem1, menuItem2, menuItem3];

    CGRect drawRect = [sender convertRect:[sender bounds] toView:self.view];
    [sharedController setTargetRect:drawRect inView:self.view];

    [sharedController setMenuItems:menuItems];
    [sharedController setMenuVisible:YES animated:YES];

    [sharedController setMenuItems:nil];
}

Can anyone explain me how to do this?

Thanks!

Was it helpful?

Solution

Make a subclass of UITextView. In your subclass, override canPerformAction:withSender: to return NO if the action is @selector(select:) or @selector(selectAll:). For more information:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top