Ho un NSTextFieldCell che desidero visualizzare con allineamento centrale verticale. Grazie ad una vecchia domanda qui e un post sul blog ho due soluzioni funzionanti.Allineamento verticale NSTextFieldCell, le soluzioni sembrano schiacciare l'allineamento orizzontale
Tuttavia, entrambe le soluzioni sembrano schiacciare la mia capacità di impostare la cella come allineata a destra. Qualcuno può aiutarmi a fare in modo che una di queste soluzioni supporti entrambe le forme di allineamento?
Ecco il codice per una soluzione:
@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 soluzione alternativa è (ottenuto da 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
Questo mi richiede di accoppiare quella parte della presentazione con il mio livello dati, e onestamente non sono sicuro che mi piaccia. Questo dovrebbe rispettare le opzioni presentate in IB, ma per alcuni motivi non lo è (per quanto riguarda l'allineamento verticale dovrebbe essere un'opzione in IB). –
Non sono sicuro di come lo si mischia più che impostare la stringa di NSTextFieldCell su qualcosa dal livello del modello.:-) L'allineamento (e il carattere, il colore e la dimensione, ...) fa parte di tutto questo. Sono d'accordo, tuttavia, che l'allineamento verticale dovrebbe essere un'opzione: file un rapporto di miglioramento. Dal momento che non è disponibile, dovrai tenerne conto nel codice del disegno. –
Er, questa è "richiesta di miglioramento". Link: http://bugreporter.apple.com –