1

can you explain to me how to make an app on osx (with swift 3) showing only (without icon on the dock) two row of text in the menu bar like in the picture below ? or at least how to show text on the menu bar ?

OSX Menu bar picture: osx menu bar picture

PS: I am new on osx app dev so don't hesitate to be very verbose ;)

MatejMecka
  • 1,448
  • 2
  • 24
  • 37
  • See the following topic. It's for Objective-C. But you will get some idea. http://stackoverflow.com/questions/21896172/mac-status-bar-application-not-working/21898566#21898566 – El Tomato Dec 16 '16 at 23:44

2 Answers2

5
  • This kind of app is called menu bar app or menulet.
  • The dockless appearance is set by the key LSUIElement (1) in Info.plist
  • Use a custom NSView
  • Assign the view as a subview to the view of the button of the NSStatusItem
  • Draw the text in drawRect

Important: Using a custom view requires to implement all methods to handle the highlighting, mouse clicks and NSMenu delegate.

Here is an article how to do it, it's in Objective-C but this might be a starting point:
Adding a Custom View to an NSStatusItem

vadian
  • 274,689
  • 30
  • 353
  • 361
2
var statusBarItem: NSStatusItem?

func applicationDidFinishLaunching(_ notification: Notification) {
    statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    if let button = self.statusBarItem?.button {
        let style = NSMutableParagraphStyle()
        style.maximumLineHeight = 10
        style.alignment = NSTextAlignment.left
        let attributes = [NSAttributedString.Key.paragraphStyle: style, NSAttributedString.Key.font: NSFont.systemFont(ofSize: 10.0), NSAttributedString.Key.baselineOffset: -5] as [NSAttributedString.Key : Any]
        let textString = "Line1\nLine2"
        let attributedTitle = NSAttributedString(string: textString, attributes: attributes)
        button.attributedTitle = attributedTitle
    }
}
Shin
  • 21
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '21 at 08:06