NKorotkov NKorotkov - 2 months ago 27
iOS Question

TTTAttributedLabel link is styled and has UILongPressGestureRecognizer but doesn't have UITapGestureRecognizer

In my project I've added TTTAttributedLabel to a view in IB and added a link to it.

I've looked through similar questions on SO (like this and this), but no luck.

It displays properly, link is styled as expected. But

is not being called when I try to tap on the link. But if I use a long press instead
is fired as expected. So I'm sure the delegate is working. It seems like label doesn't even have a tapGestureRecognizer

(lldb) po label.gestureRecognizers
<__NSSingleObjectArrayI 0x60800000ee60>(
<UILongPressGestureRecognizer: 0x7f88f6c5d480; state = Began; view = <TTTAttributedLabel 0x7f88f6f8ada0>; target= <(action=longPressGestureDidFire:, target=<TTTAttributedLabel 0x7f88f6f8ada0>)>>

Here's the code:

RFStaticAttributedLabelFormView *labelView = [RFStaticAttributedLabelFormView new];
labelView.staticLabel.userInteractionEnabled = YES;
labelView.staticLabel.delegate = self;
NSString *text = RFLocalized(@"TXT_FINES_SEARCH_FNS");
[labelView.staticLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:nil];
NSRange r = [text rangeOfString:RFLocalized(@"TXT_FINES_SEARCH_FNS_LINK")];
[labelView.staticLabel addLinkToURL:[NSURL URLWithString:@"http://www.google.com"] withRange:r];

RFStaticAttributedLabelFormView is a view, that is instantiated from XIB.

Not sure where to go from here.


I've created a fresh new project with a single TTTAttributedLabel. After I hooked up delegate methods everything started to work as expected, including a single tap. I've checked gesture recognisers on the label - and it turns out it doesn't have a tap recogniser either, only a long press one.

After that I reviewed my original project and I've found the origin of the problem. The main view in the hierarchy had another tap gesture recogniser that was intercepting all tap events. I've added this line:

tap.cancelsTouchesInView = NO;

And links in TTTAttributedLabel started to work as it should. Hope this answer helps someone with the similar problem.