更改NSMutableAttributedString中链接的颜色


76

我有以下代码,但链接始终为蓝色。我如何才能改变它们的颜色?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)];
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)];
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)];

_string是NSMutableAttributedString,并且位置和长度正常。



如果您认为该问题已被用户充分回答,请选择该问题作为接受的答案。
Will Von Ullrich'9

Answers:


179

迅速

为Swift 4.2更新

使用linkTextAttributesUITextView

textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]

在上下文中:

let attributedString = NSMutableAttributedString(string: "The site is www.google.com.")
let linkRange = (attributedString.string as NSString).range(of: "www.google.com")
attributedString.addAttribute(NSAttributedString.Key.link, value: "https://www.google.com", range: linkRange)
let linkAttributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor.green,
    NSAttributedString.Key.underlineColor: UIColor.lightGray,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]

// textView is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString

目标C

使用linkTextAttributesUITextView

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

资料来源:这个答案

这篇文章

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

我已经能够更改链接的颜色,但是当我不断按下链接时如何更改显示的颜色?
Mago Nicolas Palacios

46

链接颜色是标签/文本视图的颜色。因此,您可以通过更改视图的颜色来更改它。但是,如果要在同一视图中使用不同的链接颜色,则此方法将无效。


在iOS 12.为我工作
虹膜Veriris

对于UITextView
效果

8
在Xcode 11.2.1,UILabel @crcalin的Swift 5中不起作用
iOS

8

迅速

 let str = "By using this app you agree to our Terms and Conditions and Privacy Policy"
 let attributedString = NSMutableAttributedString(string: str)
 var foundRange = attributedString.mutableString.rangeOfString("Terms and Conditions")

 attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange)
 foundRange = attributedString.mutableString.rangeOfString("Privacy Policy")
 attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange)
 policyAndTermsTextView.attributedText = attributedString
 policyAndTermsTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.blueColor()]

1
 NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];

目标C

带下划线的白色可点击文本。为代码选择必要的属性并使用它。

要在其中包含带有可点击链接的字符串,请执行以下操作:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Click " attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15]}];
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];
[string appendAttributedString:attributedString];

结果,您将获得字符串“ Click here”,并且“ here”将是一个链接。您可以为每个字符串设置不同的样式。


1

对于swift3.0

  override func viewDidLoad() {
     super.viewDidLoad()

  let linkAttributes = [
        NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
        ] as [String : Any]
  let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")

  attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))

  attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))

  textview.delegate = self
  textview.attributedText = attributedString
  textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
  textview.textColor = UIColor.white
  }


  func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    return true
   }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.