我有一个基于视图的自定义NSTableCellView的NSTableView。此自定义NSTableCellView具有多个标签(NSTextField)。NSTableCellView的整个UI都是在IB中构建的。
NSTableCellView可以处于正常状态和处于选择状态。在正常状态下,所有文本标签都应该是黑色的,在选中状态下,它们应该是白色的。
我该如何管理它呢?
发布于 2012-02-11 06:15:59
完成此操作的最简单方法可能是子类NSTextField,并覆盖子类中的drawRect:方法。在那里,您可以使用以下代码确定当前是否选择了包含您的NSTextField实例的NSTableCellView实例(我在NSOutlineView中使用该代码,但它也应该在NSTableView中使用):
BOOL selected = NO;
id tableView = [[[self superview] superview] superview];
if ([tableView isKindOfClass:[NSTableView class]]) {
NSInteger row = [tableView selectedRow];
if (row != -1) {
id cellView = [tableView viewAtColumn:0 row:row makeIfNecessary:YES];
if ([cellView isEqualTo:[self superview]]) selected = YES;
}
}然后绘制视图,如下所示:
if (selected) {
// set your color here
// draw [self stringValue] here in [self bounds]
} else {
// call [super drawRect]
}发布于 2012-03-07 13:40:21
覆盖setBackgroundStyle:在NSTableCellView上,了解背景何时发生变化,这会影响您在单元格中应该使用的文本颜色。
例如:
- (void)setBackgroundStyle:(NSBackgroundStyle)style
{
[super setBackgroundStyle:style];
// If the cell's text color is black, this sets it to white
[((NSCell *)self.descriptionField.cell) setBackgroundStyle:style];
// Otherwise you need to change the color manually
switch (style) {
case NSBackgroundStyleLight:
[self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:0.4 alpha:1.0]];
break;
case NSBackgroundStyleDark:
default:
[self.descriptionField setTextColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
break;
}
}在源列表视图中,单元格视图的背景样式设置为浅色,它的textField的backgroundStyle也是如此,但是textField也在其文本下绘制阴影,并且还没有找到确切的控制/确定它应该发生的因素。
发布于 2013-08-15 22:47:25
无论表视图的样式是什么,这都是有效的:
- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
[super setBackgroundStyle:backgroundStyle];
NSTableView *tableView = self.enclosingScrollView.documentView;
BOOL tableViewIsFirstResponder = [tableView isEqual:[self.window firstResponder]];
NSColor *color = nil;
if(backgroundStyle == NSBackgroundStyleLight) {
color = tableViewIsFirstResponder ? [NSColor lightGrayColor] : [NSColor darkGrayColor];
} else {
color = [NSColor whiteColor];
}
myTextField.textColor = color;
}https://stackoverflow.com/questions/9149138
复制相似问题