Vertically Centre Text in NSSecureTextField with s

2019-04-21 01:15发布

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?

3条回答
你好瞎i
2楼-- · 2019-04-21 01:24

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];
查看更多
SAY GOODBYE
3楼-- · 2019-04-21 01:32

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.

查看更多
男人必须洒脱
4楼-- · 2019-04-21 01:46

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

查看更多
登录 后发表回答