Tôi đang cố gắng căn giữa văn bản trong NSTextFields của mình, nhưng một trong số chúng là một mật khẩu, vì vậy đó là một NSSecureTextField. Tôi đã thiết lập lớp của nó là MDVerticallyCenteredSecureTextFieldCell
với việc thực hiện sau đây:Văn bản trung tâm theo chiều dọc trong NSSecureTextField với phân lớp
- (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];
}
Một lớp con tương tự đã được làm việc cho thường xuyên NSTextFieldCell
s, chỉ cần không an toàn phiên bản này. Có vẻ như Apple đã bằng cách nào đó bảo vệ các phương pháp này khỏi bị ghi đè.
Bây giờ trường mật khẩu là người duy nhất lệch:
bất cứ ai có thể đề xuất một cách có phương pháp các NSSecureTextFieldCell
lớp con để được gọi hoặc một cách khác để theo chiều dọc tập trung các lĩnh vực văn bản?
Cảm ơn, điều này đã hiệu quả. – jrc
Điều này cũng phù hợp với tôi. Cảm ơn! – Tommy