// // KMComboBox.m // SignFlow // // Created by wanjun on 2021/1/15. // #import "KMOCRComboBox.h" @implementation KMOCRComboBox #pragma mark - Draw - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; switch (self.type) { case KMOCRComboBoxType_None: { NSPoint p = _comboxRect.origin; p.x += _comboxRect.size.width - 20; p.y += (_comboxRect.size.height-16)/2; [[NSColor whiteColor] setFill]; NSRectFill(NSMakeRect(p.x, 0, 16.0, _comboxRect.size.height)); NSImage *image = [NSImage imageNamed:@"KMImageNameUXIconBtnArrowDown"]; NSRect rect =NSZeroRect; rect.size = CGSizeMake(16.0, 16.0); [image drawInRect:NSMakeRect(p.x, p.y, 16.0, 16.0) fromRect:rect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil]; // NSString *title =[super stringValue]; // if (title == nil) // { // title = @""; // } // // NSSize titleSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[self font] forKey:NSFontAttributeName]]; // CGFloat titleY = _comboxRect.origin.y + (_comboxRect.size.height - titleSize.height)/2; // NSRect rectTitle = dirtyRect; // rectTitle.origin = NSMakePoint(8, titleY); // rectTitle.size.height = titleSize.height; // [title drawInRect:rectTitle withAttributes:nil]; } break; default: break; } } #pragma mark - Private Methods - (void)setType:(KMOCRComboBoxType)type { _type = type; switch (type) { case KMOCRComboBoxType_None: { self.wantsLayer = YES; self.layer.cornerRadius = 5.0; self.layer.borderWidth = 0.5; self.layer.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor; self.backgroundColor = [NSColor whiteColor]; self.buttonBordered = NO; } break; default: break; } } @end @implementation KMOCRComboBoxCell - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame { NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender]))); NSRect rect = CGRectMake(8, 0, frame.size.width, frame.size.height); return NSInsetRect(rect, 0.0, offset); } - (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 @implementation KMOCRStampComboBoxCell - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame { NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender]))); return NSInsetRect(frame, 0.0, offset); } - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view { self.backgroundColor = [NSColor whiteColor]; [super drawInteriorWithFrame: [self adjustedFrameToVerticallyCenterText:frame] inView:view]; } @end