【问题标题】:TTTAttributedLabel links are being styled but not clickableTTTAttributedLabel 链接正在设置样式但不可点击
【发布时间】:2015-09-19 16:20:44
【问题描述】:

我一直在寻找使可点击链接正常工作的解决方案。我可以在使用 UITextView + NSAttributedString 时让它工作,但是当它是 UITableViewCell 时它不能正确自动布局。

现在我已将 TTTAttributedLabel 添加到我的项目中,它完美地设置了视图的样式。链接也会变成蓝色并带有下划线。

但是点击它们什么也没做。我确实在我的控制器上实现了 TTTAttributedLabelDelegate,使情节提要中的标签实现了 MyLabel(它只是扩展了 TTTAttributedLabel 并具有委托选项,因为我希望它们在同一个函数中触发)。现在我已经将控制器设置为我认为它可能无法指向自身的委托。

但是这些函数都没有被触发,我得到了断点并登录了它。

我实现了 didSelectLinkWithUrl 和 didLongPressLinkWithUrl。

 func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
        Debug.log("link clicked")
    }
    func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
        Debug.log("link long clicked")
    }

出口

@IBOutlet weak var content: MyLabel!

我的标签

导入 UIKit 导入 TTTAttributedLabel

class MyLabel : TTTAttributedLabel, TTTAttributedLabelDelegate {

override func didMoveToSuperview() {
    if (self.delegate == nil) {
        self.delegate = self
    }
    self.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
    self.userInteractionEnabled = true
}

func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
    Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
    Debug.log("link long clicked")
}

有人知道我可能会错过什么吗?

更新

我发现只是粘贴在一个 url f/e http://example.com 中就会变得活跃并且实际上是可点击的,并且 didSelectLinkWithUrl 变成可点击的,尽管我需要一个属性字符串并且它基于 HTML 字符串。

【问题讨论】:

  • 斯威夫特 3/4:NSTextCheckingResult.CheckingType.link.rawValue

标签: ios swift hyperlink tttattributedlabel


【解决方案1】:

The implementation of setAttributedText: 不会更新 linkModels 数组,而 the implementation of setText: 会。我相信这是导致您的问题的原因。

要解决此问题,请设置标签的 text 属性而不是 attributedText 属性。

The docs 也包含此警告:

TTTAttributedLabel 可以显示纯文本和属性文本:只需将 NSStringNSAttributedString 传递给 setText: 设置器。 永远不要分配给attributedText 属性。

文档还显示了此示例用法:

TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Tom Bombadil"
                                                                attributes:@{
        (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
        NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
        NSKernAttributeName : [NSNull null],
        (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor
}];

// The attributed string is directly set, without inheriting any other text
// properties of the label.
attributedLabel.text = attString;

【讨论】:

  • @MathijsSegers 我添加了一些关于为什么会发生这种行为的更多细节 - 但基本上,只需将您的属性字符串设置为 text 属性。
  • @MathijsSegers 能否请您展示一下您的快速代码,我真的无法让它工作。它说:无法将类型“NSAttributedString”的值分配给类型“String?”
  • @hugocarlmartin,而不是label.text = attrbText,使用label.setText(attrbText)
【解决方案2】:

Aaron Brager 是正确的! 我曾经遇到过同样的问题,但我通过替换行在 Swift 中修复了它:

label.attributedText = attributedString

用线:

label.setText(attributedString)

这被编译器接受,因为 setText 方法接受 AnyObject。 我还增加了链接的属性字符串的字体,以便它捕获我的点击,它现在正在工作! 我将它用于截断链接,这是整个部分:

label.lineBreakMode = .ByTruncatingHead
label.attributedTruncationToken = NSMutableAttributedString(string: "... Show more", attributes: [NSForegroundColorAttributeName: UIColor.cueCyan(), NSLinkAttributeName: readMoreLink, NSFontAttributeName: UIFont.formFont(.Light, size: fontSize+24)!])
label.userInteractionEnabled = true
label.delegate = self

【讨论】:

  • 你能举一个 readMoreLink 的例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多