iPhone - 在UILabel中去掉链接下划线

3
我正在使用TTTAttributedLabel (https://github.com/twotoasters/TTTAttributedLabel)。我已经正确获取了带有一些可点击文本的标签。我需要像上面图片中的用户名那样显示我的文本(即没有下划线)。我该怎么做?

尝试使用以下代码:comment [self.summaryLabel addLinkToURL:url withRange:linkRange];。addLinkToURL方法可以在文本中添加下划线,这可能会对你有所帮助。 - LittleIDev
3个回答

9
尝试这段代码(由于使用手机编写,格式可能不太好看...):
NSDictionary *linkAttributes = @{[NSNumber numberWithInt:kCTUnderlineStyleNone] : (id)kCTUnderlineStyleAttributeName};
self.label.linkAttributes = linkAttributes;

在TTTAttributedLabel中它百分之百有效,对于UILabel可能也会有效。 - Paresh Navadiya

0

如果您已经具有带下划线样式的标签,且要以编程方式删除它,请使用以下代码

 let string = "your text"
 let attributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.systemFont(ofSize: 13)
            ]
 let attributedString = NSAttributedString(string: string, 
 attributes: attributes)
 yourLabel.attributedText = attributedString

0

对于 Swift 4.0

   let LinkAttributes = NSMutableDictionary(dictionary: testLink.linkAttributes)
   LinkAttributes[NSAttributedStringKey.underlineStyle] =  NSNumber(value: false)
   yourLabel.linkAttributes = LinkAttributes as NSDictionary as! [AnyHashable: Any]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接