TTTAttributedLabel Link detecting not work in iOS8 with swift
Asked Answered
M

5

8

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
        }
    }
}
Moriyama answered 22/10, 2014 at 3:1 Comment(2)
Check out : https://mcmap.net/q/1325043/-tttattributedlabel-issue-detecting-linksJawbone
@Prince it's not workMoriyama
M
12

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

Metope answered 22/10, 2014 at 9:51 Comment(3)
I followed your answer but I am not being able to make it work. Any idea on how to debug it? The text is there but no link is being found.Mcmullan
UPDATE 1: I started debugging by adding the delegate functions attributedLabel didSelectLinkWithURL and didLongPressLinkWithURL. The former was not working but the latter is. UPDATE 2: The didSelectLinkWithURL was not being called because I had a tapper in the view that was with tapper.cancelsTouchesInView = true. When I set it to false, the click started to work.Mcmullan
It appears that NSForegroundColorAttributeName is not working on iOS8. If that happens, just use kCTForegroundColorAttributeName instead.Mcmullan
F
3

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.

Flaunty answered 19/7, 2016 at 2:57 Comment(0)
I
0
    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
Indonesian answered 12/9, 2015 at 7:41 Comment(1)
hi kamlesh i am facing clicking problem in truncation token in tttattributedlabel.Do you have an idea of that?Angeliqueangelis
T
0

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

Thurman answered 8/1, 2019 at 22:36 Comment(0)
B
0

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

    self.enabledTextCheckingTypes = NSTextCheckingTypeLink;
Buttonwood answered 3/4, 2019 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.