Macos 更改菜单栏应用程序的字体大小

Macos 更改菜单栏应用程序的字体大小,macos,cocoa,nsstatusbar,Macos,Cocoa,Nsstatusbar,是否存在一种简单的方法来更改菜单栏应用程序标题的字体大小,使@“title”显示比默认值更小(或更大) statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; [statusItem setHighlightMode:YES]; [statusItem setTitle:@"title"]; [statusItem setMenu:statusM

是否存在一种简单的方法来更改菜单栏应用程序标题的字体大小,使@“title”显示比默认值更小(或更大)

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

[statusItem setHighlightMode:YES];
[statusItem setTitle:@"title"];
[statusItem setMenu:statusMenu];

您可以按如下方式更改状态项的字体:

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];

但是,系统菜单栏不会重新调整大小以容纳过大而无法容纳的字体,因此您不能使字体过大。

您可以按如下方式更改状态项的字体:

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];

但是,系统菜单栏不会重新调整大小以容纳过大而无法容纳的字体,因此您不能将字体设置得过大。

我发现了一种类似的方法

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];


谢谢

我找到了一种类似的方法,效果很好

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];


谢谢

两个答案对我来说都不合适=( 状态栏的字体相同

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];

这两个答案对我都不适用=( 状态栏的字体相同

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];

回应Eugene,为了实现图标和标题的显示,我使用了我计划在下面发布的方法

statusItem=[[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength]retain];
NSFont*font=[NSFont fontWithName:@“LucidaGrande”大小:12.0];
NSDictionary*attrsDictionary=[NSDictionary Dictionary WithObject:font forKey:NSFontAttributeName];
NSAttributedString*attrString=[[NSAttributedString alloc]initWithString:@“title”属性:attrsDistrictionary];
NSBundle*bundle=[NSBundle mainBundle];
statusImage=[[NSImage alloc]initWithContentsOfFile:[bundle pathForResource:@“icon”类型:@“png”];
statusHighlightImage=[[NSImage alloc]initWithContentsOfFile:[bundle pathForResource:@“icon alt”类型:@“png”];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setHighlightMode:是];
[statusItem SetAttributeTitle:attrString];
[状态项设置菜单:状态菜单];
我计划发布一个到教程的链接,它体面地解释了某些代码的大部分原因

响应Eugene,为了实现图标和标题的显示,我使用了我计划在下面发布的方法

statusItem=[[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength]retain];
NSFont*font=[NSFont fontWithName:@“LucidaGrande”大小:12.0];
NSDictionary*attrsDictionary=[NSDictionary Dictionary WithObject:font forKey:NSFontAttributeName];
NSAttributedString*attrString=[[NSAttributedString alloc]initWithString:@“title”属性:attrsDistrictionary];
NSBundle*bundle=[NSBundle mainBundle];
statusImage=[[NSImage alloc]initWithContentsOfFile:[bundle pathForResource:@“icon”类型:@“png”];
statusHighlightImage=[[NSImage alloc]initWithContentsOfFile:[bundle pathForResource:@“icon alt”类型:@“png”];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setHighlightMode:是];
[statusItem SetAttributeTitle:attrString];
[状态项设置菜单:状态菜单];
我计划发布一个到教程的链接,它体面地解释了某些代码的大部分原因

使用此代码,我收到一条错误和警告消息,此错误消息显示“@”标记之前的预期表达式,此警告消息显示“'NSMutableAttributeString'可能不响应“-setAttributes:”。如果我删除此”[attrStr setAttributes:节,此代码似乎正常使用此代码我收到一个错误和警告消息,此错误消息显示“在“@”标记之前需要表达式,此警告消息显示“'nSmutableAttributeString'可能不响应“-setAttributes:”。如果我删除此“[attrStr setAttributes:”第节,此代码似乎在您希望设置标题或显示“加载…”的情况下运行,您可以在其中使用initWithString:@“title”,也可以将“title”更改为“加载…”,还可以删除[statusItem setTitle:@“Loading”];第行,我还建议将“retain”添加到statusItem=[[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength];,正如我在我发布的两组代码中所写的那样,我添加了一个答案,其中的代码我用于实现在希望设置标题或“加载…”显示的机会上同时显示图标和标题,其中initWithString:@“title”,可能是机会“title”到“加载…”,也删除[statusItem setTitle:@“Loading”];行中,我还建议将“retain”添加到statusItem=[[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength],正如我在两组代码中所写的一样,我用我用来实现的代码添加了一个答案,既有一个图标,也有一个标题显示我编辑了这篇主文,添加了一个指向教程的链接,该教程恰当地解释了某些代码的大部分原因我编辑了这篇主文,添加了指向教程的链接,该教程恰当地解释了ce的大部分原因不确定码