我有一个NSOutlineView,其中包含父项和子项列表。

将父项(带有空心箭头的项)制成粗体字的最简单方法是什么?

谢谢

最佳答案

如果使用的是基于单元格的大纲视图,则可以使用此委托方法来加粗父项:

// Assuming you use NSTextFieldCell
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if ([self outlineView:outlineView numberOfChildrenOfItem:item] > 0) {
        [(NSCell *)cell setFont: [NSFont boldSystemFontOfSize: ((NSCell *)cell).font.pointSize]];
    } else {
        [(NSCell *)cell setFont: [NSFont systemFontOfSize: ((NSCell *)cell).font.pointSize]];
    }
}


否则,在基于视图的大纲视图中,您可以执行以下操作:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    NSTableCellView *cellView = [outlineView makeViewWithIdentifier:@"MyIdentifier" owner:self];
    if ([self outlineView:outlineView numberOfChildrenOfItem:item] > 0) {
        [cellView.textField setFont: [NSFont boldSystemFontOfSize: cellView.textField.font.pointSize]];
    } else {
        [cellView.textField setFont: [NSFont systemFontOfSize: cellView.textField.font.pointSize]];
    }

    // do whatever else you do
    return cellView;
}


另外,NSOutlineViewDelegate-outlineView:isGroupItem:可能实际上是您要查找的内容:https://developer.apple.com/library/mac/documentation/cocoa/reference/NSOutlineViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSOutlineViewDelegate/outlineView:isGroupItem

10-07 21:31