2011-07-27 26 views
7

我有一個NSStatusItem,我用一個屬性串吧,設置是這樣:高亮NSStatusItem與屬性串

[statusItem setAttributedTitle:as]; 

其中as是我的屬性串。我用它來突出特定項目的某些部分,當滿足某些條件時,通過對它們進行不同的着色。因此,例如,我的狀態項目可能包含一些紅色文本和一些黑色文本。

現在的問題是,當我使用setAttributedTitle,然後單擊狀態項目時,顏色不會反轉,因爲我希望他們。例如,當我僅使用setTitle時,文本在未選中時爲黑色,選中時爲白色。現在它只是保持我設置的顏色。

有沒有辦法讓它在選擇時反轉顏色?如果不是,我該如何做到這一點?對不起,我是Objective-C的初學者。

+0

你有沒有打過電話'setHighlightMode:YES '? – jtbandes

+0

@jtbandes:是的,但只有當突出顯示時才控制藍色背景,看起來似乎。 – houbysoft

回答

4

貌似要做到這一點的唯一方法是:

  • 不使用setMenu:

  • 設置爲statusItem菜單而是使用setAction:,改變字符串的顏色,顯示菜單,然後更改顏色返回

對於ins ,孟清湘,使用類似:

[statusItem setAction:@selector(statusItemClicked)]; 

並實現statusItemClicked方法是這樣的:

- (void) statusItemClicked { 

    // change color of attributed string to its highlighted state here 

    [statusItem popUpStatusItemMenu:statusItemMenu]; // show the menu 
                // which used to be set 
                // using setMenu: 

    // change color of attributed string back its non-highlighted state here 
} 
2

可以實現以下NSMenuDelegate方法:

- (void) menuWillOpen:(NSMenu *) aMenu { 
    // use an attributed string to set the title to your highlighted color 
} 


- (void) menuDidClose:(NSMenu *) aMenu { 
    // use an attributed string to set the title black 
} 

[statusItem setMenu:[self menu]]; 
[[self menu] setDelegate:self];