2

我尝试在没有任何突出显示的情况下在 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];
}

不幸的是,没有效果。

选择时如何使行中的文本变为粗体?

4

1 回答 1

3

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

- (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]];
    }
}
于 2012-01-30T10:47:52.240 回答