iOS TTTAttributedLabel Delegate didSelectLinkWithURL not getting called
Asked Answered
G

4

13

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

I have set the protocol delegate in my header file

@interface TwitterFeedControlleriPad : UIViewController <TTTAttributedLabelDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateWaterfallLayout>

I have set the ViewController to it (is not nil, I checked already).

cell.tweetLabel.delegate = self;

It's showing all the links right in the label but when I tap them it's not calling the function.

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url

I've already used it before with success and it's exactly the same code!! It's driving me crazy!

Grimona answered 22/7, 2013 at 20:10 Comment(2)
How did you create the TTTAttributedLabel? Is it in a storyboard or xib? If so, did you set the "custom class" of the object (in the identity inspector panel)? And did you hook it up to your IBOutlet? Or did you create it in code and remember to add its subview?Artless
Hi Nicholas, it is in a storyboard, I have two storyboards one for iPhone and one for iPad I did it first for the one for iPhone and everything works great, then I did the same for the iPad and it shows the label and the links but it doesn't fire the function. I created the TTTAttributedLabel programmatically and added it to a view through subview.Grimona
G
5

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!

Grimona answered 23/7, 2013 at 4:18 Comment(0)
G
50

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;

Guyer answered 5/9, 2013 at 20:42 Comment(3)
Totally had the same issue. My TTTAttributedLabel was working when I implemented it, but later down the line I implemented a UITapGestureRecognizer and didn't get a chance to test the adverse effects. This solved my problem - thank you!Madame
Thank you. This answer helped me so much. here's an upvoteEmerald
you saved my life :DEstas
G
5

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!

Grimona answered 23/7, 2013 at 4:18 Comment(0)
T
4

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

Theatre answered 15/4, 2015 at 21:10 Comment(0)
O
3

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
    }
}
Oosperm answered 15/9, 2017 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.