nevan king
nevan king

Reputation: 113747

Is there an interface to create the glossy cut/copy popup

Is there a way to create one of the black glossy popups, similar to the one that appears to copy and paste text. I just want to give some information, so the behaviour I want is closer to the way the keyboard popup works when you type a letter but the appearance I want is the copy/paste dialogue.

Are either of these open to the public, or do I have to create my own implementation?

Upvotes: 0

Views: 363

Answers (2)

nzeltzer
nzeltzer

Reputation: 521

You want to look at UIMenuItemController. The sample project "CopyPasteTile" provides an example of a custom implementation.

Second time this has come up this morning!

Here's some sample code for adding a custom menu item to a UIView subclass (it assumes you've added a long press gesture recognizer with the appropriate target action):

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(laughOutLoud:))
        return YES;
    return NO;
}

- (void)laughOutLoud:(id)sender {
    NSLog(@"LOL!");
}

- (void)gestureRecognizerDidPress:(UILongPressGestureRecognizer*)recognizer {
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        // Important: the view must become the first responder,
        // and implement the canBecomeFirstResponder method.
        [self becomeFirstResponder];
        UIMenuController *controller = [UIMenuController sharedMenuController];
        UIMenuItem *testItem1 = [[UIMenuItem alloc] initWithTitle:@"Laugh" action:@selector(laughOutLoud:)];
        [controller setMenuItems:[NSArray arrayWithObject:testItem1]];
        [controller update];
        // In real life, the target rect should represent the selection
        [controller setTargetRect:CGRectZero inView:self];
        [controller setMenuVisible:YES animated:YES];
    }
}

Upvotes: 0

Jasarien
Jasarien

Reputation: 58448

The copy/cut/paste UI is given for free with any of the text based UI controls in UIKit (UITextField, UITextView, etc.), but if you want to use a similary styled UI with other parts of your app you'll have to create your own.

Upvotes: 1

Related Questions