Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tapping URL doesn't work in TTTAttributedLabel while there is a UITapGestureRecognizer on its superview

There is container view and a UITapGestureRecognizer on it. And it also has a subview which kind of TTTAttributedLabel.

When I remove the gesture recognizer from the container view, the delegate method of TTTAttributedLabelDelegate

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url can be called.

When I add the gesture recognizer on container view. Only its action method gets called. The delegate method of TTTAttributedLabelDelegate won't be called.

Now I need the delegate method to be called when I tap on a link in TTTAttributedLabel, and action method to called when I tap on other area of container view.

Thanks.

like image 367
panyf07 Avatar asked Mar 12 '14 07:03

panyf07


2 Answers

use this gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch for detection your event.

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
      if ([touch.view isKindOfClass:[TTTAttributedLabel class]])
      {
           return FALSE;
      }
      else
      {

         return TRUE;
      }
}

you can also use if ([touch.view isKindOfClass:[UIControl class]]) for all UIControl e.g Button even detect with UIGestureRecognizer. hope this help's you

like image 150
Nitin Gohel Avatar answered Oct 17 '22 15:10

Nitin Gohel


I tried @Nitin's solution but it has a problem as @n00bProgrammer pointed out. Furthermore, user experience became very bad. I had to tap several times to fire tapGesture which is added on a superView.

Here you have a better solution. TTTAttributedLabel has a useful instance method like below.

- (BOOL)containslinkAtPoint:(CGPoint)point

This returns whether an NSTextCheckingResult is found at the given point.

So, use the following code snippet then only link added part of the text will be working as a tappable link and the rest of area will fire your tapGesture as you desired. User experience is excellent as well.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([yourTTTAttributedLabel containslinkAtPoint:[touch locationInView:yourTTTAttributedLabel]])
        return FALSE;
    else
        return TRUE;    
}
like image 8
Vincent Gigandet Avatar answered Oct 17 '22 15:10

Vincent Gigandet