2011-07-04 7 views
2

NSAutlineViewにNSAttributedStringを使用してフォーマットしたデータを入力しています。これまでのところ、私はテキストフォント、サイズ、色をフォーマットしました。私の問題は、行が選択されても前景色が変わらないことです。 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]; 
} 
を働きました
関連する問題