How do I add a custom UIMenuItem to the UIMenuController in a UITextView?

13,461

Solution 1

So I ended up using the following with results that I wanted, I placed the following block in the viewDidLoad method of my view controller as Alex hinted to:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
    [menuItem release]; 
}

Then I added the following to the view controller to show the item conditionally when text is selected within the UITextView which I named "textView":

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(changeColor:)) {
        if (textView.selectedRange.length > 0) {
            return YES;
        }
    }
    return NO;
}

I chose to place the canPerformAction:withSender: method in the view controller instead of a custom UITextView class because this way the other options (e.g. copy, cut, paste, etc.) behave as they normally would since the method is called on every object up the responder chain.

Solution 2

I was only able to create a custom menu item that is ALWAYS present in the popup menu, by doing the following:

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

This is called in the viewDidLoad of the view controller.

I'm still struggling to make this only show my menu item conditionally(if, for instance, there is something selected), by intercepting the event that summons up the menu controller. So far I wasn't able to do this.

Share:
13,461

Related videos on Youtube

Kyle Zaragoza
Author by

Kyle Zaragoza

Updated on April 27, 2022

Comments

  • Kyle Zaragoza
    Kyle Zaragoza about 2 years

    I'm trying to add a menu item next to the "copy", "paste" items in a UITextView. I've created a subclass of UITextView and copied the example from apple's docs here:

    http://developer.apple.com/iphone/library/documentation/General/Conceptual/iPadProgrammingGuide/Text/Text.html#//apple_ref/doc/uid/TP40009370-CH8-SW28

    I simply created a UiTextView in IB and set its class to my CustomTextView class. Unfortunately this didn't work. Although, if I set my CustomTextView to a subclass of UIView, it works great. Any help here?

    I've also made a very simple example project of my situation here:

    apps.popsweet.com/TextViewTrial.zip

  • ingh.am
    ingh.am over 13 years
    +1 however when I do this the changeColor method points to my custom TableViewCell class and crashes because it's not existant. Can I point this to the view controller?
  • Paul Solt
    Paul Solt over 11 years
    You may need to make sure the UIWindow is the key window and to implement this method: - (BOOL)canBecomeFirstResponder { return YES; }