【问题标题】:NSTableCellView paddingNSTableCellView 内边距
【发布时间】:2015-01-23 22:27:26
【问题描述】:

如何向NSTableCellView 添加填充?

我希望内容具有 10 像素的填充,类似于您在纯 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; 
}

【问题讨论】:

    标签: objective-c xcode cocoa nstableview nstablecellview


    【解决方案1】:

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

    之前我在获取NSAttributedString 的正确边界框高度时遇到了一些问题。我不记得我是如何解决它们的,但有some discussions 讨论此事。

    想法 #1:

    使用NSTableView's intercell spacing。它也可以在 Interface Builder 中的 table view 的 size 选项卡中使用。寻找单元格间距。

    想法 #2:

    编辑界面时:

    • 除非您需要其他内容,否则请使用 Apple 提供的文本或图像和文本表格单元格视图。
    • 使用尺寸选项卡更改表格视图内表格单元格视图的高度。
    • 在表格单元格视图中重新定位文本字段。

    想法#3:

    使用自定义单元格。

    • 您可以通过覆盖-[NSTextFieldCell editWithFrame:inView:editor:delegate:event:] 来更改字段编辑器的位置。还有-[NSTextFieldCell setUpFieldEditorAttributes]。我发现 this sample code 很有用。
    • 如果增加单元格的高度,有a couple of ways可以让NSTextFieldCell垂直居中绘制文字。

    【讨论】:

    • 在我的 NSTextFieldCell 子类中既没有调用 editWithFrame:.. 也没有调用 setupFieldEditorAttributes。你碰巧知道为什么吗? (基于视图的 NSTable)
    • 我用TableViewPlayGround 尝试了一些东西并相应地编辑了我的答案。
    【解决方案2】:

    使用 setContentInset 方法。它设置内容视图和封闭表格视图之间的插入距离。

    例如

    self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, -20, 0);
    

    【讨论】:

    • 似乎只有IOS。
    • 这仅适用于 iOS!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多