TTTAttributedLabel链接检测在iOS8中使用swift无法正常工作

问题描述 投票:8回答:5

我想使用TTTAttributedLabel来检测UITableViewCell标签中文本的链接,但它不起作用。我正在使用iOS8的swift。下面是UITableViewCell代码:

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
        }
    }
}
ios uitableview swift ios8 tttattributedlabel
5个回答
12
投票

我认为你没有正确配置你的custom cell

首先在customCell上声明并连接你的IBOutlet-s。选择textLabel并将其类添加到TTTAttributedLabel。您的自定义单元格应如下所示:

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
    }
}

其次,您需要在使用tableView数据源和委托的类中添加TTTAttributedLabelDelegate

然后在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

然后,如果您有需要从TTTAttributedLabelDelegate执行的方法,请添加它们并进行计算。

希望能帮助到你


3
投票

如果您已将TTTAttributedLabel设置为UILabel的类,在nib或storyboard中,请确保User Interaction Enabled设置为true,因为UILabel将禁用用户交互。


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

0
投票

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


0
投票

没有什么对我有用,最后我把下面的代码放在commonInit()方法的TTTAttributedLabel.m中

    self.enabledTextCheckingTypes = NSTextCheckingTypeLink;
© www.soinside.com 2019 - 2024. All rights reserved.