NSTableCellView填充

Wes*_*ley 6 xcode cocoa objective-c nstableview nstablecellview

如何添加填充NSTableCellView

我希望内容的填充量为10px,类似于纯HTML中的内容.(所有面).文本应在中间垂直居中.

目前我正在NSTextFieldCell的子类中执行此操作.但这似乎不正常.

编辑文本时,编辑文本字段不使用textfieldcell中的填充.

图2: 检查图像

检查图像

这是我目前的代码,(子类NSTextFieldCell)

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];

NSAttributedString *aTitle = [self attributedStringValue];
if ([aTitle length] > 0) {
    [aTitle drawInRect:titleRect];
}
}

- (NSRect)titleRectForBounds:(NSRect)bounds
{
NSRect titleRect = bounds;

titleRect.origin.x += 10;
titleRect.origin.y = 2;

NSAttributedString *title = [self attributedStringValue];
if (title) {
    titleRect.size = [title size];
} else {
    titleRect.size = NSZeroSize;
}

// We don't want the width of the string going outside the cell's bounds
CGFloat maxX = NSMaxX(bounds);
CGFloat maxWidth = maxX - NSMinX(titleRect);
if (maxWidth < 0) {
    maxWidth = 0;
}

titleRect.size.width = MIN(NSWidth(titleRect), maxWidth);

return titleRect; 
}
Run Code Online (Sandbox Code Playgroud)

tsn*_*rri 5

以下是获取填充的一些选项。

早些时候,我在为设置正确的边框高度时遇到一些问题NSAttributedString。我不记得我是如何解决它们的,但是对此事有一些讨论

想法1:

使用NSTableView的小区间间距。还可以在“界面生成器”中的表视图的“大小”选项卡中使用它。寻找像元间距。

想法2:

编辑界面时:

  • 除非您需要其他功能,否则请使用Apple提供的文本或图像以及文本表单元格视图。
  • 使用大小选项卡在表格视图内更改表格单元格视图的高度。
  • 将文本字段重新放置在表格单元格视图内。

想法3:

使用自定义单元格。

  • 您可以通过覆盖更改字段编辑器的位置-[NSTextFieldCell editWithFrame:inView:editor:delegate:event:]。也有-[NSTextFieldCell setUpFieldEditorAttributes]。我发现此示例代码很有用。
  • 如果增加单元格的高度,有两种方法可以使NSTextFieldCell文本垂直居中。