gpt4 book ai didi

cocoa - NSTableView 行中的垂直对齐文本

转载 作者:行者123 更新时间:2023-12-03 16:02:40 27 4
gpt4 key购买 nike

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

任何人都可以建议我任何方法吗?

谢谢

米拉杰

最佳答案

这是一个简单的代码解决方案,显示了可用于居中对齐 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 blog entry展示了另一种效果也很好的替代解决方案。

关于cocoa - NSTableView 行中的垂直对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2103125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com