6

Is there any way to do this?

I tried putting the following into the subclass:

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

But when I sent the object a becomeFirstResponder message, it still didn't become the first responder.

Zorayr
  • 23,770
  • 8
  • 136
  • 129
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • UILabel becoming a first responder? What do you want it to respond? – kennytm Nov 03 '10 at 17:18
  • 3
    It inherits from UIResponder so it does have the capability. – Ben Nov 03 '10 at 17:21
  • I'm using someone's library that does stuff when an object with certain properties becomes the first responder. It also sends messages to the first responder. I happen to want to tie all this in to my label. – William Jockusch Nov 03 '10 at 17:44

3 Answers3

2

Yes, it is possible. You should:

  1. Override becomeFirstResponder to return YES.

  2. Set userInteractionEnabled to YES.

  3. Add a UITapGestureRecognizer to handle taps.

  4. Call becomeFirstResponder from the tap handler.

You can even override inputView to get a input control at the bottom of the screen. Otherwise there will be nothing.

Ivan Nikitin
  • 3,578
  • 27
  • 39
  • What I should return in `inputView` to see the keyboard on the screen? – derpoliuk Apr 01 '14 at 07:58
  • @derpoliuk thanks for the comment. My fault, didn't check that UIKeyboard class is a private API. – Ivan Nikitin Apr 01 '14 at 08:50
  • So there is no way to get keyboard on the screen after `UILabel` becomes first responder? I'm trying to mimic Viber or Telegram behaviour, when the keyboard is on the screen and long press on the chat message shows `UIMenuController`. – derpoliuk Apr 01 '14 at 09:25
  • Not sure that your UILabel should become first responder to be able to display a UIMenuController. Are you sure your UITextField responsible for the keyboard on screen is in the same UIViewController? – Ivan Nikitin Apr 01 '14 at 12:05
  • I call `[UILabel becomeFirstResponder]` because of `UIMenuController`. In this case `UILabel` able to respond for `copy:` message from `UIMenuController`. If I call `[UITextView becomeFirstResonder]` `UILabel` no longer able to respond to the `copy:`. Thanx for response. – derpoliuk Apr 01 '14 at 15:52
0

This worked for me.

Subclass UILabel and override these methods:

- (BOOL)canBecomeFirstResponder {
        return YES;
    }

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

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

Add Gesture to your label to detect tap.

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

Handle gesture event, present a UIMenuController with the desired frame:

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

        [_messageLabel becomeFirstResponder];
        [[UIMenuController sharedMenuController] setTargetRect:_messageLabel.bounds inView:_messageLabel];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:NO];
    }
Lirik
  • 3,167
  • 1
  • 30
  • 31
0

I have no definite answer, just a few (maybe crazy) ideas: Have you also tried to override becomeFirstResponder to return YES? Does it have userInteractionEnabled also set to YES?

Otherwise, make it a custom UIButton instead of a custom UILabel...

F'x
  • 12,105
  • 7
  • 71
  • 123