我尝试在 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/