更改 NSStatusBar 字体颜色和大小

Change NSStatusBar font color and size

我的项目中有一个自定义的状态栏(macOS、Xcode 10、Cocoa、Swift)。

我想更改将显示在状态中的文本的字体和颜色。

@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....
    }

感谢 Vadian,我把答案放在这里。 (Swift 4) 希望这对未来的读者有所帮助。

@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

    }