在UILabel.attributedText *不*蓝色和*不*下划线的链接

我想在我的OHAttributedLabel中的一些词是链接,但我希望他们是蓝色以外的颜色,我不想下划线。

这给了我一个带下划线的文字的蓝色链接:

-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy]; [mutableAttributedText beginEditing]; [mutableAttributedText addAttribute:kOHLinkAttributeName value:[NSURL URLWithString:@"http://www.somewhere.net"] range:range]; [mutableAttributedText addAttribute:(id)kCTForegroundColorAttributeName value:color range:range]; [mutableAttributedText addAttribute:(id)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleNone] range:range]; [mutableAttributedText endEditing]; self.label.attributedText = mutableAttributedText; } 

由于我使用的是OHAttributedLabel,我也尝试过使用NSAttributedString+Attributes.h类中的方法,但是那些返回蓝色下划线的链接也是如此:

 -(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy]; [mutableAttributedText setLink:[NSURL URLWithString:@"http://www.somewhere.net"] range:range]; [mutableAttributedText setTextColor:color range:range]; [mutableAttributedText setTextUnderlineStyle:kCTUnderlineStyleNone range:range]; self.label.attributedText = mutableAttributedText; } 

如果我在每个版本中注释掉设置链接的行,那么文本就会变成我所传递的内容的颜色。 这似乎是设置链接覆盖这个并把它变回蓝色。

不幸的是,我发现的苹果文档页面显示了如何设置链接文本为蓝色,并强调它,正是我不需要: https : //developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/任务/ ChangingAttrStrings.html

所以我结束了使用TTTAttributedLabel:

 -(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* newTextWithLinks = [self.label.attributedText mutableCopy]; NSURL *url = [NSURL URLWithString:@"http://www.reddit.com"]; self.label.linkAttributes = @{NSForegroundColorAttributeName: color, NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}; [self.label addLinkToURL:url withRange:range]; } 

我发现OHAttributedLabel实际上有设置链接的方法,并为这些链接声明颜色和下划线样式。 但是,我希望链接是基于参数的不同颜色。 TTTAttributedLabel允许您为每个创build的链接设置它的linkAttributes属性。

我正在使用TTTAttributedLabel 。 我想改变链接文本的颜色,并保留下划线。 皮姆的回答看起来不错,但对我来说并不合适。 这是什么工作:

 label.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor magentaColor], (id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle] }; 

注意:如果您不希望文本下划线,则从字典中删除kCTUnderlineStyleAttributeName键。

这是我的改进版本的Ramsel已经很好的答案。 我相信它更具可读性,我希望它能够得到很好的使用。

 label.linkAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] }; 

这是一个其他连续名称的列表 。

如果您使用的是uitextview,则可能需要更改tintColor以更改链接颜色

TTTAttributedLabel Swift 2.3示例:

 yourLabel.linkAttributes = [ NSForegroundColorAttributeName: UIColor.grayColor(), NSUnderlineStyleAttributeName: NSNumber(bool: true) ] yourLabel.activeLinkAttributes = [ NSForegroundColorAttributeName: UIColor.grayColor().colorWithAlphaComponent(0.8), NSUnderlineStyleAttributeName: NSNumber(bool: false) ] 

斯威夫特4

 yourLabel.linkAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.grayColor(), NSAttributedStringKey.underlineStyle: NSNumber(value: true) ] yourLabel.activeLinkAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.grayColor().withAlphaComponent(0.7), NSAttributedStringKey.underlineStyle: NSNumber(value: false) ] 

Swift 3 TTTAttributedLabel示例:

 yourLabel.linkAttributes = [ NSForegroundColorAttributeName: UIColor.green, NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleNone.rawValue) ] yourLabel.activeLinkAttributes = [ NSForegroundColorAttributeName: UIColor.green, NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleDouble.rawValue) ] 

对于Swift 3使用TTTAttributedLabel

 let title: NSString = "Fork me on GitHub!" var attirutedDictionary = NSMutableDictionary(dictionary:attributedLabel.linkAttributes) attirutedDictionary[NSForegroundColorAttributeName] = UIColor.red attirutedDictionary[NSUnderlineStyleAttributeName] = NSNumber(value: NSUnderlineStyle.styleNone.rawValue) attributedLabel.attributedText = NSAttributedString(string: title as String) attributedLabel.linkAttributes = attirutedDictionary as! [AnyHashable: Any] let range = subtitleTitle.range(of: "me") let url = URL(string: "http://github.com/mattt/") attributedLabel.addLink(to: url, with: range) 

对于使用TTTAttributedLabel Swift 3来说:

1)在故事板上添加一个标签,并将其类定义为TTTAttributedLabel 故事板视图

2)在代码中定义@IBOutlet var termsLabel: TTTAttributedLabel!

3)然后在ViewDidLoad写下这些行

 let textString = "By using this app you agree to the Privacy Policy & Terms & Conditions." guard let labelString = termsLabel.attributedText else { return } guard let privacyRange = labelString.string.range(of: "Privacy Policy") else { return } guard let termsConditionRange = labelString.string.range(of: "Terms & Conditions") else { return } let privacyNSRange: NSRange = labelString.string.nsRange(from: privacyRange) let termsNSRange: NSRange = labelString.string.nsRange(from: termsConditionRange) termsLabel.addLink(to: URL(string: "privacy"), with: privacyNSRange) termsLabel.addLink(to: URL(string: "terms"), with: termsNSRange) termsLabel.delegate = self let attributedText = NSMutableAttributedString(attributedString: termsLabel.attributedText!) attributedText.addAttributes([NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 12)!], range: termsNSRange) attributedText.addAttributes([NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 12)!], range: privacyNSRange) attributedText.addAttributes([kCTForegroundColorAttributeName as String: UIColor.orange], range: termsNSRange) attributedText.addAttributes([kCTForegroundColorAttributeName as String: UIColor.green], range: privacyNSRange) attributedText.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue], range: termsNSRange) attributedText.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue], range: privacyNSRange) termsLabel.attributedText = attributedText 

看起来像这样 模拟器视图

4)最后编写TTTAttributedLabelfunction,以便您可以点击打开链接

 public func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) { switch url.absoluteString { case "privacy": SafariBrowser.open("http://google.com", presentingViewController: self) case "terms": SafariBrowser.open("http://google.com", presentingViewController: self) default: break } } 

Swift 3:

 // apply link attributes to label.attributedString, then label.tintColor = UIColor.red // any color you want