问题
I'm doing some custom drawing in a NSSearchFieldCell
subclass. However overriding any of its two drawing methods causes the placeholder text not aligned.
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)
}
}
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.
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.
This was observed on macOS 10.12.6.
回答1:
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;
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
I have made it in objective-c so read it accordingly and make it useful in swift.
来源:https://stackoverflow.com/questions/45646222/overriding-nssearchfieldcell-causes-placeholder-not-centered