Я пытаюсь расположить текст по центру в моих полях NSTextFields, но одно из них предназначено для пароля, поэтому это NSSecureTextField.Я установил, что его класс MDVerticallyCenteredSecureTextFieldCell
со следующей реализацией:
- (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];
}
Подобный подкласс уже работает для обычных NSTextFieldCell
s, только небезопасные версии этого,Похоже, что Apple каким-то образом защитила эти методы от переопределения.
Теперь поле пароля является единственным смещенным:
Может кто-нибудь предложить способ получитьметоды подкласса NSSecureTextFieldCell
для вызова или другой способ вертикального центрирования текстового поля?