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