NSFontPanel: how to preselect foreground color

Hi everybody.

This is how I've set up a viewController in order to display the shared FontPanel, thereby letting the user chose a specific font.


class GeneralViewController: NSViewController {


    @objc func changeFont(_ sender: NSFontManager) {
        let sharedFontManager = NSFontManager.shared
        .
        .
        .
        // Works just fine
    }


    @objc func changeAttributes(_ sender: AnyObject) {
        let convertedAttributes: [String: Any?] = sender.convertAttributes([:])
        guard let color = convertedAttributes[NSAttributedString.Key.foregroundColor.rawValue] as? NSColor else {
            return
        }
        .
        .
        .
        // Works just fine
    }


    @IBAction func selectFontAndColorAction(_ sender: NSButton?) {
        let sharedFontManager = NSFontManager.shared
        sharedFontManager.target = self
        triggeredBy = sender?.identifier
        let selectedFont = NSFont......
        let selectedColor = NSColor......


        sharedFontManager.setSelectedFont(selectedFont, isMultiple: false)
        /// FIX: - My try!!! Not Working!!!
        sharedFontManager.setSelectedAttributes([NSAttributedString.Key.foregroundColor.rawValue: selectedColor], isMultiple: false)
        /// My Try!!! Not Working


        sharedFontManager.orderFrontFontPanel(sender)
    }
}


So far, I've manged to show the fontPanel and I'm able to select (choose) any font properly, along with its attributes (underline style, underline color, etc.)

What I'm not able to implement (googled a lot but to no avail, docs and tutorials are very scarse) is a way to set the initial foreground color displayed in the fontpanel's attributes toolbar. I get always Black



Can anyone shed some light on the matter?


Thank you


P.S.: My dev target is AppKit, not UIKIT

Just guessing, but you probably do that by setting the default color in NSColorPanel.


[[NSColorPanel sharedColorPanel] setColor:[NSColor blueColor]];


I haven't tested this since I don't use the standard font panel. What I do know, however, is that setting things for the color panel in the app delegate, such as applicationDidFinishLaunching, sets up the panel for the whole app (it *is* a shared color panel).

NSFontPanel: how to preselect foreground color
 
 
Q