NSTableView 行中的垂直对齐文本

发布于 2024-08-18 17:09:04 字数 117 浏览 4 评论 0原文

我对 NSTableView 有一个小问题。当我增加表中一行的高度时,其中的文本在行顶部对齐,但我想将其垂直居中对齐!

谁能建议我有什么方法可以做到这一点?

谢谢,

米拉杰

I have a small problem with NSTableView. When I am increasing height of a row in table, the text in it is aligned at top of row but I want to align it vertically centered!

Can anyone suggest me any way to do it ??

Thanks,

Miraaj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∞梦里开花 2024-08-25 17:09:04

这是一个简单的代码解决方案,显示了可用于居中对齐 TextFieldCell 的子类。

标头

#import <Cocoa/Cocoa.h>


@interface MiddleAlignedTextFieldCell : NSTextFieldCell {

}

@end

代码

@implementation MiddleAlignedTextFieldCell

- (NSRect)titleRectForBounds:(NSRect)theRect {
    NSRect titleFrame = [super titleRectForBounds:theRect];
    NSSize titleSize = [[self attributedStringValue] size];
    titleFrame.origin.y = theRect.origin.y - .5 + (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];
}

@end

此博客条目展示了一种也有效的替代解决方案。

This is a simple code solution that shows a subclass you can use to middle align a TextFieldCell.

the header

#import <Cocoa/Cocoa.h>


@interface MiddleAlignedTextFieldCell : NSTextFieldCell {

}

@end

the code

@implementation MiddleAlignedTextFieldCell

- (NSRect)titleRectForBounds:(NSRect)theRect {
    NSRect titleFrame = [super titleRectForBounds:theRect];
    NSSize titleSize = [[self attributedStringValue] size];
    titleFrame.origin.y = theRect.origin.y - .5 + (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];
}

@end

This blog entry shows an alternative solution that also works well.

十二 2024-08-25 17:09:04

下面是基于上面答案构建的代码的 Swift 版本:

import Foundation
import Cocoa

class VerticallyCenteredTextField : NSTextFieldCell
{

    override func titleRectForBounds(theRect: NSRect) -> NSRect
    {
        var titleFrame = super.titleRectForBounds(theRect)
        var titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize.height) / 2.0
        return titleFrame
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
    {
        var titleRect = self.titleRectForBounds(cellFrame)

        self.attributedStringValue.drawInRect(titleRect)
    }
}

然后我在 NSTableView 中设置 tableView heightOfRow 的高度:

func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
    return 30
}

将 NSTextFieldCell 的类设置为 VerticallyCenteredTextField:

在此处输入图像描述

和 TableViewCell 的高度

在此处输入图像描述

< img src="https://i.sstatic.net/mV0Be.png" alt="在此处输入图像描述">

感谢 Bryan 的帮助。

Here is the Swift version of the code building on the answer above:

import Foundation
import Cocoa

class VerticallyCenteredTextField : NSTextFieldCell
{

    override func titleRectForBounds(theRect: NSRect) -> NSRect
    {
        var titleFrame = super.titleRectForBounds(theRect)
        var titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize.height) / 2.0
        return titleFrame
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
    {
        var titleRect = self.titleRectForBounds(cellFrame)

        self.attributedStringValue.drawInRect(titleRect)
    }
}

Then I set the height of the tableView heightOfRow in the NSTableView:

func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
    return 30
}

Set the class of the NSTextFieldCell to be VerticallyCenteredTextField:

enter image description here

and the height of the TableViewCell

enter image description here

enter image description here

Thanks Bryan for your help.

暖风昔人 2024-08-25 17:09:04

即使这是一个非常古老的问题,有一个公认的答案,这里有一个替代解决方案:

进入 IB,选择位于 NSTableCellView 中的 NSTextField 并添加一个新的“在容器中垂直居中”约束。您还应该添加水平约束(可能应该将前导/尾随空格设置为 0 或任何适合您需要的值)。

在 NSOutlineView 中使用它并且效果非常好。在我的例子中,性能不是问题,因为我没有很多单元格,但我希望它不会比手动计算大小差。

Even if this is a very old question with an accepted answer, here's an alternate solution:

Go into the IB, select your NSTextField sitting in a NSTableCellView and add a new "Center Vertically in Container" constraint. You should also add horizontal constraints (which should probably be leading/trailing space set to 0 or whatever suits your needs).

Used this in an NSOutlineView and worked like a charm. Performance wasn't an issue in my case, as I didn't have many cells, but I would expect it's not worse than manually calculating sizes.

怎言笑 2024-08-25 17:09:04

@iphaaw 的答案针对 Swift 4 进行了更新(请注意,为了清楚起见,我还在类名末尾添加了“Cell”,这也需要与 Interface Builder 中的类名相匹配):

import Foundation
import Cocoa

class VerticallyCenteredTextFieldCell : NSTextFieldCell {
    override func titleRect(forBounds theRect: NSRect) -> NSRect {
        var titleFrame = super.titleRect(forBounds: theRect)
        let titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize().height) / 2.0
        return titleFrame
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        let titleRect = self.titleRect(forBounds: cellFrame)
        self.attributedStringValue.draw(in: titleRect)
    }
}

@iphaaw's answer updated for Swift 4 (note I also added "Cell" on the end of the class name for clarity, which also needs to match the class name in Interface Builder):

import Foundation
import Cocoa

class VerticallyCenteredTextFieldCell : NSTextFieldCell {
    override func titleRect(forBounds theRect: NSRect) -> NSRect {
        var titleFrame = super.titleRect(forBounds: theRect)
        let titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize().height) / 2.0
        return titleFrame
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        let titleRect = self.titleRect(forBounds: cellFrame)
        self.attributedStringValue.draw(in: titleRect)
    }
}
路弥 2024-08-25 17:09:04

对于某些属性字符串(例如带有上标的字符串),仅绘制属性字符串可能会产生奇怪的结果。

我建议在drawInterior中调用super...

  override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
    super.drawInterior(withFrame: self.titleRect(forBounds: cellFrame), in: controlView)
}

Just drawing the attributed string can yield strange results for some attributed strings (with superscript for example).

I’d suggest calling super in drawInterior…

  override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
    super.drawInterior(withFrame: self.titleRect(forBounds: cellFrame), in: controlView)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文