views:

50

answers:

1

I have successfully been able to add a custom UIMenuItem to the Copy & Paste menu in my iPhone app, and even subclassed UITextView to get rid of the standard menu items. However, what I need to do is to somehow capture the fact that the menu is going to be displayed before it actually happens, and add the word at the insertion point into the menu.

For example, if the text in the UITextView is "This is a test.", and the person touched the word "is", it would add that word as a UIMenuItem to the UIMenuController.

It is important that the menu show the word only directly after it has been touched. The next invocation of the menu would show the next word touched, etc. Touching the word in the menu would then show more detail. I already have code that finds the word touched based on selectedRange. All I need to do is to add that word as a UIMenuItem before the menu displays. Another less elegant solution might be to allow the person to touch a static menu item that then adds and redisplays the menu, with different options, based on the word touched.

I am hoping that there is a way to intercept the UIMenuController, possibly by subclassing it, so that I can get to the insertion point before the balloon displays, but still able to effect a change to it, by changing the menu item list.

Is there a way to do this? Can anybody show me a code snippet or point me to some documentation that might help me? Thanks.

My only other solution is to somehow create my own balloon and somehow disable the Copy & Paste menu. I would rather not have to try that.

A: 

At startup somewhere:

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObject:testMenuItem];
[testMenuItem release];

And in your UITextView or UITextField subclass:

@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(test:)) {
        // Return YES only if it's possible to perform the action at this time
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
- (void)test:(id)sender {
    // Perform the action here
}
@end
rpetrich
rpetrich - That is the standard way to add a custom UIMenuItem. I already have that code in place. However, I can't intercept the touch to change the menu item at runtime, based on the selectedRange of the touch.
Implement `textViewDidChangeSelection:` in you `UITextViewDelegate`; inside update the `title` property of the `UIMenuItem` to represent the title that the button should have given the selection. Alternatively, it may be possible to update the menu item's title inside `canPerformAction:withSender:`, but I wouldn't count on that.
rpetrich