Change the color of a link in an NSMutableAttributedString

IosIphoneNsattributedstringNsmutableattributedstring

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

#Swift

Updated for Swift 4.2

Use linkTextAttributes with a UITextView

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

And in context:

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

#Objective-C

Use linkTextAttributes with a 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;

Solution 2 - Ios

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.

Solution 3 - Ios

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

Solution 4 - Ios

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

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.

Solution 5 - Ios

For 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
   }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncdubView Question on Stackoverflow
Solution 1 - IosSuragchView Answer on Stackoverflow
Solution 2 - IoscrcalinView Answer on Stackoverflow
Solution 3 - IosDasogaView Answer on Stackoverflow
Solution 4 - IosVictorianecView Answer on Stackoverflow
Solution 5 - IosMaulik PatelView Answer on Stackoverflow