使用子类在NSSecureTextField中垂直居中放置文本 [英] Vertically Centre Text in NSSecureTextField with subclassing

查看:132
本文介绍了使用子类在NSSecureTextField中垂直居中放置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在NSTextFields中垂直居中放置文本,但是其中之一是用于输入密码的,因此它是NSSecureTextField.我将其类设置为 MDVerticallyCenteredSecureTextFieldCell 具有以下实现:

I'm trying to vertically centre text in my NSTextFields, but one of them is for a password, so it's a NSSecureTextField. I've set it's class to be MDVerticallyCenteredSecureTextFieldCell with the following implementation:

- (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了,只是它的不安全版本.看来苹果已经以某种方式保护了这些方法,使其不被覆盖.

A similar subclass is already working for regular NSTextFieldCells, just not secure versions of this. It seems like Apple has somehow protected these methods from being overridden.

现在,密码字段是唯一未对齐的字段:

Now the password field is the only one misaligned:

有人可以建议一种方法来调用NSSecureTextFieldCell子类的方法,还是另一种方法来垂直居中文本字段?

Can anyone suggest a way get the NSSecureTextFieldCell subclass's methods to be called or another way to vertically centre the text field?

推荐答案

尝试使用常规NSTextField,并将NSSecureTextFieldCell子类放入其中.我遇到了同样的问题,这种组合奏效了.

Try using a regular NSTextField, with your NSSecureTextFieldCell subclass inside. I had the same problem and this combination worked.

这篇关于使用子类在NSSecureTextField中垂直居中放置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆