NSMenuItem height?
Asked Answered
P

4

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

Primero answered 3/8, 2013 at 10:7 Comment(0)
G
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.

Guizot answered 3/8, 2013 at 14:56 Comment(2)
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.Primero
Nice outside of the box thinking xDEstipulate
Q
1

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
                                          ])
Quiver answered 10/9, 2021 at 2:42 Comment(0)
T
0
// you want height 100    
[menuItem setView:[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 0, 100)]];
Tricornered answered 21/11, 2014 at 3:36 Comment(1)
This gets rid of all the text in the view, unfortunately.Greenland
L
-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

Lawrence answered 18/8, 2013 at 15:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.