iOS TTTAttributedLabel Delegate didSelectLinkWithURL not getting called

前端 未结 4 1447
灰色年华
灰色年华 2021-02-07 17:45

I\'m having problems setting up the TTTAttributedLabel within my project.

I have set the protocol delegate in my header file

@interface TwitterFeedContr         


        
相关标签:
4条回答
  • 2021-02-07 18:21

    Solved! The problem was the "User Interaction Enabled" Checkbox in the CollectionViewCell... It was disabled! it took me 4 hours to figured it out!! Thank you anyway!

    0 讨论(0)
  • 2021-02-07 18:27

    The UILabel userInteractionEnabled seems to be disabled by default, so unless you explicitly enable it, it won't work. That was my problem.

    0 讨论(0)
  • 2021-02-07 18:31

    One possible cause for the same problem (similar to Joeran's answer) is if you have a custom UITapGestureRecognizer on the view that keeps the TTTAttributedLabel's tap gesture from being called. In my case I needed to block the tap gesture's action from being called if the tap was on a link, so cancelsTouchesInView was not enough.

    My solution: blocking the tap gesture from being recognized at all.

    In viewDidLoad:

    tapGesture.delegate = self
    

    And below the actual implementation of my class:

    extension MyView: UIGestureRecognizerDelegate {
        override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            if gestureRecognizer == self.tapGesture {
                let view = gestureRecognizer.view
                let location = gestureRecognizer.location(in: view)
                let subview = view?.hitTest(location, with: nil)
                // test if the tap was in a TTTAttributedLabel AND on a link
                if let ttt = subview as? TTTAttributedLabel, ttt.link(at: gestureRecognizer.location(in: ttt)) != nil {
                    return false
                }
            }
            //else
            return true
        }
    }
    
    0 讨论(0)
  • 2021-02-07 18:34

    I understand it is not your case, but this is for everyone who have the same problem as I had and stumble on this thread.

    I had an UITapGestureRecognizer on the same view as the TTTAttributedLabel. Because of the first one, the 'touchEnded' function of the TTTAttributedLabel wasn't called, which is responsible for handling clicked links.

    I solved the problem by adding the line: tapGestureRecognizer.cancelsTouchesInView = NO;

    0 讨论(0)
提交回复
热议问题