Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a touch method for UILabel?

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?

like image 910
Matoe Avatar asked Jun 12 '11 21:06

Matoe


People also ask

How do I make a UILabel clickable?

To make UILabel clickable you will need to enable user interaction for it. To enable user interaction for UILabel simply set the isUserInteractionEnabled property to true.

How do I know if my UILabel is truncated Swift?

You can calculate the width of the string and see if the width is greater than label width. Save this answer.


3 Answers

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 {
    // ...
}
like image 122
Wilbur Vandrsmith Avatar answered Sep 24 '22 07:09

Wilbur Vandrsmith


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.

like image 33
Jonathan Grynspan Avatar answered Sep 26 '22 07:09

Jonathan Grynspan


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

Or just use a UIButton.

like image 28
fzwo Avatar answered Sep 22 '22 07:09

fzwo