2013-06-24 42 views
1

可能會改變一個菜單欄應用程序標題字體大小的簡單方法存在,使得@「標題」顯示比默認小(或更大)更改菜單欄應用程序的字體大小

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

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

回答

1

我發現這工作

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

感謝

1

您可以更改字體您的狀態項目如下:

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

系統菜單欄將不會重新大小以適應字體過大,以適應在它的,所以你不能讓字體過大。

+0

大多數原因的使用這種代碼,收到錯誤和警告消息,此錯誤消息顯示「以前的預期表達式@'標記,並且此警告消息顯示」'NSMutableAttributedString'可能不會響應'-setAttributes:'。有機會我刪除這個「[attrStr setAttributes:」部分,這個代碼似乎工作 – coltcdc

0

兩個答案不爲我工作了類似的方法=( 狀態欄具有相同的字體

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

有機會你喜歡設置標題或有「加載...」顯示,你有initWithString:@「title」,可能機會「title」爲「正在加載...」,同時刪除[statusItem setTitle:@「Loading」];線,我還建議添加「保留」到statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] ;,寫在我發佈的代碼集 – coltcdc

+0

我添加了一個答案,我用代碼實現了一個圖標也是一個標題顯示 – coltcdc

2

迴應尤金,實現了ic上還我使用的方法的標題顯示我打算後下

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

我打算髮布鏈接到教程,體面解釋了某些代碼 http://www.sonsothunder.com/devres/revolution/tutorials/StatusMenu.html

+1

我編輯了這篇文章,添加了一個教程的鏈接,可以正確解釋某些代碼的大部分原因 – coltcdc