Setting NSLinkAttributeName font color

2019-02-11 14:11发布

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?

8条回答
beautiful°
2楼-- · 2019-02-11 14:41

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

查看更多
再贱就再见
3楼-- · 2019-02-11 14:44

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;    
查看更多
登录 后发表回答