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:
- The Title Label is left aligned with all the other
NSMenuItem
's titles - The Detail Text Label is right-aligned
- The Detail Text Label has a different text color than the title
- Selection/Submenus etc. work as expected
What have I tried already
By reading the documentation I found the following possible implementations:
- Create a custom
NSView
and setNSMenuItem.view
- Use a default
NSMenuItem
and use aNSAttributedString
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?