6

How can I change the height of an NSMenuItem? When I change the font of a menu, an NSMenuItem automatically resizes to just fit the title, leaving no space above or below the title. It looks very cramped.

Looks like this:

enter image description here

Want it to look like this:

enter image description here

I've tried a million tweaks related to the attributed title of the menu items, but to no avail. I also don't want to use the view property of the menu items because I want to keep the highlight. Any other ideas?

Edit: This is what I'd like (more or less), but based on NSMenu, not redoing it from the ground up.

enter image description here

danjonweb
  • 456
  • 3
  • 14

4 Answers4

8

You can set an empty 1-pixel wide image with the desired height:

NSImage *image=[[NSImage alloc]initWithSize:NSMakeSize(1,30)];

[menuItem setImage:image];

Obviously you end up with titles that are offset 1 pixel to the right, though that may be acceptable if uniformly applied.

Gerd K
  • 2,933
  • 1
  • 20
  • 23
  • Thanks, it worked well. I'll accept this as the answer in a day unless something better comes up. I guess the "best" solution would be to re-implement NSMenu from the ground up, but that just doesn't seem worth it. I edited the original question to show a picture of the iTunes 11 menu, which is more or less what I'm going for. – danjonweb Aug 03 '13 at 21:05
  • Nice outside of the box thinking xD – IluTov Nov 23 '14 at 14:26
0
// you want height 100    
[menuItem setView:[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 0, 100)]];
Huynh Inc
  • 2,010
  • 1
  • 25
  • 42
0

An option is to use NSAttributedString as following:

let font = NSFont.systemFont(ofSize: NSFont.systemFontSize)
let fontLineHeight = (font.ascender + abs(font.descender))
let lineHeight: CGFloat = fontLineHeight * 1.4
let style = NSMutableParagraphStyle()
style.minimumLineHeight = lineHeight
style.maximumLineHeight = lineHeight
let baselineOffset = (lineHeight - fontLineHeight) / 2
let item = NSMenuItem()
item.attributedTitle = NSAttributedString(string: title,
                                          attributes: [
                                            .paragraphStyle: style,
                                            .baselineOffset: baselineOffset
                                          ])
ericmaciel
  • 469
  • 4
  • 3
-1

If you are looking for a re-implementation of NSMenu based upon NSWindow rather than the Carbon side of things check out JGMenuWindow:

https://github.com/SquaredTiki/JGMenuWindow

Joshua
  • 15,200
  • 21
  • 100
  • 172