Ross Stepaniak
Ross Stepaniak

Reputation: 855

NSTextField NSAttributedString foregroundColor for links is broken on macOS 14

I'm trying to set an attributed string to a textfield that contains a link and show this link in red and not in a default light blue color. MacOS seems to useNScolor.linkColor for links.

textField.allowsEditingTextAttributes = true
textField.isSelectable = true

let urlText = "https://google.com"
let attributedString = NSMutableAttributedString(string: urlText)
let range = NSMakeRange(0, attributedString.string.count) 
   
attributedString.beginEditing()
attributedString.addAttribute(.link, value: urlText, range: range)
attributedString.addAttribute(.foregroundColor, value: NSColor.red, range: range)
attributedString.endEditing()
       
// Just an outlet textfield on a cell
textField.attributedStringValue = attributedString

I checked the official documentation about how this should be done: https://developer.apple.com/library/archive/qa/qa1487/_index.html Specifically, this line:

// make the text appear in blue
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

It doesn't work no matter what I do. The color of the link is always the default one. It's sad that such simple things is so hard to achieve on macOS. Maybe someone has some ideas.

Upvotes: 0

Views: 26

Answers (0)

Related Questions