7

我一直在寻找使可点击链接正常工作的解决方案。我可以在使用 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 字符串。

4

2 回答 2

16

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

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

文档还包括此警告:

TTTAttributedLabel可以显示纯文本和属性文本:只需将NSStringor传递NSAttributedStringsetText:setter。永远不要分配给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;
于 2015-07-21T14:39:51.063 回答
2

亚伦布拉格是正确的!我曾经遇到过同样的问题,但我通过替换以下行在 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
于 2016-12-12T14:27:57.460 回答