设置centersPlaceholder = false
后,占位符在左侧正确对齐,但搜索按钮不显示其图像。输入某些文本时的取消按钮也是如此。这是正确的行为还是我错过了什么?
这个相关的问题揭示了这种奇怪的行为如何被用来(滥用(在现场强制左对齐。只需对 NSSearchFieldCell 进行子类并覆盖draw
方法:
import Cocoa
class FilterFieldCell: NSSearchFieldCell {
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
super.draw(withFrame: cellFrame, in: controlView)
}
}
然后将搜索字段单元格设置为此类。
#define kSearchButtonWidth 22.f
#import "AMSearchField.h"
@implementation AMSearchField
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (NSRect)rectForSearchButtonWhenCentered:(BOOL)isCentered
{
CGRect rect = [super rectForSearchButtonWhenCentered:isCentered];
if (rect.size.width < kSearchButtonWidth) {
rect.size.width = kSearchButtonWidth;
}
return rect;
}
- (NSRect)rectForSearchTextWhenCentered:(BOOL)isCentered
{
CGRect rect = [super rectForSearchTextWhenCentered:isCentered];
if (rect.origin.x < kSearchButtonWidth) {
CGFloat delta = kSearchButtonWidth - rect.origin.x;
rect.origin.x = kSearchButtonWidth;
rect.size.width = rect.size.width - delta;
}
return rect;
}
@end
在 XIB 文件中,将"搜索类"设置为"AMSearchField