Cocoa NSTableView:如何正确调整单元格中文本的大小

Cocoa NSTableView:如何正确调整单元格中文本的大小,cocoa,nstableview,nstablecellview,Cocoa,Nstableview,Nstablecellview,我需要在NSTableView(基于视图的模式)中增加字体大小,以便在我的应用程序的某种演示文稿中实现更好的可读性 在-(NSView*)tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn行:(NSInteger)行中更改字体效果良好: NSTableCellView *cellView = [tableView makeViewWithIdentifier:identifier

我需要在NSTableView(基于视图的模式)中增加字体大小,以便在我的应用程序的某种演示文稿中实现更好的可读性

-(NSView*)tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn行:(NSInteger)行中更改字体效果良好

    NSTableCellView *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
    [cellView.textField setFont:self.tableFont];
    cellView.textField.stringValue = rowData[row];
    [cellView.textField setNeedsDisplay:YES];
我还使用
-(CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row更新了行高,这同样适用于:

- (CGFloat) tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
    if (self.tableFont) {
        NSLog(@"Setting row height to %.1f", [self.tableFont pointSize]+8);
        return [self.tableFont pointSize] + 8;
    }
    return [NSFont systemFontSize] + 8;
}
(可能有更优雅的方法来确定正确的高度)

现在,我的问题是: 我确实看到字体被改变了。我还看到行高发生了变化(当我使用交替行着色时,可以很好地看到)。但是单元格中的文本似乎被剪裁到了原来的高度(可能是17像素),因此,如果字体尺寸较大,我只能看到文字的顶部,然后是一些空白,然后是下一行的上半部分

对我来说,它看起来像是一些视图将NSTableViewCell剪辑到原来的高度。。。我已经将帧和边界设置为不同的值,但这并没有改变任何东西:

    NSRect rect = [cellView.textField frame];
    rect.origin.y = 1;
    rect.size.height = [self.tableFont pointSize] + 6;
    [[cellView.textField cell] setFrame:rect];
    [cellView.textField setFrame:rect];
    [cellView.textField setBounds:rect];
    [cellView.textField setNeedsDisplay:YES];
    [cellView setFrame:rect];
    [cellView setBounds:rect];
    [cellView setNeedsDisplay:YES];
我在这里有点迷路了。。。我想我错过了一些简单的东西,但我不知道该去哪里找

谢谢,
Jan

您必须确保设置了约束,以便在点击自动布局过程时,NSTextField可以自动增长。NSTextField的高度不应受到任何约束,因为它将具有基于fontSize的固有高度

此外,在基于视图的模式下,cellView的“textField”和“imageView”插座也有一个不合适的功能:在NStable中,cellView将直接在其textField上设置框架,而在-viewWillDraw中设置imageView,忽略水平约束并打破其自身的布局规则。(该文件归档为雷达11713245和15359487)

下面是我在10.8和10.9的自定义NSTableCellView类中用来解决这一问题的代码。注意:在我的例子中,我只需要重置字段的x偏移:

#pragma mark NSView

- (void)viewWillDraw;
{
    const NSRect imageViewFrame = self.imageView.frame, textFieldFrame = self.textField.frame;

    [super viewWillDraw]; // NSTableCellView in  source list-mode outline view will apply custom metrics and positioning here by calling -_doStandardRowSizeStyleLayout, which manually calls setFrame: on the textField and imageView, screwing up our autolayout...

    // ...so put the x offsets back the way we really wanted them, dammit (Radar 11713245, 15359487)
    if (imageViewFrame.origin.x != self.imageView.frame.origin.x)
        [self.imageView setFrameOrigin:(NSPoint){imageViewFrame.origin.x, self.imageView.frame.origin.y}];

    if (textFieldFrame.origin.x != self.textField.frame.origin.x)
        [self.textField setFrameOrigin:(NSPoint){textFieldFrame.origin.x, self.textField.frame.origin.y}];
    if (textFieldFrame.size.width != self.textField.frame.size.width)
        [self.textField setFrameSize:(NSSize){textFieldFrame.size.width, self.textField.frame.size.height}];
}

谢谢你的回答,威尔!我检查了所有的约束,在Interface Builder中没有看到任何会弄乱NSTextField的约束。创建了两个约束(约束:),但同时删除它们并没有显示任何效果。textfield框架大小似乎固定为我在Interface Builder中为NSTableCellView.textfield子类元素定义的大小,即使在viewWillDraw中重写它也是不可能的。作为一种解决方法,我现在为NSTableCellView.textField框架使用了一个巨大的设置。。。现在似乎可以了。所以我想我真正的问题是:为什么在程序执行期间,帧大小固定在我在IB中输入的帧大小上?表格视图的行高设置为“自动”、“不固定”。