有没有一种“正确”的方法让 NSTextFieldCell 绘制垂直居中的文本?
-
22-07-2019 - |
题
我有一个 NSTableView
有几个文本列。默认情况下, dataCell
这些列是 Apple 的一个实例 NSTextFieldCell
类,它可以完成各种奇妙的事情,但它绘制与单元格顶部对齐的文本,并且我希望文本在单元格中垂直居中。
有一个内部标志 NSTextFieldCell
可以用来垂直居中文本,而且效果很好。然而,由于它是一个内部标志,因此它的使用不受苹果公司的批准,并且它可能会在未来的版本中毫无警告地消失。我目前正在使用这个内部标志,因为它简单而有效。苹果显然花了一些时间来实现该功能,所以我不喜欢重新实现它的想法。
所以;我的问题是这样的: 实现与 Apple 的 NStextFieldCell 完全相同但绘制垂直居中文本而不是顶部对齐的内容的正确方法是什么?
作为记录,这是我当前的“解决方案”:
@interface NSTextFieldCell (MyCategories)
- (void)setVerticalCentering:(BOOL)centerVertical;
@end
@implementation NSTextFieldCell (MyCategories)
- (void)setVerticalCentering:(BOOL)centerVertical
{
@try { _cFlags.vCentered = centerVertical ? 1 : 0; }
@catch(...) { NSLog(@"*** unable to set vertical centering"); }
}
@end
使用方法如下:
[[myTableColumn dataCell] setVerticalCentering:YES];
解决方案
其他的答案没有为多行工作。因此,我开始继续使用无证cFlags.vCentered
财产,但引起我的应用程序从App Store拒绝。最后我用的马特·贝尔的解决方案修改后的版本,对于多线,自动换工作,和截短的最后一行:
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSAttributedString *attrString = self.attributedStringValue;
/* if your values can be attributed strings, make them white when selected */
if (self.isHighlighted && self.backgroundStyle==NSBackgroundStyleDark) {
NSMutableAttributedString *whiteString = attrString.mutableCopy;
[whiteString addAttribute: NSForegroundColorAttributeName
value: [NSColor whiteColor]
range: NSMakeRange(0, whiteString.length) ];
attrString = whiteString;
}
[attrString drawWithRect: [self titleRectForBounds:cellFrame]
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin];
}
- (NSRect)titleRectForBounds:(NSRect)theRect {
/* get the standard text content rectangle */
NSRect titleFrame = [super titleRectForBounds:theRect];
/* find out how big the rendered text will be */
NSAttributedString *attrString = self.attributedStringValue;
NSRect textRect = [attrString boundingRectWithSize: titleFrame.size
options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin ];
/* If the height of the rendered text is less then the available height,
* we modify the titleRect to center the text vertically */
if (textRect.size.height < titleFrame.size.height) {
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - textRect.size.height) / 2.0;
titleFrame.size.height = textRect.size.height;
}
return titleFrame;
}
(此代码假定ARC;添加一个自动释放attrString.mutableCopy后如果使用手工存储器管理)
其他提示
重写的NSCell的-titleRectForBounds:
应该这样做 - 那就是负责告诉细胞在哪里画它的文本的方法是:
- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributedStringValue] size];
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - titleSize.height) / 2.0;
return titleFrame;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
[[self attributedStringValue] drawInRect:titleRect];
}
FYI,这个效果很好,虽然我没有设法得到它留下来中心当您编辑单元格......我有时有大量文本的细胞并可能导致他们不重合,如果文本此代码高度大于它的尝试垂直居中它在细胞中这是我的改性方法:
- (NSRect)titleRectForBounds:(NSRect)theRect
{
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributedStringValue] size];
// test to see if the text height is bigger then the cell, if it is,
// don't try to center it or it will be pushed up out of the cell!
if ( titleSize.height < theRect.size.height ) {
titleFrame.origin.y = theRect.origin.y + (theRect.size.height - titleSize.height) / 2.0;
}
return titleFrame;
}
对于任何试图利用这个球马特的drawInteriorWithFrame:inView:
方法,这将不再绘制背景,如果你设置你的画之一。为了解决这个添加沿着线的东西
[[NSColor lightGrayColor] set];
NSRectFill(cellFrame);
到您drawInteriorWithFrame:inView:
方法的开始。
虽然这是一个很老的问题......
我相信 NSTableView 实现的默认样式严格用于具有相同大小和字体的单行文本显示。
在这种情况下,我建议,
- 设置字体。
- 调整
rowHeight
.
也许你会得到安静密集的行。然后,通过设置给它们填充 intercellSpacing
.
例如,
core_table_view.rowHeight = [NSFont systemFontSizeForControlSize:(NSSmallControlSize)] + 4;
core_table_view.intercellSpacing = CGSizeMake(10, 80);
这是通过两个属性调整您将得到的结果。
这不适用于多行文本,但如果您不需要多行支持,则对于快速垂直居中来说已经足够好了。
我有同样的问题,这里是我做的解决方案:
1)在界面生成器,选择您NSTableCellView。确保它一样大的尺寸检查行高度。例如,如果你的行高度为32,使你的细胞高度32
2)确保您的电池处于有利位置你行(我的意思是可见的)
3)中选择的细胞内的TextField和去你尺寸检查员
4)你应该看到“安排”项,然后选择“垂直居中集装箱”
- >的文本字段将中心本身在细胞
没有。正确的方法是把字段在另一视图中,并使用自动布局或父视图的布局将它定位。