Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 重写NSSearchFieldCell会导致占位符未居中_Swift_Macos_Appkit_Nssearchfield - Fatal编程技术网

Swift 重写NSSearchFieldCell会导致占位符未居中

Swift 重写NSSearchFieldCell会导致占位符未居中,swift,macos,appkit,nssearchfield,Swift,Macos,Appkit,Nssearchfield,我正在一个NSSearchFieldCell子类中进行一些自定义绘图。但是,替代其两种绘图方法中的任何一种会导致占位符文本不对齐 例如,只需使用这个覆盖NSCell的绘图方法的自定义NSSearchFieldCell子类,就会导致占位符文本左对齐 class CustomSearchFieldCell: NSSearchFieldCell { override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {

我正在一个
NSSearchFieldCell
子类中进行一些自定义绘图。但是,替代其两种绘图方法中的任何一种会导致占位符文本不对齐

例如,只需使用这个覆盖
NSCell
的绘图方法的自定义
NSSearchFieldCell
子类,就会导致占位符文本左对齐

class CustomSearchFieldCell: NSSearchFieldCell {
    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.draw(withFrame: cellFrame, in: controlView)
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.drawInterior(withFrame: cellFrame, in: controlView)
    }
}
甚至在将搜索字段的
centersPlaceholder
属性设置为
true
并重新布局搜索字段或重置搜索字段的
stringValue
之后,也会出现这种情况

注释掉这两种方法将使占位符文本(和放大镜)再次居中

然而,只要一个覆盖(即使它什么也不做,只调用其超类的实现)就会使搜索字段的占位符文本和放大镜左对齐

问题是,如何让
居中对齐占位符
工作,并且仍然拥有自定义图形

请注意,我需要在单元格内进行一些自定义绘图和鼠标处理,因此需要覆盖


这是在macOS 10.12.6上观察到的。

您应该研究此方法,因为当鼠标离开文本字段并相应更新帧时,会调用此方法:-

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag;
我要求将NSSearchFieldCell的文本垂直居中,下面的代码实现了这一点,因此您可以尝试将所需的组件居中-:

//
//  VerticallyCenteredTextFieldCell.m
//
//  Created by Vikram on 01/03/17.
//  Copyright © 2017 Vikram. All rights reserved.
//

#import "VerticallyCenteredTextFieldCell.h"

@implementation VerticallyCenteredTextFieldCell

- (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-3);
}
- (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];
}

@end

我已经在objective-c中创建了它,因此请相应地阅读它,并使其在swift中有用。

当我查看我的代码时,我有相同的要求,但我没有在我的应用程序中使用任何方法来实现这一点,我也没有使用draw方法。这是从哪里来的?