首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TTTAttributedLabel链接正在样式化,但不可单击。

TTTAttributedLabel链接正在样式化,但不可单击。
EN

Stack Overflow用户
提问于 2015-07-02 08:34:07
回答 2查看 6.8K关注 0票数 7

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

现在,我已经将TTTAttributedLabel添加到我的项目中,它的视图样式非常完美。链接也会变成蓝色并加下划线。

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

但是这些函数都没有被触发,我有断点和日志。

我实现了didSelectLinkWithUrl和didLongPressLinkWithUrl。

代码语言:javascript
复制
 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")
    }

插座

代码语言:javascript
复制
@IBOutlet weak var content: MyLabel!

MyLabel

导入UIKit导入TTTAttributedLabel

代码语言:javascript
复制
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的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-21 14:39:51

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

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

医生们还包括此警告:

TTTAttributedLabel可以同时显示纯文本和属性文本:只需将NSStringNSAttributedString传递给setText:设置器即可。从不分配给属性.

文档还显示了这个示例的用法:

代码语言:javascript
复制
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;
票数 16
EN

Stack Overflow用户

发布于 2016-12-12 14:27:57

亚伦·布雷格是对的!我以前也有同样的问题,但是我用Swift换了线路来解决这个问题:

代码语言:javascript
复制
label.attributedText = attributedString

这句话是:

代码语言:javascript
复制
label.setText(attributedString)

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31179458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档