我试图在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]; }
类似的子类已经适用于常规的NSTextFieldCells,只是不是安全的版本.苹果似乎以某种方式保护这些方法不被覆盖.
现在密码字段是唯一的一个不对齐:
任何人都可以提出一种方式,让NSSecureTextFieldCell子类的方法被调用,或者另一种方式来垂直居中的文本字段?
解决方法
尝试使用常规NSTextField,其中包含您的NSSecureTextFieldCell子类.我有同样的问题,这个组合有效.