alinear verticalmente el texto en la fila NSTableView
-
21-09-2019 - |
Pregunta
Tengo un pequeño problema con NSTableView. Cuando yo estoy aumentando la altura de una fila en la tabla, el texto está alineado en la parte superior de la fila, pero quiero alinearlo centra verticalmente!
¿alguien puede sugerir cualquier forma de hacerlo ??
Gracias,
Miraaj
Solución
Esta es una solución simple código que muestra una subclase que 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
espectáculos de esta entrada del blog una solución alternativa que también funciona bien.
Otros consejos
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)
}
}
Luego establece la altura de la tableView heightOfRow en el NSTableView:
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
return 30
}
Configurar la clase de la NSTextFieldCell sea VerticallyCenteredTextField:
y la altura de la TableViewCell
Gracias a Bryan por su ayuda.
@ de iphaaw actualiza para Swift 4 (nota que también agregó "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)
}
}
Incluso si se trata de una cuestión muy viejo con una respuesta aceptada, aquí es una solución alternativa:
Entre en el IB, seleccione su NSTextField sentado en una NSTableCellView y añadir una nueva restricción "Centrar verticalmente en el contenedor". También debe agregar restricciones horizontales (que probablemente deberían estar encabezando / arrastran espacio conjunto a 0 o lo que se adapte a sus necesidades).
Se utiliza esto en un NSOutlineView y trabajó como un encanto. El rendimiento no era un problema en mi caso, ya que no tengo muchas células, pero yo esperaría que no es peor que los tamaños de cálculo manualmente.