2010-01-20 9 views
11

Tengo un pequeño problema con NSTableView. Cuando estoy aumentando la altura de una fila en la tabla, el texto en ella está alineado en la parte superior de la fila, pero quiero alinearlo centrado verticalmente.alineando texto verticalmente en la fila NSTableView

¿Alguien puede sugerirme alguna forma de hacerlo?

Gracias,

Miraaj

Respuesta

20

Ésta es una solución de código simple que muestra una subclase puede utilizar para alinear un medio TextFieldCell.

la cabecera

#import <Cocoa/Cocoa.h> 


@interface MiddleAlignedTextFieldCell : NSTextFieldCell { 

} 

@end 

el 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 

This blog entry muestra una solución alternativa que también funciona bien.

+1

'tamaño 'supone un ancho infinito; no tiene en cuenta el ajuste de línea. Sugiero 'boundingRectForSize: options:', con el tamaño de 'cellFrame', en su lugar. –

8

Aquí está la versión Swift del edificio código en la respuesta anterior:

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

Entonces fijar la altura de la tableView heightOfRow en el NSTableView:

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

definen la clase de la NSTextFieldCell que se VerticallyCenteredTextField:

enter image description here

y la altura de la TableViewCell

enter image description here

enter image description here

Gracias Bryan por su ayuda.

0

@ respuesta iphaaw de actualizar para Swift 4 (Nota También he añadido "celda" en el final del nombre de la clase para mayor claridad, que también debe coincidir con el nombre de la clase en el 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) 
    } 
} 
Cuestiones relacionadas