22

I feel like I'm missing something easy but I can't seem to find out how to do this:

I set the attribute to a link like so:

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

That works but the link is blue and I can't seem to change the color. This sets everything except the link to white:

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

Is there another color attribute name that I can't seem to find that is specific to links?

Oren
  • 5,055
  • 3
  • 34
  • 52

8 Answers8

65
  1. Use a UITextView
  2. Set the UITextView's linkTextAttributes like so:

    textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
Justin Moser
  • 2,005
  • 3
  • 22
  • 28
  • 8
    This answer works but only if you can bind to a UITextView. Doesn't work for UILabel. – Oren Jun 14 '15 at 23:31
  • 4
    this question is about NSAttributedString, not UITextView – blueether Dec 13 '16 at 06:41
  • 1
    Well, if you can't do it with an NSAttributedString, then the next best thing is an answer that can achieve what you want. And this answer is that. – Rickster Feb 09 '17 at 23:53
4

I actually ended up using TTTAttributedLabel for my label and then was able to do the following which worked perfectly:

NSDictionary *linkAttributes = @{(id)kCTForegroundColorAttributeName: [UIColor whiteColor],
                                 (id)kCTUnderlineStyleAttributeName: [NSNumber numberWithInt:kCTUnderlineStyleSingle]
                                 };
self.lblDescription.linkAttributes = linkAttributes;    
Oren
  • 5,055
  • 3
  • 34
  • 52
4

txtLabel.linkAttributes = @{};

This is the right one, call this line before set any other attribute

SeiU
  • 41
  • 2
3

Explanation:

This is surprisingly easy, text components that supports link detection have a property called linkTextAttributes.

This property stores the style for the link as of the component can apply it when detects a new link.

Summing up, your style will be applied and then the style stored in this property (in this order), successfully overriding your desired style.

Solution:

Set this property to empty ( linkTextAttributes = [:] ) and take total control of you link Styles.


TIP: This can be used to create touchable elements on your UITextView that behaves like a button. Creating a Really nice effect

Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
2

For swift (for reference for others):

// Color the links
var linkAttributes: NSMutableDictionary = NSMutableDictionary()
linkAttributes.setValue(self.appDelegate.variables.color320, forKey: NSForegroundColorAttributeName)

myTextView.linkTextAttributes = linkAttributes as [NSObject : AnyObject]
Alex
  • 5,298
  • 4
  • 29
  • 34
2

For 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

enter image description here

Stephen Chen
  • 3,027
  • 2
  • 27
  • 39
1

This works for me:

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];
}
gschool
  • 11
  • 1
-1

If you use TTTAttributedLabel, Oren's answer is worked.But you need to pay attention to the linkAttributes's warning in annotation.

/**
A dictionary containing the default NSAttributedString attributes to be applied to links detected or manually added to the label text. The default link style is blue and underlined.

@warning You must specify linkAttributes before setting autodecting or manually-adding links for these attributes to be applied.
*/

@property (nonatomic, strong) NSDictionary *linkAttributes;

Laban Liao
  • 19
  • 6