Might a simple method of changing a menu bar application titles font size exist, making @"title" display smaller (or larger) than default

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];

[statusItem setHighlightMode:YES];
[statusItem setTitle:@"title"];
[statusItem setMenu:statusMenu];
有帮助吗?

解决方案 2

I found a similar method which works

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];

NSFont *font = [NSFont fontWithName:@"LucidaGrande" size:12.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"title" attributes:attrsDictionary];

[statusItem setHighlightMode:YES];
[statusItem setAttributedTitle:attrString];
[statusItem setMenu:statusMenu];

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/CreatingAttributedStrings.html

thanks

其他提示

Responding to Eugene, to achieve having an icon also a title display I used a method I plan to post below

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];

NSFont *font = [NSFont fontWithName:@"LucidaGrande" size:12.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"title" attributes:attrsDictionary];

NSBundle *bundle = [NSBundle mainBundle];

statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];

[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setHighlightMode:YES];
[statusItem setAttributedTitle:attrString];
[statusItem setMenu:statusMenu];

i plan to post a link to tutorial which decently explains a majority of reason for certain code http://www.sonsothunder.com/devres/revolution/tutorials/StatusMenu.html

You can change the font of your status item as follows:

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:@"MyTitle"];
[attrStr setAttributes:@{NSFontAttributeName: [NSFont systemFontOfSize:22]}
                 range:NSMakeRange(0,7)];

[statusItem setHighlightMode:YES];
[statusItem setAttributedTitle:attrStr];

The system menu bar will not re-size to accommodate a font that is too large to fit within it though, so you can't make the font too large.

both answers don't work for me =( Status bar has same font

NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:9.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"title" attributes:attrsDictionary];


statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
NSImage *statusImage = [NSImage imageNamed:@"icon.png"];
[statusItem setImage:statusImage];
NSImage *altStatusImage = [NSImage imageNamed:@"icon.png"];
[statusItem setAlternateImage:altStatusImage];
[statusItem setHighlightMode:YES];
[statusItem setAttributedTitle:attrString];
[statusItem setTitle:@"Loading..."];
[statusItem setMenu:statusMenu];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top