Vertically Centre Text in NSSecureTextField with subclassing

对着背影说爱祢 提交于 2019-12-05 01:07:54

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

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.

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!