TTTAttributedLabel links are being styled but not clickable
Asked Answered
B

2

7

I've been looking into a solution to getting clickable links working. I can get this working when using UITextView + NSAttributedString but it just doesn't autolayout properly when it's a UITableViewCell.

Now I've added the TTTAttributedLabel to my project and it styles the views just perfectly. The links also turn blue and are underlined.

However clicking them does nothing. I did implement TTTAttributedLabelDelegate on my controller, made the label in the storyboard implement MyLabel (Which just extends TTTAttributedLabel and has the delegate options since I want them to fire inside the same function). For now I've set the controller to be the delegate I was thinking it might not work pointing to itself.

But none of these functions get fired, I got breakpoints and logs in it.

I Implemented didSelectLinkWithUrl and didLongPressLinkWithUrl.

 func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
        Debug.log("link clicked")
    }
    func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
        Debug.log("link long clicked")
    }

Outlet

@IBOutlet weak var content: MyLabel!

MyLabel

import UIKit import TTTAttributedLabel

class MyLabel : TTTAttributedLabel, TTTAttributedLabelDelegate {

override func didMoveToSuperview() {
    if (self.delegate == nil) {
        self.delegate = self
    }
    self.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
    self.userInteractionEnabled = true
}

func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
    Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
    Debug.log("link long clicked")
}

Anyone know what I could be missing?

Update

I found out that just pasting in an url f/e http://example.com becomes active and is actually clickable and the didSelectLinkWithUrl becomes clickable, allthough I need an attributed string and it's based on a HTML String.

Berserk answered 2/7, 2015 at 8:34 Comment(1)
Swift 3/4: NSTextCheckingResult.CheckingType.link.rawValueSkellum
A
17

The implementation of setAttributedText: doesn't update the linkModels array, while the implementation of setText: does. I believe this is what causes your issue.

To resolve, set the label's text property instead of the attributedText property.

The docs also include this warning:

TTTAttributedLabel can display both plain and attributed text: just pass an NSString or NSAttributedString to the setText: setter. Never assign to the attributedText property.

The docs also show this example usage:

TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Tom Bombadil"
                                                                attributes:@{
        (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
        NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
        NSKernAttributeName : [NSNull null],
        (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor
}];

// The attributed string is directly set, without inheriting any other text
// properties of the label.
attributedLabel.text = attString;
Acoustics answered 21/7, 2015 at 14:39 Comment(3)
@MathijsSegers I added some more details about why this behavior occurs - but basically, just set your attributed string to the text property.Acoustics
@MathijsSegers can u please show ur pice of swift code, I really cant get it to work. It says: Cannot assign value of type 'NSAttributedString' to type 'String?'Fidgety
@hugocarlmartin, instead of label.text = attrbText, use label.setText(attrbText).Stuffed
L
2

Aaron Brager is correct! I used to have the same problem, but I got it fixed in Swift by replacing the line:

label.attributedText = attributedString

with the line:

label.setText(attributedString)

This is accepted by the compiler because the setText method accepts AnyObject. I also increased the font of the attributed string of the link so it captures my tap and it is working now! I used this for the truncation link, this is the whole part:

label.lineBreakMode = .ByTruncatingHead
label.attributedTruncationToken = NSMutableAttributedString(string: "... Show more", attributes: [NSForegroundColorAttributeName: UIColor.cueCyan(), NSLinkAttributeName: readMoreLink, NSFontAttributeName: UIFont.formFont(.Light, size: fontSize+24)!])
label.userInteractionEnabled = true
label.delegate = self
Lieu answered 12/12, 2016 at 14:27 Comment(1)
Can you include an example of the readMoreLink?Durrace

© 2022 - 2024 — McMap. All rights reserved.