Is there a touch method for UILabel?

22,052

Solution 1

You could use a gesture recognizer:

- (void)someSetupMethod {
    // ...
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didTapLabelWithGesture:)];
    [label addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
    // ...
}

Solution 2

By default, UILabel isn't configured to accept touch input. However, if you use a UIButton instead and set it to have a custom appearance, you can make it look like a (single-line) label and have it respond to touch events.

Solution 3

You can subclass it and override the touch methods. You probably want to override touchesEnded:withEvent:.

Or just use a UIButton.

Share:
22,052
Matoe
Author by

Matoe

Learner of Objective-C. :)

Updated on July 09, 2022

Comments

  • Matoe
    Matoe almost 2 years

    I'd like to do an action if someone touches a predeclared UILabel, something like:

    if (label is touched) {
        my actions;
    }
    

    Is there a method/way to do that?

  • Cameron
    Cameron over 6 years
    This was really easy to do! Thanks!
  • MahajanSagar
    MahajanSagar about 6 years
    If I have multiple label's, Then how to recognise which label is tapped ???