2

NSAttributedStringを使用してフォーマットしたデータをNSOutlineViewに入力しています。これまで、テキストのフォント、サイズ、色をフォーマットしました。私の問題は、行を選択しても前景色が変わらないことです。NSTextFieldCellを作成し、Interface Builderで色をdisabledControlTextColorに設定すると、正常に機能します。選択されていない場合は灰色で表示され、白で選択されている場合は、プログラムでこの色を属性付き文字列定義に設定すると、常に次のように表示されます。グレー。

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

前もって感謝します。

4

2 に答える 2

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];
于 2011-07-07T13:05:51.643 に答える
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];
}
于 2013-01-02T20:55:03.327 に答える