15

NSTableView に小さな問題があります。テーブルの行の高さを増やしていると、その中のテキストが行の上部に配置されますが、垂直方向の中央に配置したいです!

誰かが私にそれを行う方法を提案できますか??

ありがとう、

ミラージ

4

5 に答える 5

21

これは、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

このブログ エントリは、うまく機能する代替ソリューションを示しています。

于 2010-01-20T16:46:15.950 に答える
8

上記の答えに基づいて構築されたコードの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 の高さ

ここに画像の説明を入力

ここに画像の説明を入力

ブライアン、助けてくれてありがとう。

于 2015-06-28T09:12:10.057 に答える