IOS - is it possible to make a UILabel subclass object become the first responder?

Is there any way to do this?

I tried to put the following in a subclass:

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

But when I sent the getFirstResponder message to the object, it still was not the first responder.

+5
source share
3 answers

Yes it is possible. You should:

  • Cancel becomeFirstResponderto return YES.

  • Set userInteractionEnabledto YES.

  • Add UITapGestureRecognizerto handle cranes.

  • Call becomeFirstResponderfrom handler.

You can even override inputViewto get an input control at the bottom of the screen. Otherwise there will be nothing.

+1

, ( , ) : becomeFirstResponder, YES? userInteractionEnabled YES?

UIButton UILabel...

0

.

UILabel :

- (BOOL)canBecomeFirstResponder {
        return YES;
    }

    - (BOOL)canPerformAction:(SEL)action
                  withSender:(id)sender
    {
        return (action == @selector(copy:));
    }

    - (void)copy:(id)sender {
        [[UIPasteboard generalPasteboard] setString:self.text];
    }

, .

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressDetected:)];
[_messageLabel addGestureRecognizer:longPress];

, UIMenuController :

- (void)LongPressDetected:(UILongPressGestureRecognizer*)gesture {

        [_messageLabel becomeFirstResponder];
        [[UIMenuController sharedMenuController] setTargetRect:_messageLabel.bounds inView:_messageLabel];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:NO];
    }
0

All Articles