2011-07-04 30 views
2

我使用NSAttributedString格式化數據填充NSOutlineView。到目前爲止,我已經格式化了文本的字體,大小和顏色。我的問題是,當選擇該行時,前景色不會改變。如果你在界面生成器上創建一個NSTextFieldCell並將其顏色設置爲disabledControlTextColor,它可以正常工作:未選中時顯示爲灰色,當選擇白色時,當我以編程方式將此顏色設置爲屬性字符串定義時,它始終顯示爲灰色。NSCell上的NSAttributedString

NSMutableAttributedString *result = [[[NSMutableAttributedString alloc] initWithString:value] autorelease]; 
NSDictionary *attributes = [[NSDictionary dictionaryWithObjectsAndKeys: 
            [NSFont systemFontOfSize:[NSFont systemFontSize] -1], NSFontAttributeName, 
            [NSColor disabledControlTextColor], NSForegroundColorAttributeName, nil] retain]; 

[result addAttributes:attributes range:[value rangeOfString:value]]; 

在此先感謝。

回答

5

對NSCell進行子類化時,設置文本字段值時,我們應該詢問單元格是否被高亮顯示,然後設置文本的前景色。

NSString *titleValue = @"TEST"; 
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:titleValue];  
NSColor *color = [self isHighlighted] ? [NSColor whiteColor] : [NSColor blackColor]; 
NSDictionary *attributes = [[NSDictionary dictionaryWithObjectsAndKeys: 
             [NSFont boldSystemFontOfSize:[NSFont systemFontSize] + 1], NSFontAttributeName, 
             color, NSForegroundColorAttributeName, nil] autorelease]; 
[titleString addAttributes:attributes range:[titleValue rangeOfString:titleValue]]; 
[self setAttributedStringValue:value]; 
0

使用這種自定義單元格,我想在互聯網上的一切,終於下面的事情工作

- (void)updateCellDisplay { 
    if (self.selected || self.highlighted) { 
    self.nameLabel.textColor = [UIColor lightGrayColor]; 
    self.colorLabel.textColor = [UIColor lightGrayColor]; 
    } 
    else { 
    self.nameLabel.textColor = [UIColor blackColor]; 
    self.colorLabel.textColor = [UIColor blackColor]; 
    } 
} 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 
    [super setHighlighted:highlighted animated:animated]; 
    [self updateCellDisplay]; 
} 

- (void) setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
    [self updateCellDisplay]; 
}