2

I have using a Xamarin binding for https://github.com/TTTAttributedLabel/TTTAttributedLabel

And as per - Make link in UILabel.attributedText *not* blue and *not* underlined

self.label.linkAttributes = @{NSForegroundColorAttributeName: color, 
                               NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};

I would like to set my link attributes, I am just not sure of syntax -

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html

I have tried variations on these -

 label.LinkAttributes = new NSDictionary(
            new NSString("NSForegroundColorAttributeName"), UIColor.Red,
            new NSString("NSUnderlineStyleAttributeName"), new NSUnderlineStyle() == NSUnderlineStyle.Double);

 label.LinkAttributes = new NSDictionary(
            new NSString("NSForegroundColorAttributeName"), UIColor.Red,
            new NSString("NSUnderlineStyleAttributeName"), new NSNumber(2));

But not working. Not sure how to pass in an UIColor as cannot see its Type available, it is doing "something" as its wiped my underline + blue colour with this code.

Community
  • 1
  • 1
WickedW
  • 2,331
  • 4
  • 24
  • 54

1 Answers1

2

The following line of code was taken from the github README.md from the project you linked.

(id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,

It looks like the library deals with CGColor and not UIColor (or NSColor which are OSX only). There's (too) many ways to represent colours and, sadly, most API only works with one. In this case you'll need to use:

UIColor.Red.CGColor

instead of:

UIColor.Red

in the dictionary you give to the LinkAttributes property of your label.

The key kCTForegroundColorAttributeName (from your comment) must also match Apple's constant value, which can be different than the constant's name. In Xamarin.iOS this constant is exposed as:

CoreText.CTStringAttributeKey.ForegroundColor

So it the library (re)uses the CoreText constants then this is the value to use.

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Thank you @poupou, unfortunately its still is not working with .CGColor either, I am wondering if this alternative - label.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor magentaColor], (id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle] }; call style mentioned in the SO answer above has something to do with, I am just not sure if I have the right key and putting in new NSString("Whatever"), UIColor.Red.CGColor); brings me the same result. – WickedW Dec 22 '15 at 09:29
  • Updated answer based on your comment, i.e. it cannot be be any `NSString` key, it must be what the library looks for - which, from your comment, looks like its re-using CoreText constants. – poupou Dec 22 '15 at 13:19
  • Thank you sir, works like magic! - label.LinkAttributes = new NSDictionary( CoreText.CTStringAttributeKey.ForegroundColor, UIColor.Red.CGColor); – WickedW Dec 29 '15 at 18:03
  • A final point, is there a process for unearthing the CoreText Key Constant you posted or is it just experience? I looked in the code and could not see a trail to follow nor by searching on google for the constant names and Xamarin etc ... thanks – WickedW Dec 29 '15 at 18:10