objective-c – 使用子类化在NSSecureTextField中垂直居中文本
发布时间:2020-12-16 03:06:59 所属栏目:百科 来源:网络整理
导读:我试图在NSTextFields中垂直居中的文本,但其中一个是密码,所以它是一个NSSecureTextField.我把它设置为 MDVerticallyCenteredSecureTextFieldCell ,具有以下实现: - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame { // super would normally
我试图在NSTextFields中垂直居中的文本,但其中一个是密码,所以它是一个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]; } 类似的子类已经适用于常规的NSTextFieldCells,只是不是安全的版本.苹果似乎以某种方式保护这些方法不被覆盖. 现在密码字段是唯一的一个不对齐: 任何人都可以提出一种方式,让NSSecureTextFieldCell子类的方法被调用,或者另一种方式来垂直居中的文本字段? 解决方法
尝试使用常规NSTextField,其中包含您的NSSecureTextFieldCell子类.我有同样的问题,这个组合有效.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |