objective-c – 使用子类化在NSSecureTextField中垂直居中文本

我试图在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子类.我有同样的问题,这个组合有效.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...