在NSTableView中将选定的行加粗

时间:2012-01-30 09:47:25

标签: cocoa select nstableview bold

我尝试在NSTableView中制作粗体行选择样式而不进行任何突出显示

我切换了突出显示:

[myTable setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

但是我在选择的行中使文本变为粗体有些麻烦。正如我所建议的,我需要更改NSTableView源代码的属性:

- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
    NSDictionary *boldFont = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:13.0]
                                                         forKey:NSFontAttributeName];
    NSDictionary *normalFont = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:13.0]
                                                         forKey:NSFontAttributeName];

    for(MyClass *obj in tableSourceList)
        obj.name = [[NSMutableAttributedString alloc]
                           initWithString:obj.name
                               attributes: normalFont];

    long row = [myTable selectedRow];
    MyClass objectAtSelectedRow = [tableSourceList objectAtIndex: row];
    objectAtSelectedRow.name = [[NSMutableAttributedString alloc]
                            initWithString:dr.dreamname
                                attributes: boldFont]; 
    [tableSourceList replaceObjectAtIndex:row withObject:objectAtSelectedRow];
}

不幸的是,没有效果。

如何在选中时以粗体显示文字?

1 个答案:

答案 0 :(得分:2)

您可以尝试通过更改要在表的委托中呈现的表格单元来实现:

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    // Maybe a test on the table column is recommanded if some cells should not be modified
    NSIndexSet *selection = [aTableView selectedRowIndexes];
    if ([selection containsIndex:rowIndex]) {
        [aCell setFont:[NSFont boldSystemFontOfSize:12]];
    } else {
        [aCell setFont:[NSFont systemFontOfSize:12]];
    }
}