设置NSLinkAttributeName字体颜色

我觉得我错过了一些容易,但我似乎无法find如何做到这一点:

我将属性设置为如下所示的链接:

[myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange]; 

这有效,但链接是蓝色的,我似乎无法改变颜色。 这设置除了链接到白色的一切:

 [myAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:selectedRange]; 

是否有另一个颜色属性名称,我似乎无法find特定于链接?

  1. 使用UITextView
  2. 设置UITextViewlinkTextAttributes如下所示:

     textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; 

我实际上最终使用TTTAttributedLabel作为我的标签,然后能够完成以下工作:

 NSDictionary *linkAttributes = @{(id)kCTForegroundColorAttributeName: [UIColor whiteColor], (id)kCTUnderlineStyleAttributeName: [NSNumber numberWithInt:kCTUnderlineStyleSingle] }; self.lblDescription.linkAttributes = linkAttributes; 

快速(供其他人参考):

 // Color the links var linkAttributes: NSMutableDictionary = NSMutableDictionary() linkAttributes.setValue(self.appDelegate.variables.color320, forKey: NSForegroundColorAttributeName) myTextView.linkTextAttributes = linkAttributes as [NSObject : AnyObject] 

txtLabel.linkAttributes = @{};

这是正确的,在设置任何其他属性之前调用这一行

这适用于我:

 txtLabel.linkAttributes = @{}; NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:expression]; { [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range]; [string addAttribute:NSLinkAttributeName value:@"link" range:result.range]; } 

说明:

这非常简单,支持链接检测的文本组件具有一个名为linkTextAttributes的属性。

该属性存储组件的链接样式 ,可以在检测到新链接时应用它。

总结一下,你的风格将被应用,然后存储在这个属性中的样式 (按这个顺序),成功地覆盖你所期望的风格。

解:

将此属性设置为空( linkTextAttributes = [:] )并完全控制您的链接样式。


提示:这可以用来在你的UITextView上创build一个像button一样的可触摸元素 。 创造一个非常好的效果🙃

对于Swift 3

 var aURL = "Http:// Your URL" var description = "Click Me:" let attributedString = NSMutableAttributedString(string: description + aURL) /// Deal with link color let foundURLRange = attributedString.mutableString.range(of: aURL) attributedString.addAttribute(NSLinkAttributeName, value: aURL, range: foundURLRange) textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.yellow] /// Deal with description color let foundDescriptionRange = attributedString.mutableString.range(of: description) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: foundDescriptionRange) textview.attributedText = attributedString 

在这里输入图像说明

如果你使用TTTAttributedLabel,Oren的答案是有效的。但是你需要注意linkAttributes在注解中的警告。

/ **
包含默认NSAttributedString属性的字典,将应用于检测到的或手动添加到标签文本的链接。 默认的链接样式是蓝色和下划线。

@warning在设置自动检测或手动添加要应用的这些属性的链接之前,您必须指定linkAttributes
* /

@属性(非primefaces,强大)NSDictionary * linkAttributes;