Ken*_*and 2 macos xcode cocoa nstableview xcode4
我有一个 NSTableView,它在我的窗口上从边缘延伸到边缘,但是表格边缘单元格中的数据确实需要一些填充。如果我在边缘留下排水沟,窗口看起来不太好,所以我想尝试在一些单元格内添加一些填充,这样数据就不会紧贴边缘。
我在 Interface Builder 或关于向单元格添加填充或插入的代码文档中找不到任何内容。
有什么建议?
您可以继承 NSTextFieldCell 并覆盖 drawInteriorWithFrame:inView: 方法来自定义绘制字符串。
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect titleRect = [self titleRectForBounds:cellFrame];
NSAttributedString *aTitle = [self attributedStringValue];
if ([aTitle length] > 0) {
[aTitle drawInRect:titleRect];
}
}
Run Code Online (Sandbox Code Playgroud)
其中titleRectForBounds:添加一些空间
- (NSRect)titleRectForBounds:(NSRect)bounds
{
NSRect titleRect = bounds;
titleRect.origin.x += 5;
titleRect.origin.y += 5;
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)
在http://comelearncocoawithme.blogspot.com/2011/09/custom-cells-in-nstableview-part-1.html 上有一个更完整的例子
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |