我一直在寻找一个解决方案,使可点击链接工作。我可以在使用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!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的。
发布于 2015-07-21 14:39:51
setAttributedText:不更新linkModels数组,而setText:更新。我相信这就是导致你的问题的原因。
若要解决此问题,请设置标签的text属性而不是attributedText属性。
医生们还包括此警告:
TTTAttributedLabel可以同时显示纯文本和属性文本:只需将NSString或NSAttributedString传递给setText:设置器即可。从不分配给属性.
文档还显示了这个示例的用法:
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;发布于 2016-12-12 14:27:57
亚伦·布雷格是对的!我以前也有同样的问题,但是我用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 = selfhttps://stackoverflow.com/questions/31179458
复制相似问题