Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codeswimmer/3363074 to your computer and use it in GitHub Desktop.
Save codeswimmer/3363074 to your computer and use it in GitHub Desktop.
CRTransparentSearchFieldCell -- transparent search field cell
#import <Cocoa/Cocoa.h>
@interface CRTransparentSearchFieldCell : NSSearchFieldCell {
BOOL _hasText;
}
@end
#import "CRTransparentSearchFieldCell.h"
@implementation CRTransparentSearchFieldCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect rect = NSInsetRect(cellFrame, 0.5, 0.5);
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect
xRadius:rect.size.height/2.0
yRadius:rect.size.height/2.0];
[[NSColor colorWithDeviceWhite:0 alpha:0.4] set];
[path stroke];
NSRect searchButtonCellFrame = cellFrame;
searchButtonCellFrame.size = [[self searchButtonCell] cellSize];
[[self searchButtonCell] drawWithFrame:searchButtonCellFrame
inView:controlView];
if (_hasText) {
NSRect cancelButtonCellFrame = cellFrame;
cancelButtonCellFrame.origin.x = cellFrame.origin.x +
cellFrame.size.width - [[self cancelButtonCell] cellSize].width;
cancelButtonCellFrame.size = [[self cancelButtonCell] cellSize];
[[self cancelButtonCell] drawWithFrame:cancelButtonCellFrame
inView:controlView];
}
}
- (void)setStringValue:(NSString *)aString {
// We need _hasText, instead of querying [self stringValue] when drawing,
// because querying it will redraw cell, which freezes text cursor.
_hasText = ![aString isEqualToString:@""];
[super setStringValue:aString];
}
@end
@dchest
Copy link

dchest commented Aug 15, 2012

This doesn't work as intended, see newer version :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment