cocoa - 在 NSTableView 中将所选行设为粗体

标签 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];
}

不幸的是,没有效果。

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

最佳答案

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

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

关于cocoa - 在 NSTableView 中将所选行设为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061958/

相关文章:

objective-c - 将 NSView 实例保存在数组中

Mysql多次选择COL以获得不同的值

javascript - 如何使用 javascript 或 jquery 删除选择上的名称并在文本区域上添加名称?

javascript - 如何为 Material ui 选择字段设置值?

cocoa - 反转选定 TableView 行中的按钮图像

Swift 3 从字典中填充 TableView?

Objective-C:在 -(void)drawRect 中使用 "if else"语句来获取复选框状态?

objective-c - 解释崩溃日志 Objective C

ios - 有没有办法在没有按钮的情况下在 iOS 中创建类似 UIButton 的框架?

objective-c - Objective-C - 将 char[] 转换为十六进制格式的 NSString