Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a link in an NSMutableAttributedString

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.

like image 255
cdub Avatar asked Feb 06 '15 07:02

cdub


People also ask

How do I change the color of a link in TextView Swift?

You can Change the Hyperlink Color in a TextView by the following: In the Nib file, you can go to the Properties Window and change the Tint to which ever color you want to.

What is an attributed string?

Overview. Attributed strings are character strings that have attributes for individual characters or ranges of characters. Attributes provide traits like visual styles for display, accessibility for guided access, and hyperlink data for linking between data sources.


2 Answers

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; 
like image 191
Suragch Avatar answered Sep 22 '22 14:09

Suragch


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.

like image 45
crcalin Avatar answered Sep 23 '22 14:09

crcalin