2012-11-21 48 views
5

我使用NSOutlineView与源列表样式,并使用基于视图(而不是基于单元格)大纲视图。NSOutlineView源列表样式,基于视图,更改字体

我想能够使一些行粗体。但是,我尝试更改字体(手动在IB中,通过viewForTableColumn中的代码:...或通过Font Bold绑定)至今已被忽略。

this message,看来这是因为NSOutlineView源列表样式接管管理文本字段的外观:

我猜你已经迷上了你的文本字段的文本框出口NSTableCellView的?如果是这样,我想你可能会遇到NSTableView自动管理源列表的外观。

尝试断开textField插座上的文本字段并查看您的自定义字体是否粘贴。

如果我断开textField插座,外观确实出现在我的控制之下,并且我的插件工作正常。

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

任何人都可以回答任何这些问题:

  1. 我怎样才能字体加粗绑定时NSOutlineView是管理我的文本字段
  2. 的外观。如果我没有工作NSOutlineView管理我的文本字段的外观,如何让它看起来和行为如果我确实有它的管理呢?
+0

自提问之后获得的所有见解?我有类似的问题:( –

+0

我想我放弃了,并使用了一个图标,而不是改变字体。这可能是可能的,但我从来没有发现如何。 –

回答

6

我想我找到了解决办法:

NSTableCellView管理的外观是textField出口通过设置包含的控件的细胞backgroundStyle财产。将其设置为NSBackgroundStyleDark会在NSTextFieldCell中触发一条特殊路径,该路径基本上设置为attributedStringValue,更改文本颜色并通过NSShadowAttributeName添加阴影。

你可以做的是两两件事:

  • 设置你自己的backgroundStyle在自定义行或单元格视图子类。
  • 在单元格的文本字段中使用自定义NSTextFieldCell并更改行为/绘图。

我们做了后者,因为我们需要不同颜色的主题(不同颜色)表格视图。最方便的(虽然肯定不是最有效的),我们发现了这个位置是覆盖- drawInteriorWithFrame:inView:并调用超之前修改单元格的属性串,恢复原始算账:

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

在希望这可以帮助其他处于类似的情况。

+0

谢谢你,它帮助。 – Tommy

0

不知道我是否错过了您的问题中的任何内容,但是使用以下作品替换我的字体。 ReminderTableCellView只是NSTableCellView的一个子类,添加了一个额外的dateField。

- (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与源代码清单选项选择,但我不明白为什么这would'nt正常outlineView工作。

enter image description here

相关问题