Sto provando a centrare verticalmente il testo nel mio NSTextFields, ma uno di questi è per una password, quindi è un NSSecureTextField. Ho impostato sua categoria ad essere MDVerticallyCenteredSecureTextFieldCell
con la seguente implementazione:Verticalmente Centra il testo in NSSecureTextField con sottoclasse
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
// super would normally draw text at the top of the cell
NSInteger offset = floor((NSHeight(frame) -
([[self font] ascender] - [[self font] descender]))/2);
return NSInsetRect(frame, 0.0, offset+10);
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
[super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
inView:controlView editor:editor delegate:delegate event:event];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate
start:(NSInteger)start length:(NSInteger)length {
[super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
inView:controlView editor:editor delegate:delegate
start:start length:length];
}
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
[super drawInteriorWithFrame:
[self adjustedFrameToVerticallyCenterText:frame] inView:view];
}
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[super drawWithFrame:cellFrame inView:controlView];
}
Una sottoclasse simile è già al lavoro per regolari NSTextFieldCell
s, solo che non garantire le versioni di questo. Sembra che Apple abbia in qualche modo protetto questi metodi dall'essere sovrascritti.
Ora il campo password è l'unico disallineato:
Qualcuno può suggerire un modo ottenere metodi della NSSecureTextFieldCell
della sottoclasse di essere chiamati o un altro modo per centrare verticalmente il campo di testo?
Grazie, ha funzionato. – jrc
Questo ha funzionato anche per me. Grazie! – Tommy