2011-06-18 62 views
5

當我將我的按鈕設置爲禁用時,文本變爲灰色(之前爲黑色)。 在我的窗口中,結果是當禁用按鈕時文本不可讀。當禁用按鈕時,是否有辦法防止文本變灰?

我找遍了整個NSButton/NSButtonCell /的NSCell/NSControl的這些文件中,但我沒有發現任何方式保持文本的黑色。你知道我該怎麼做嗎?

+0

你能覆蓋按鈕的特性,而不是將其禁用 - 但也許禁用互動,或者只是改變按鈕的顏色/邊框,以保留您的文字顏色?只是想知道你是否嘗試過這種修復方法,但我知道你在說什麼。 – Luke

+0

mmmm ..我想我可以設置圖像/備用圖像相同的「禁用」的皮膚,所以用戶會明白,這是禁用的。但它會使代碼非常難看 - 我將不得不檢查每個動作函數,如果該按鈕被禁用或不是 –

+0

在iOS中,我可以這樣做:myButton.userInteractionDisabled = YES,然後設置alpha以使其外觀被禁用。我想你可以用你的NSButton做類似的事情。只需確認,您只需將啓用的屬性設置爲NO,即您的文本顏色問題來自哪裏? – Luke

回答

1

您可以爲IB中的每個狀態設置按鈕屬性(字體,顏色)。那麼將其禁用狀態的文本顏色設置爲黑色幫助?

+0

我沒有看到這樣的選項在IB。我使用內置IB的xcode 4 –

+0

可可不是可可觸摸。 – Kappe

-1

在可可觸摸存在對一個API:

[myButton的setTextColor:的UIColor blackColor] forState:UIControlStateDisabled];

對於可可我不知道。

+4

解決了Cocoa Touch的問題,但解決了Cocoa的問題,這正是詢問者提出的問題。 –

+0

我錯過了,謝謝指出 –

0

子類NSButtonCell和它(不直接按鈕 - >更深一層)分配給IB的鈕釦電池。在子類中實現以下和更改大小,字體和顏色,只要你喜歡:

- (NSAttributedString*)attributedTitleForString:(NSString*)string 
{ 
    // Colors 
    NSColor *disabledColor = [NSColor grayColor]; 
    NSColor *enabledColor = [NSColor redColor]; 

    // Font 
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
    [style setAlignment:NSLeftTextAlignment]; 
    NSFont *font = [NSFont systemFontOfSize:11]; 

    // Enabled 
    NSDictionary *enabledAttrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
             enabledColor, NSForegroundColorAttributeName, 
             style, NSParagraphStyleAttributeName, 
             font, NSFontAttributeName, 
             nil]; 
    NSAttributedString* enabledTitle = [[NSAttributedString alloc]initWithString:string attributes:enabledAttrsDictionary]; 

    // Disabled 
    NSDictionary *disabledAttrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
             disabledColor, NSForegroundColorAttributeName, 
             style, NSParagraphStyleAttributeName, 
             font, NSFontAttributeName, nil]; 

    NSAttributedString* disabledTitle = [[NSAttributedString alloc]initWithString:string attributes:disabledAttrsDictionary]; 

    if (![self isEnabled]) 
     return disabledTitle; 
    else 
     return enabledTitle; 
} 

編輯:只工作,如果setWantsLayers是假

相關問題