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?
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.
You can calculate the width of the string and see if the width is greater than label width. Save this answer.
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 {
// ...
}
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.
You can subclass it and override the touch methods. You probably want to override touchesEnded:withEvent:
.
Or just use a UIButton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With