JeremyWei
JeremyWei

Reputation: 93

TTTAttributedLabel Link detecting not work in iOS8 with swift

I want to use TTTAttributedLabel to detect the link of the text in the Label of UITableViewCell, but it doesn't work. I'm using swift with iOS8. below is UITableViewCell code:

class StoryTableViewCell: UITableViewCell, TTTAttributedLabelDelegate {
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        // Link properties
        let textLabel = self.descriptionLabel
        let linkColor = UIColor(red: 0.203, green: 0.329, blue: 0.835, alpha: 1)
        let linkActiveColor = UIColor.blackColor()

        if (textLabel is TTTAttributedLabel) {
            var label = textLabel as TTTAttributedLabel
            label.linkAttributes = [NSForegroundColorAttributeName : linkColor]
            label.activeLinkAttributes = [NSForegroundColorAttributeName : linkActiveColor]        
            label.enabledTextCheckingTypes = NSTextCheckingType.Link.toRaw()

            label.delegate = self
        }
    }
}

Upvotes: 8

Views: 9800

Answers (5)

guru
guru

Reputation: 2817

Nothing works for me finally i put below code in TTTAttributedLabel.m in commonInit() method

    self.enabledTextCheckingTypes = NSTextCheckingTypeLink;

Upvotes: 0

Ning
Ning

Reputation: 158

swift 4.2 label.enabledTextCheckingTypes = NSTextCheckingResult.CheckingType.link.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue

Upvotes: 0

David Rees
David Rees

Reputation: 7312

If you've set TTTAttributedLabel as the class of your UILabel, within a nib or the storyboard, make sure User Interaction Enabled is set to true, as be default a UILabel will have user interaction disabled.

Upvotes: 3

E-Riddie
E-Riddie

Reputation: 14780

I think you have not configured your custom cell correctly.

First at your customCell declare and connect your IBOutlet-s. Select your textLabel and add its class to TTTAttributedLabel. Your custom cell should look like this:

class StoryTableViewCell: UITableViewCell {
    @IBOutlet weak var textLabel: TTTAttributedLabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

Second you need to add the TTTAttributedLabelDelegate at the class where you are using the tableView datasource and delegate.

Then under cellForRowAtIndexPath

var cell: StoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier") as StoryTableViewCell

let linkColor = UIColor(red: 0.203, green: 0.329, blue: 0.835, alpha: 1)
let linkActiveColor = UIColor.blackColor()

cell.textLabel.delegate = self

cell.textLabel.linkAttributes = [kCTForegroundColorAttributeName : linkColor]
cell.textLabel.activeLinkAttributes = [kCTForegroundColorAttributeName : linkActiveColor]        
cell.textLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue

Then if you have methods that need to be executed from TTTAttributedLabelDelegate add them and do your calculations.

Hope it helps

Upvotes: 12

Kamlesh Delat
Kamlesh Delat

Reputation: 138

    let linkColor = UIColor.blueColor()
    let linkActiveColor = UIColor.greenColor()

    textLabel.delegate = self

    textLabel.linkAttributes = [kCTForegroundColorAttributeName : linkColor.CGColor,kCTUnderlineStyleAttributeName : NSNumber(bool: true)]
    textLabel.activeLinkAttributes = [NSForegroundColorAttributeName : linkActiveColor]
    textLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue

Upvotes: 0

Related Questions