Tengo un NSTextFieldCell que deseo mostrar con alineación vertical media. Gracias a una pregunta anterior aquí y una entrada de blog, tengo dos soluciones de trabajo.alineación vertical NSTextFieldCell, las soluciones parecen aplastar la alineación horizontal
Sin embargo, ambas soluciones parecen aplastar mi capacidad de configurar la celda como alineado a la derecha. ¿Alguien puede ayudarme a hacer que cualquiera de estas soluciones sea compatible con ambas formas de alineación?
Este es el código para una solución:
@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
La solución alternativa es (obtenido de this blog):
@implementation RSVerticallyCenteredTextFieldCell
- (NSRect)drawingRectForBounds:(NSRect)theRect
{
NSRect newRect = [super drawingRectForBounds:theRect];
if (mIsEditingOrSelecting == NO)
{
// Get our ideal size for current text
NSSize textSize = [self cellSizeForBounds:theRect];
// Center that in the proposed rect
float heightDelta = newRect.size.height - textSize.height;
if (heightDelta > 0)
{
newRect.size.height -= heightDelta;
newRect.origin.y += (heightDelta/2);
}
}
return newRect;
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
{
aRect = [self drawingRectForBounds:aRect];
mIsEditingOrSelecting = YES;
[super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
mIsEditingOrSelecting = NO;
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{
aRect = [self drawingRectForBounds:aRect];
mIsEditingOrSelecting = YES;
[super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent];
mIsEditingOrSelecting = NO;
}
@end
Eso requiere que me una en esa parte de la presentación con mi capa de datos, y sinceramente, no estoy seguro de que me guste. Esto debería respetar las opciones presentadas en IB, pero por alguna razón no (para ese asunto, la alineación vertical también debería ser una opción en IB). –
No estoy seguro de cómo se está mezclando más que establecer la cadena de NSTextFieldCell en algo de su capa de modelo.:-) La alineación (y la fuente, y el color, y el tamaño, ...) es parte de todo esto. Estoy de acuerdo, sin embargo, en que la alineación vertical debe ser una opción: presentar un informe de mejora. Como no está disponible, tendrá que contabilizarlo en el código de dibujo. –
Er, eso es "solicitud de mejora". Enlace: http://bugreporter.apple.com –