Pergunta

Eu tenho um pequeno problema com o NSTABLEVIEW. Quando estou aumentando a altura de uma linha na mesa, o texto está alinhado no topo da linha, mas quero alinhá -lo verticalmente!

Alguém pode me sugerir alguma maneira de fazer isso ??

Obrigado,

Miraaj

Foi útil?

Solução

Esta é uma solução de código simples que mostra uma subclasse que você pode usar para alinhar o meio de um TextFieldCell.

o cabeçalho

#import <Cocoa/Cocoa.h>


@interface MiddleAlignedTextFieldCell : NSTextFieldCell {

}

@end

o código

@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

Esta entrada do blog mostra uma solução alternativa que também funciona bem.

Outras dicas

Aqui está a versão rápida do edifício de código na resposta acima:

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)
    }
}

Em seguida, defino a altura do TableView HeightOfrow no NSTABLEVIEW:

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

Defina a classe do NSTEXTFIELDCELL para ser verticalmente centradoTextfield:

enter image description here

e a altura do TableViewCell

enter image description here

enter image description here

Obrigado Bryan por sua ajuda.

@A resposta do iPhaaw atualizada para o Swift 4 (note que também adicionei "célula" no final do nome da classe para clareza, que também precisa corresponder ao nome da classe no 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)
    }
}

Mesmo que essa seja uma pergunta muito antiga com uma resposta aceita, aqui está uma solução alternativa:

Vá para o IB, selecione seu NSTEXTIELD SITEL em uma NSTabLECELLVIEW e adicione uma nova restrição "Center verticalmente no contêiner". Você também deve adicionar restrições horizontais (que provavelmente deveriam estar liderando/arrastando espaço definido como 0 ou o que for adequado às suas necessidades).

Usou isso em um NSOUTLINEVIEW e trabalhou como um encanto. O desempenho não foi um problema no meu caso, pois eu não tinha muitas células, mas eu esperaria que não seja pior do que os tamanhos calculistas manualmente.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top