NSTableCellView 中的 NSTextField

发布于 2025-01-02 14:22:38 字数 220 浏览 6 评论 0原文

我有一个基于视图的 NSTableView 和一个自定义 NSTableCellView。这个自定义的 NSTableCellView 有几个标签(NSTextField)。 NSTableCellView 的整个 UI 都是用 IB 构建的。

NSTableCellView可以处于正常状态和选中状态。在正常状态下,所有文本标签应为黑色,在选定状态下,它们应为白色。

我该如何处理这个问题?

I have a view based NSTableView with a custom NSTableCellView. This custom NSTableCellView has several labels (NSTextField). The whole UI of the NSTableCellView is built in IB.

The NSTableCellView can be in a normal state and in a selected state. In the normal state all text labels should be black, in the selected state they should be white.

How can I manage this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千紇 2025-01-09 14:22:38

重写 NSTableCellView 上的 setBackgroundStyle: 来了解背景何时发生变化,这会影响您应该在单元格中使用的文本颜色。

例如:

- (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;
    }
}

在源列表表视图中,单元格视图的背景样式设置为“浅色”,其文本字段的背景样式也是如此,但是文本字段还在其文本下方绘制阴影,并且尚未找到确切的控制/确定应该它的原因发生。

Override setBackgroundStyle: on the NSTableCellView to know when the background changes which is what affects what text color you should use in your cell.

For instance:

- (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;
    }
}

In source list table views the cell view's background style is set to Light, as is its textField's backgroundStyle, however the textField also draws a shadow under its text and haven't yet found exactly what is controlling that / determining that should it happen.

留蓝 2025-01-09 14:22:38

完成此操作的最简单方法可能是子类化 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]
}

Probably the easiest way to accomplish this would be to subclass NSTextField and to override the drawRect: method in your subclass. There you can determine whether the NSTableCellView instance containing your NSTextField instances is currently selected by using this code (which I use with a NSOutlineView, but it should also work with 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;
    }
}

Then draw the view like this:

if (selected) {
    // set your color here
    // draw [self stringValue] here in [self bounds]
} else {
    // call [super drawRect]
}
╰ゝ天使的微笑 2025-01-09 14:22:38

无论表视图具有什么样式,这都有效:

- (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;
}

This works no matter what style the table view has:

- (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;
}
流年已逝 2025-01-09 14:22:38

斯威夫特4

 override var backgroundStyle: NSView.BackgroundStyle {
     get {
        return super.backgroundStyle
     }
     set {
        self.yourCustomLabel.textColor = NSColor(calibratedWhite: 0.0, alpha: 1.0)//black
     }
 }

Swift 4

 override var backgroundStyle: NSView.BackgroundStyle {
     get {
        return super.backgroundStyle
     }
     set {
        self.yourCustomLabel.textColor = NSColor(calibratedWhite: 0.0, alpha: 1.0)//black
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文