更改NSMutableAttributedString中链接的颜色

时间:2022-11-21 15:55:45

I have the following code but my links are always blue. How do I cange the color of them?

我有以下代码,但我的链接始终是蓝色的。我如何塑造它们的颜色?

[_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 is a NSMutableAttributedString and the position and length work fine.

_string是一个NSMutableAttributedString,位置和长度工作正常。

5 个解决方案

#1


79  

Swift

Updated for Swift 3

针对Swift 3进行了更新

Use linkTextAttributes with a UITextView

将linkTextAttributes与UITextView一起使用

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green]

And in context:

在上下文中:

let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)
let linkAttributes: [String : Any] = [
    NSForegroundColorAttributeName: UIColor.green,
    NSUnderlineColorAttributeName: UIColor.lightGray,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]

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

Swift 4:

斯威夫特4:

let linkAttributes: [String : Any] = [
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.green,
    NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray,
    NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]

Objective-C

Use linkTextAttributes with a UITextView

将linkTextAttributes与UITextView一起使用

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

Source: this answer

资料来源:这个答案

And from this post:

从这篇文章:

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;

#2


27  

The link color is the tint color of the label/textView. So, you can change it by changing the tint color of the view. However, this will not work if you want different link colours within the same view.

链接颜色是标签/ textView的着色颜色。因此,您可以通过更改视图的色调颜色来更改它。但是,如果您想在同一视图中使用不同的链接颜色,则无法使用此功能。

#3


3  

Swift

迅速

 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()]

#4


0  

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

Objective-C

Objective-C的

This will make underlined white clickable text. Select necessary attributes for your code and use it.

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

To have string with clickable link in it do next:

要在其中包含带可点击链接的字符串,请执行下一步:

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];

As a result you will get string 'Click here' and 'here' will be a link. You can set different styles to each string.

因此,您将获得字符串'Click here','here'将是一个链接。您可以为每个字符串设置不同的样式。

#5


0  

For swift3.0

对于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
   }

#1


79  

Swift

Updated for Swift 3

针对Swift 3进行了更新

Use linkTextAttributes with a UITextView

将linkTextAttributes与UITextView一起使用

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green]

And in context:

在上下文中:

let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)
let linkAttributes: [String : Any] = [
    NSForegroundColorAttributeName: UIColor.green,
    NSUnderlineColorAttributeName: UIColor.lightGray,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]

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

Swift 4:

斯威夫特4:

let linkAttributes: [String : Any] = [
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.green,
    NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray,
    NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]

Objective-C

Use linkTextAttributes with a UITextView

将linkTextAttributes与UITextView一起使用

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

Source: this answer

资料来源:这个答案

And from this post:

从这篇文章:

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;

#2


27  

The link color is the tint color of the label/textView. So, you can change it by changing the tint color of the view. However, this will not work if you want different link colours within the same view.

链接颜色是标签/ textView的着色颜色。因此,您可以通过更改视图的色调颜色来更改它。但是,如果您想在同一视图中使用不同的链接颜色,则无法使用此功能。

#3


3  

Swift

迅速

 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()]

#4


0  

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

Objective-C

Objective-C的

This will make underlined white clickable text. Select necessary attributes for your code and use it.

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

To have string with clickable link in it do next:

要在其中包含带可点击链接的字符串,请执行下一步:

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];

As a result you will get string 'Click here' and 'here' will be a link. You can set different styles to each string.

因此,您将获得字符串'Click here','here'将是一个链接。您可以为每个字符串设置不同的样式。

#5


0  

For swift3.0

对于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
   }