2

我正在用我使用 NSAttributedString 格式化的数据填充 NSOutlineView。到目前为止,我已经格式化了文本字体、大小和颜色。我的问题是选择行时前景色不会改变。如果您创建一个Nstextfieldcell并将颜色设置为接口构建器上的禁用ControltextColor,则可以正常工作:当未选择选择时,它是灰色的,当我选择白色时,当我编程将此颜色设置为属性字符串定义时,它总是显示为始终显示为灰色的。

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的时候,在设置textfield值的时候,我们应该先询问cell是否是Highlighted,然后再设置文本的前景色。

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 回答