Reputation: 7391
What I want to implement
I am looking for a way to display a title label and a detail text label in a NSMenuItem
.
Preferably it would look something like this:
NSMenuItem
's
titles What have I tried already
By reading the documentation I found the following possible implementations:
NSView
and set NSMenuItem.view
NSMenuItem
and use a NSAttributedString
First I tried to use a custom NSView
. However I could not get the NSMenuItem
to size correctly in order to display all the available text. I guess some autoresizing masks do not work correctly but I am not sure. Also this way I would need to re-implement selection/arrow for the submenu, ...
Then I started to experiment with NSAttributedString
. I calculate the title with the most characters and then pad the string with title += string.padding(toLength: maxTitleLength, withPad: " ", startingAt: 0)
. The NSAttributedString
colors the title and the detail label differently. However this does not seem to work since the detail labels are not correctly aligned although the title is padded to the same length. I guess this makes sense since characters have different widths?
TL;DR - Question
So is there any other way to implement the desired design which I did not find? Do you have any advice for me on how to implement this?
Upvotes: 1
Views: 795
Reputation: 897
I am the creator of the app in the screenshot.
These are standard NSMenuItems, not custom views.
It is just a NSAttributedString with NSTextTab(type: .rightTabStopType, location: longestTitleWidth)
and different text color.
Upvotes: 3
Reputation: 7391
Nevermind I found the answer myself. Actually it works for me now by using the view
property of the NSMenuItem
to set a custom view. This answer lead me to the right direction: Highlighting a NSMenuItem with a custom view?.
Upvotes: 1