2

I have done the following to have different color for links in my NIAttributedLabel:

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:86.0/255.0 green:134.0/255.0 blue:172.0/255.0 alpha:1.0] forKey:NSForegroundColorAttributeName];
        [attributes setValue:[UIColor colorWithRed:0.0 green:136/255.f blue:204/255.f alpha:1.0] forKey:NSForegroundColorAttributeName];
        [self.commentsText_ setAttributesForLinks:attributes];

but I am not seeing two different colors in the links, instead I am just seeing one. What am I doing wrong here? Basically I have a link that I've added via addLink as follows:

[self.commentsText_ addLink:[NSURL URLWithString:url] range:usernameRange];

and I want this to have the redColor. How do I do so?

adit
  • 32,574
  • 72
  • 229
  • 373
  • 1
    Why would you expect to see 2 colors? Your second setValue: call just replaces the first one since you're setting the value of the same key. – rdelmar Sep 30 '12 at 05:49
  • so how do I make it so that I can see two colors for different links? – adit Sep 30 '12 at 15:47

2 Answers2

1

If you want different colors for different links, then you need to create separate attribute dictionaries, and call setAttributesForLinks: with the different dictionaries.

        NSMutableDictionary *attributes1 = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:86.0/255.0 green:134.0/255.0 blue:172.0/255.0 alpha:1.0] forKey:NSForegroundColorAttributeName];  
        [self.commentsText1_ setAttributesForLinks:attributes1];

        NSMutableDictionary *attributes2 = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:0.0 green:136/255.f blue:204/255.f alpha:1.0] forKey:NSForegroundColorAttributeName];  
        [self.commentsText2_ setAttributesForLinks:attributes2];
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • so you're saying that one NIAttributedLabel can only have 1 link color? That defeats the whole purpose then – adit Sep 30 '12 at 16:00
  • I don't know what your purpose is, so I can't comment. I don't see any reference to setAttributes:forLinks: in the Nimbus docs, so I'm not sure what is possible with that method. – rdelmar Sep 30 '12 at 16:16
1

If you would like to set different colors for different links then you should disable the automatic link styles by setting linkColor to nil and then explicitly apply the different styles to your links.

featherless
  • 2,118
  • 19
  • 19