TTTAttributedLabel 链接正在设置样式但不可点击 [英] TTTAttributedLabel links are being styled but not clickable

查看:20
本文介绍了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,使故事板中的标签实现了 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")
    }

出口

@IBOutlet weak var content: MyLabel!

我的标签

导入 UIKit导入 TTTAttributedLabel

import UIKit import 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?

更新

我发现只需粘贴一个 url f/e http://example.com 就会变为活动状态并且实际上是可点击的,而 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.

推荐答案

setAttributedText: 的实现不会更新 linkModels 数组,而 setText: 的实现确实如此.我相信这就是导致您出现问题的原因.

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 属性.

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

文档 还包含此警告:

TTTAttributedLabel 可以显示纯文本和属性文本:只需将 NSStringNSAttributedString 传递给 setText:二传手.切勿分配给 attributedText 属性.

TTTAttributedLabel can display both plain and attributed text: just pass an NSString or NSAttributedString to the setText: setter. Never assign to 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 链接正在设置样式但不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆