NSTextView has the property:
var linkTextAttributes: [NSAttributedString.Key : Any]? { get set }
That is used for:
The attributes used to draw the onscreen presentation of link text.
Link text attributes are applied as temporary attributes to any text with a link attribute.
I managed to change the attributes as follow:
import Cocoa
class ChangeColorVisitedLinksViewController: NSViewController {
@IBOutlet var textView: NSTextView!
var linkRanges = [NSRange]()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
let string = textView.string
let types = NSTextCheckingResult.CheckingType.link
let detector = try? NSDataDetector(types: types.rawValue)
if let matches = detector?.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count)){
for match in matches {
linkRanges.append(match.range)
}
}
}
}
extension ChangeColorVisitedLinksViewController : NSTextViewDelegate{
func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool{
for linkRange in linkRanges{
if linkRange.contains(charIndex){
guard let textStorage = textView.textStorage else { return false }
textStorage.setAttributes([NSAttributedString.Key.foregroundColor: NSColor.red] , range: linkRange)
}
}
return false
}
}