How to get UILabel to respond to tap?

Happy picture Happy · Aug 23, 2011 · Viewed 72.2k times · Source

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.

Answer

pythonquick picture pythonquick · Aug 23, 2011

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;