Search code examples
iosipaduilabel

How to get UILabel to respond to tap?


I have discovered that I can create UILabel much faster than UITextField and I plan to use UILabel most of the time for my data display app.

To make a long story short though, I wish to let the user tap on a UILabel and have my callback respond to that. Is that possible?

Thanks.


Solution

  • You can add a UITapGestureRecognizer instance to your UILabel.

    For example:

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [myLabel addGestureRecognizer:tapGestureRecognizer];
    myLabel.userInteractionEnabled = YES;