How to install font NSMenu / NSMenuItems?

I cannot figure out how to set the font / style of my NSMenuItems in my NSMenu. I tried the setFont method in NSMenu, but it does not seem to affect menu items. NSMenuItem does not have a setFont method. I would like all of them to have the same font / style, so I would hope that only one property can be set somewhere.

+4
source share
4 answers

They can have an assigned title, so you can set the attribute string as a title with all attributes, including the font:

NSMutableAttributedString* str =[[NSMutableAttributedString alloc]initWithString: @"Title"]; [str setAttributes: @{ NSFontAttributeName : [NSFont fontWithName: @"myFont" size: 12.0] } range: NSMakeRange(0, [str length])]; [label setAttributedString: str]; 
+7
source

NSMenuItem supports attribute strings as headers:

 - (void)setAttributedTitle:(NSAttributedString *)string; 

Code example:

 NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Hi, how are you?" action:nil keyEquivalent:@""]; NSDictionary *attributes = @{ NSFontAttributeName: [NSFont fontWithName:@"Comic Sans MS" size:19.0], NSForegroundColorAttributeName: [NSColor greenColor] }; NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:[menuItem title] attributes:attributes]; [menuItem setAttributedTitle:attributedTitle]; 

Documentation: https://developer.apple.com/library/mac/#documentation/cocoa/reference/applicationkit/classes/nsmenuitem_class/reference/reference.html

+8
source

Actually [NSMenu setFont:] works for all submenus of menu items (if the latter do not have their own font). Maybe you set the attribute name before setting the menu font? I realized this by writing my own procedure for repeating menu items.

If you need some custom processing (i.e. changing the font for not all elements or adjusting it for different elements), here is a simple iterative code:

 @implementation NSMenu (MenuAdditions) - (void) changeMenuFont:(NSFont*)aFont { for (NSMenuItem* anItem in self.itemArray) { NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:aFont forKey:NSFontAttributeName]; anItem.attributedTitle = [[[NSAttributedString alloc] initWithString:anItem.title attributes:attrsDictionary] autorelease]; if (anItem.submenu) [anItem.submenu changeMenuFont:aFont]; } } @end 
+1
source

+ menuBarFontOfSize: from NSFont is your friend here.

  • If you do not plan to change the font family, you should use [NSFont menuBarFontOfSize:12] to get the default font and set a new size.
  • If you just change the color, you still need to set the default font size by doing [NSFont menuBarFontOfSize:0] .

So, to change the color of NSMenuItem :

 NSDictionary *attributes = @{ NSFontAttributeName: [NSFont menuBarFontOfSize:0], NSForegroundColorAttributeName: [NSColor greenColor] }; NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:[menuItem title] attributes:attributes]; [menuItem setAttributedTitle:attributedTitle]; 
+1
source

Source: https://habr.com/ru/post/1447030/


All Articles