我用用NSAttributedString格式化的数据填充NSOutlineView。到目前为止,我已经格式化了文本的字体、大小和颜色。我的问题是,当行被选中时,前景色不会改变。如果在接口生成器上创建NSTextFieldCell并将颜色设置为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]];提前谢谢。
发布于 2011-07-07 21:05:52
当子类化NSCell时,当设置文本字段值时,我们应该询问单元格是否为isHighlighted,然后设置文本的前景颜色。
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];发布于 2013-01-03 04:55:03
在自定义单元格中使用它,我尝试了互联网上的所有东西,最后下面的东西起作用了
- (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];
}https://stackoverflow.com/questions/6576031
复制相似问题