Change NSStatusBar font color and size

Viewed 458

I have a custom built statusbar in my project (macOS, Xcode 10, Cocoa, Swift).

I would like to change the font and the color of my text that will appear in the status.

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet private weak var mainMenu: NSMenu! 
    @IBOutlet private weak var quitMenuItem: NSMenuItem! 
    .....code....
    private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

     
    // MARK: NSApplicationDelegate
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        .....code....
        // Set the main menu
        statusItem.menu = mainMenu
        .....code....
    }
1 Answers

Thanks to Vadian I put here the answer. (Swift 4) Hope this help for future readers.

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet private weak var mainMenu: NSMenu! 
    @IBOutlet private weak var quitMenuItem: NSMenuItem! 
    .....code....
    private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

     
    // MARK: NSApplicationDelegate
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        .....code....
        // Set the main menu
        statusItem.menu = mainMenu
        
        // change color
        let myString = "My Button Title"
        let myAttribute = [ NSAttributedString.Key.foregroundColor: NSColor.blue ]
        let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
        statusItem.button?.attributedTitle = myAttrString

    }
Related