NSTableView に小さな問題があります。テーブルの行の高さを増やしていると、その中のテキストが行の上部に配置されますが、垂直方向の中央に配置したいです!
誰かが私にそれを行う方法を提案できますか??
ありがとう、
ミラージ
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
このブログ エントリは、うまく機能する代替ソリューションを示しています。
上記の答えに基づいて構築されたコードの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 の高さ
ブライアン、助けてくれてありがとう。