本文介绍了TTTAttributedLabel链接正在设置样式但不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking into a solution to getting clickable links working. I can get this working when using UITextView + NSAttributedString but it just doesn't autolayout properly when it's a UITableViewCell.

现在我已经将TTTAttributedLabel添加到我的项目中了完美地调整视图的样式。链接也变为蓝色并带下划线。

Now I've added the TTTAttributedLabel to my project and it styles the views just perfectly. The links also turn blue and are underlined.

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

However clicking them does nothing. I did implement TTTAttributedLabelDelegate on my controller, made the label in the storyboard implement MyLabel (Which just extends TTTAttributedLabel and has the delegate options since I want them to fire inside the same function). For now I've set the controller to be the delegate I was thinking it might not work pointing to itself.

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

But none of these functions get fired, I got breakpoints and logs in it.

我实现了didSelectLinkWithUrl和didLongPressLinkWithUrl。

I Implemented didSelectLinkWithUrl and 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")
    }

Outlet

@IBOutlet weak var content: MyLabel!

MyLabel

import UIKit
import TTTAttributedLabel

import UIKitimport 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")
}

任何人都知道我可能缺少什么?

Anyone know what I could be missing?

更新

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

I found out that just pasting in an url f/e http://example.com becomes active and is actually clickable and the didSelectLinkWithUrl becomes clickable, allthough I need an attributed string and it's based on a HTML String.

推荐答案

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

The implementation of setAttributedText: doesn't update the linkModels array, while the implementation of setText: does. I believe this is what causes your issue.

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

To resolve, set the label's text property instead of the attributedText property.

还包括此警告:

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

The docs also show this example usage:

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;

这篇关于TTTAttributedLabel链接正在设置样式但不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:04