How to display multiple rows in macos menubar app?

506 Views Asked by At

Originally I thought adding new line should work. The problem is, the line height is too big. How can I make it more dense(closer to each other)?

There's the following example on git starting line 41. But apple docs say that custom view inside of NSStatusItem is deprecated. (not sure if it's deprecated for views inside of NSMenuItem)

        let statusBar = NSStatusBar.system
        statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)
        statusBarItem.button?.title = "123\n456"

UPD What I got so far. It's not Y centred though. And statusItem?.button?.frame = CGRect(x: 0.0, y: -2.0, ... seems incorrect and shifts the leading icon to the bottom.

var comb = NSMutableAttributedString(attributedString: str1)
comb.append(br) // new line
comb.append(str2)
let paragraphStyle = NSMutableParagraphStyle()
        
paragraphStyle.maximumLineHeight = 9

comb.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, comb.length))
statusItem?.button?.attributedTitle = comb

UPD (retarted approach with \n and baselineoffset)

var comb = NSMutableAttributedString(attributedString: NSAttributedString(string: "\n")) // this
comb.append(top)
comb.append(br)
comb.append(bottom)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.maximumLineHeight = 9
comb.addAttribute(NSAttributedString.Key.baselineOffset, value: 3.0, range:NSMakeRange(0, comb.length))

enter image description here

1

There are 1 best solutions below

0
kirqe On

As matt noted, use attributeTitle and NSMutableParagraph

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.maximumLineHeight = 9
paragraphStyle.alignment = .left

Columns formatting can be achieved by using tabs, spaces, or other separators

NSAttributedString(string: "\n10 \u{2022} 220 \u{2022} 4999", attributes: textAttr) // this is a row

statusItem?.button?.attributedTitle = rows

And to shift text from the bottom:

rows.addAttribute(NSAttributedString.Key.baselineOffset, value: 2.0, range:NSMakeRange(0, rows.length))

enter image description here