【问题标题】:NSTextField in NSTableCellViewNSTableCellView 中的 NSTextField
【发布时间】:2012-02-05 12:13:43
【问题描述】:

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

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

我该如何管理?

【问题讨论】:

    标签: macos cocoa nstableview nstextfield


    【解决方案1】:

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

    在源列表视图中,单元格视图的背景样式设置为 Light,它的 textField 的 backgroundStyle 也是如此,但是 textField 还在其文本下绘制了阴影,并且还没有找到确切的控制/确定应该它的东西发生。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 赛斯建议的方法是正确的方法,应该标记为正确答案。上面的方法是错误的,而且它会对滚动/绘图性能产生影响
      • 不幸的是,Seth 的解决方案仅适用于“源列表”样式的表格视图。在标准样式的表格视图中,只有蓝色选定状态是 NSBackgroundStyleDark。如果选择了一个项目但表格视图不是第一响应者,你会得到一个灰色的行突出显示,这就是 NSBackgroundStyleLight...
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        斯威夫特 4

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-25
          • 1970-01-01
          • 1970-01-01
          • 2015-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多