Programmatically perform actions on UILabel touchUpInside?

I have a bunch of UILabels that I add through code, and I want to perform a specific action for each if the user finger touches inside (like UIButton touchUpInside in IB). What is the best way to do this?

+4
source share
2 answers

The easiest way is to use UIButton instead of UILabel. A custom UIButton has no border and may look like a simple label, but handles event tracking for you.

Otherwise, you should get a UILabel and implement UIResponder calls to handle touches.

+17
source

You can use UITapGestureRecognizer to perform actions on label fields.

Set the number of taps and touches on the UITapGestureRecognizer.

[oneFingerTwoTaps setNumberOfTapsRequired:1]; [oneFingerTwoTaps setNumberOfTouchesRequired:1]; 

establish user interaction with the label field, for example

 labelField.userInteractionEnabled = YES; 

then add the target method on top

 UITapGestureRecognizer object. 
+5
source

Source: https://habr.com/ru/post/1315991/


All Articles