覆盖NSSearchFieldCell导致占位符不居中 [英] Overriding NSSearchFieldCell causes placeholder not centered

查看:188
本文介绍了覆盖NSSearchFieldCell导致占位符不居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在NSSearchFieldCell子类中进行一些自定义绘图.但是,覆盖其两种绘制方法中的任何一种都会导致占位符文本未对齐.

I'm doing some custom drawing in a NSSearchFieldCell subclass. However overriding any of its two drawing methods causes the placeholder text not aligned.

例如,仅通过使用覆盖NSCell的绘制方法的此自定义NSSearchFieldCell子类,将导致占位符文本左对齐.

For example, just by using this custom NSSearchFieldCell subclass that overrides NSCell's drawing method would cause the placeholder text to be left-aligned.

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之后.

This is even after setting the search field's centersPlaceholder property to true and both re-layout the search field or reset the search field's stringValue.

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

Commenting out those two methods would make the placeholder text (and magnifying glass back centered again.

但是,只有一个覆盖(即使它什么也不做,仅调用其超类的实现),会使搜索字段的占位符文本和放大镜变为左对齐.

However, just one override (even though it does nothing and only calls its superclass' implementation) would make the search field's placeholder text and magnifying glass become left-aligned.

问题是,如何使center align placeholder工作并仍具有自定义图形?

The question is, how to get thecenter align placeholder work and still have the custom drawing?

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

Note that I need to do some custom drawing and mouse handling within the cell, hence the overrides are required.

这是在macOS 10.12.6上观察到的.

This was observed on macOS 10.12.6.

推荐答案

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

You should look into this method because it gets called when mouse has left the text field and update the frame accordingly :-

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag;

我需要垂直居中放置NSSearchFieldCell的文本,下面的代码起到了窍门,因此您可以尝试使其居中放置所需的组件-:

I had a requirement to vertically centre the text of NSSearchFieldCell and below code did the trick so you can give it a try to centre the components you want - :

//
//  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中做到了,因此请仔细阅读并迅速使用.

I have made it in objective-c so read it accordingly and make it useful in swift.

这篇关于覆盖NSSearchFieldCell导致占位符不居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆