在的NSTextField NSTableCellView

问题描述 投票:5回答:4

我有一个基于NSTableView的一个自定义NSTableCellView的视图。这种风俗NSTableCellView有几个标签(的NSTextField)。该NSTableCellView的整个UI是内置的IB。

所述NSTableCellView可以是在正常状态下和在所选择的状态。在正常状态下的所有文字标签应该是黑色的,在选择的状态,他们应该是白色的。

如何管理呢?

macos cocoa nstableview nstextfield
4个回答
0
投票

可能做到这一点的最简单的方法是继承的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]
}

14
投票

覆盖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;
    }
}

在源列表表视图单元格视图的背景样式设置为轻,这是它的文本框的backgroundStyle,但文本字段还利用其文本下一个阴影,尚未发现究竟是什么控制的是/确定它应该发生。


0
投票

此作品不管表视图有什么样的风格:

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

0
投票

斯威夫特4

 override var backgroundStyle: NSView.BackgroundStyle {
     get {
        return super.backgroundStyle
     }
     set {
        self.yourCustomLabel.textColor = NSColor(calibratedWhite: 0.0, alpha: 1.0)//black
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.