How do I change the short display form of an NSMenuItem in an NSPopUpButton?
I have an NSPopUpButton with a long menu of items, some of which are quite wide - wider than the button's closed width. Some of the menu item titles are like so:
"Start of a really long name not that interesting (important info)"
When the menu is closed, this displays as:
"Start of a really long name not th..."
I'd like it to 开发者_StackOverflowdisplay as:
"Start of a really l... (important info)"
I can't figure out how NSPopUpButton is creating that ellipsis. Is there a selector being called on NSMenuItem? Does NSPopUpButton or NSMenu handle that somehow?
Which class do I need to subclass and which selector to I need to override or implement?
Many thanks.
This mechanism is called truncation. There is an option in Interface Builder called Line Breaks which is grouped in the Control group. The setting you're probably looking for is called Truncate Middle
which will start picking letters from the middle of the menu item.
Note however that this won't solve your problem entirely as the (important info)
part might not be the only one remaining after "…", but rather Name of my Me…tem (important info)
. Furthermore you might also end up with the important info partially truncated. But I figure you're fine as long as the important info is some number or a few letters.
Maybe adding an image to the menu item might suit your needs more (some sort of badge)? Maybe there is also an option for attributed strings to force letters to not be truncated… not sure however.
Fabian gave me the clue I needed. Thanks!
[[self cell] setLineBreakMode: NSLineBreakByTruncatingMiddle]
However, if there is a way to get finer-grained control, that would be even better. Is there a way to control the truncation more precisely?
精彩评论