【问题标题】:TTTAttributedLabel Link detecting not work in iOS8 with swiftTTTAttributedLabel 链接检测在 iOS8 中无法使用 swift
【发布时间】:2014-10-22 03:01:24
【问题描述】:

我想用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
        }
    }
}

【问题讨论】:

标签: ios uitableview swift ios8 tttattributedlabel


【解决方案1】:

我认为你没有配置你的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 执行的方法,请添加它们并进行计算。

希望对你有帮助

【讨论】:

  • 我听从了你的回答,但我无法让它发挥作用。关于如何调试它的任何想法?文本在那里,但没有找到链接。
  • 更新 1:我通过添加委托函数属性标签 didSelectLinkWithURL 和 didLongPressLinkWithURL 开始调试。前者不起作用,但后者起作用。更新 2:没有调用 didSelectLinkWithURL,因为我在视图中有一个 Tapper.cancelsTouchesInView = true。当我将其设置为 false 时,点击开始起作用。
  • 似乎 NSForegroundColorAttributeName 在 iOS8 上不起作用。如果发生这种情况,只需使用 kCTForegroundColorAttributeName 代替。
【解决方案2】:

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

【讨论】:

    【解决方案3】:
        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
    

    【讨论】:

    • 嗨,kamlesh,我在 tttattributedlabel 中遇到截断标记的点击问题。你知道吗?
    【解决方案4】:

    迅速 4.2 label.enabledTextCheckingTypes = NSTextCheckingResult.CheckingType.link.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue

    【讨论】:

      【解决方案5】:

      最后我把下面的代码放在 TTTAttributedLabel.m 的 commonInit() 方法中

          self.enabledTextCheckingTypes = NSTextCheckingTypeLink;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多