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)
以下是获取填充的一些选项。
早些时候,我在为设置正确的边框高度时遇到一些问题NSAttributedString。我不记得我是如何解决它们的,但是对此事有一些讨论。
使用NSTableView的小区间间距。还可以在“界面生成器”中的表视图的“大小”选项卡中使用它。寻找像元间距。
编辑界面时:
使用自定义单元格。
-[NSTextFieldCell editWithFrame:inView:editor:delegate:event:]。也有-[NSTextFieldCell setUpFieldEditorAttributes]。我发现NSTextFieldCell文本垂直居中。