8

我想用TTTAttributedLabel来检测UITableViewCell的Label中文字的链接,但是不行。我在 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
        }
    }
}
4

5 回答 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
    }
}

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

然后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添加它们并进行计算。

希望能帮助到你

于 2014-10-22T09:51:57.730 回答
3

如果您已将 TTTAttributedLabel 设置为 UILabel 的类,则在 nib 或情节提要中,请确保将User Interaction Enabled设置为 true,因为默认情况下 UILabel 将禁用用户交互。

于 2016-07-19T02:57:45.690 回答
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
于 2015-09-12T07:41:55.417 回答
0

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

于 2019-01-08T22:36:17.260 回答
0

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

    self.enabledTextCheckingTypes = NSTextCheckingTypeLink;
于 2019-04-03T15:24:14.177 回答