通过子类化在 NSSecureTextField 中垂直居中文本

发布于 2025-01-08 07:11:01 字数 1884 浏览 1 评论 0 原文

我试图将 NSTextField 中的文本垂直居中,但其中一个用于密码,因此它是 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,只是不是其安全版本。苹果似乎以某种方式保护了这些方法不被覆盖。

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

misaligned

任何人都可以建议一种获取 的方法吗? NSSecureTextFieldCell 子类的方法被调用或其他方式垂直居中文本字段?

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];
}

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:

misaligned "password" field

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2025-01-15 07:11:01

尝试使用常规 NSTextField,其中包含 NSSecureTextFieldCell 子类。我遇到了同样的问题,这个组合有效。

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

朮生 2025-01-15 07:11:01

您可以创建 NSSecureTextField 的子类,例如:

@implementation SubclassTextField

+ (Class)cellClass {
    return [SubclassTextFieldCell class];
}

@end

和单元格的子类:

@implementation SubclassTextFieldCell

- (NSRect)drawingRectForBounds:(NSRect)rect {
    // It will be working
    ...
}

...
@end

如果您这样做,则子类 NSSecureTextField 中的方法将开始工作。

You can create subclass of the NSSecureTextField like:

@implementation SubclassTextField

+ (Class)cellClass {
    return [SubclassTextFieldCell class];
}

@end

and the subclass of the cell:

@implementation SubclassTextFieldCell

- (NSRect)drawingRectForBounds:(NSRect)rect {
    // It will be working
    ...
}

...
@end

If you do so then methods from subclassed NSSecureTextField will start working.

绿光 2025-01-15 07:11:01

要使占位符文本在 NSTextFieldNSSecureTextField 中垂直居中,您所需要做的就是选中属性检查器(文本字段部分)中的“使用单行模式”复选框。如果您不使用 IB,您可以通过编程方式进行设置,如下所示:

[mySecureTextField.cell setUsesSingleLineMode:YES];

All you have to do to vertically center the placeholder text in either an NSTextField or NSSecureTextField is check the "Uses Single Line Mode" checkbox in the Attributes inspector (Text Field section). If you're not using IB, you can set it programmatically like so:

[mySecureTextField.cell setUsesSingleLineMode:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文