Cocoa NSOutlineView源代码列表样式,基于视图,更改字体

Cocoa NSOutlineView源代码列表样式,基于视图,更改字体,cocoa,nsoutlineview,Cocoa,Nsoutlineview,我使用源列表样式的NSOutlineView,并使用基于视图(而不是基于单元格)的大纲视图 我希望能够将一些行加粗。但是,到目前为止,我试图更改字体(在IB中手动更改,通过viewForTableColumn中的代码更改:…,或通过字体粗体绑定更改)的尝试被忽略 从中,这似乎是因为NSOutlineView的源列表样式接管了文本字段外观的管理: 我猜您已经将文本字段连接到NSTableCellView的文本字段出口了?如果是这样,我想您可能会遇到NSTableView对源列表外观的自动管理 尝试

我使用源列表样式的NSOutlineView,并使用基于视图(而不是基于单元格)的大纲视图

我希望能够将一些行加粗。但是,到目前为止,我试图更改字体(在IB中手动更改,通过viewForTableColumn中的代码更改:…,或通过字体粗体绑定更改)的尝试被忽略

从中,这似乎是因为NSOutlineView的源列表样式接管了文本字段外观的管理:

我猜您已经将文本字段连接到NSTableCellView的文本字段出口了?如果是这样,我想您可能会遇到NSTableView对源列表外观的自动管理

尝试断开文本字段与文本字段出口的连接,查看自定义字体是否保持不变

如果我断开textField插座,外观确实在我的控制之下,我的勇气也在发挥作用

然而,现在我不能让它看起来像自动的。我的意思是,当NSOutlineView管理文本字段的外观时,字体是粗体的,并且在选择任何项目时会出现阴影,但当我手动管理它时,情况并非如此

任何人都可以回答以下问题之一:

  • 当NSOutlineView管理文本字段的外观时,如何使字体粗体绑定工作
  • 如果我没有NSOutlineView管理文本字段的外观,我如何使其外观和行为与我管理文本字段时的外观和行为相同

  • 我想我找到了解决办法:

    NSTableCellView
    通过在包含控件的单元格上设置
    backgroundStyle
    属性来管理其
    textField
    出口的外观。将其设置为
    NSBackgroundStyleDark
    将触发
    NSTextFieldCell
    中的特殊路径,该路径实质上设置了
    attributedStringValue
    ,通过
    NSShadowAttributeName
    更改文本颜色并添加阴影

    你可以做两件事:

    • 在自定义行或单元格视图子类中自行设置
      backgroundStyle
    • 在单元格的文本字段中使用自定义的
      NSTextFieldCell
      ,并更改行为/图形
    我们做了后者,因为我们需要一个不同的主题(不同颜色)表视图的外观。为此,我们找到的最方便(尽管肯定不是最有效)的位置是重写
    -drawInteriorWithFrame:inView:
    ,并在调用super之前修改单元格的属性字符串,然后恢复原始字符串:

    - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
    {
        NSAttributedString *originalString = self.attributedStringValue;
    
        // Customize string as you like
        if (/* whatever */)
            [self setAttributedStringValue: /* some string */];
    
        // Regular drawing
        [super drawInteriorWithFrame:cellFrame inView:controlView];
    
        // Reset string
        if (self.attributedStringValue != originalString)
            self.attributedStringValue = originalString;
    }
    

    希望这能在类似情况下帮助其他人。

    不确定我是否遗漏了你问题中的任何内容,但使用以下方法更改字体对我来说很有用
    RememberTableCellView
    只是NSTableCellView的一个子类,添加了一个额外的日期字段

    - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
        //LOG(@"viewForTableColumn called");
        // For the groups, we just return a regular text view.
        if ([_topLevelItems containsObject:item]) {
            //LOG(@" top level");
            NSTableCellView *result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
            // Uppercase the string value, but don't set anything else. NSOutlineView automatically applies attributes as necessary
            NSString *value = [item uppercaseString];
            [result.textField setStringValue:value];
            //[result.textField setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
            return result;
        } else  {
            //LOG(@" menu item");
            // The cell is setup in IB. The textField and imageView outlets are properly setup.
            // Special attributes are automatically applied by NSTableView/NSOutlineView for the source list
            ReminderTableCellView *result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
            if ([item isKindOfClass:[OSTreeNode class]]) {
                [result.textField setFont:[NSFont boldSystemFontOfSize:13]];
                result.textField.stringValue = [item displayName];
                result.dateField.stringValue = [item nextReminderDateAsString];
            }
            else
                result.textField.stringValue = [item description];
            if (_loading)
                result.textField.textColor = [NSColor grayColor];
            else
                result.textField.textColor = [NSColor textColor];
            NSImage *image = [NSImage imageNamed:@"ReminderMenuIcon.png"];
            [image setSize:NSMakeSize(16,16)];
            [result.imageView setImage:image];
            //[result.imageView setImage:nil];
            return result;
        }
    }
    
    结果视图如下所示。注意,这是一个选择了源代码列表选项的NSOutlineView,但我不明白为什么这对普通的outlineView不起作用


    自提出问题以来,您获得了什么见解?我有一个类似的问题:(我想我放弃了使用图标,而不是改变字体。这可能是可能的,但我从来没有发现是怎么回事。谢谢你。这很有帮助。