KMOCRComboBox.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // KMComboBox.m
  3. // SignFlow
  4. //
  5. // Created by wanjun on 2021/1/15.
  6. //
  7. #import "KMOCRComboBox.h"
  8. @implementation KMOCRComboBox
  9. #pragma mark - Draw
  10. - (void)drawRect:(NSRect)dirtyRect {
  11. [super drawRect:dirtyRect];
  12. switch (self.type) {
  13. case KMOCRComboBoxType_None:
  14. {
  15. NSPoint p = _comboxRect.origin;
  16. p.x += _comboxRect.size.width - 20;
  17. p.y += (_comboxRect.size.height-16)/2;
  18. [[NSColor whiteColor] setFill];
  19. NSRectFill(NSMakeRect(p.x, 0, 16.0, _comboxRect.size.height));
  20. NSImage *image = [NSImage imageNamed:@"KMImageNameUXIconBtnArrowDown"];
  21. NSRect rect =NSZeroRect;
  22. rect.size = CGSizeMake(16.0, 16.0);
  23. [image drawInRect:NSMakeRect(p.x, p.y, 16.0, 16.0) fromRect:rect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
  24. // NSString *title =[super stringValue];
  25. // if (title == nil)
  26. // {
  27. // title = @"";
  28. // }
  29. //
  30. // NSSize titleSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[self font] forKey:NSFontAttributeName]];
  31. // CGFloat titleY = _comboxRect.origin.y + (_comboxRect.size.height - titleSize.height)/2;
  32. // NSRect rectTitle = dirtyRect;
  33. // rectTitle.origin = NSMakePoint(8, titleY);
  34. // rectTitle.size.height = titleSize.height;
  35. // [title drawInRect:rectTitle withAttributes:nil];
  36. }
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. #pragma mark - Private Methods
  43. - (void)setType:(KMOCRComboBoxType)type {
  44. _type = type;
  45. switch (type) {
  46. case KMOCRComboBoxType_None:
  47. {
  48. self.wantsLayer = YES;
  49. self.layer.cornerRadius = 5.0;
  50. self.layer.borderWidth = 0.5;
  51. self.layer.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
  52. self.backgroundColor = [NSColor whiteColor];
  53. self.buttonBordered = NO;
  54. }
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. @end
  61. @implementation KMOCRComboBoxCell
  62. - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame
  63. {
  64. NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender])));
  65. NSRect rect = CGRectMake(8, 0, frame.size.width, frame.size.height);
  66. return NSInsetRect(rect, 0.0, offset);
  67. }
  68. - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event
  69. {
  70. [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate event:event];
  71. }
  72. - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate start:(NSInteger)start length:(NSInteger)length
  73. {
  74. [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate start:start length:length];
  75. }
  76. - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view
  77. {
  78. [super drawInteriorWithFrame:
  79. [self adjustedFrameToVerticallyCenterText:frame] inView:view];
  80. }
  81. @end
  82. @implementation KMOCRStampComboBoxCell
  83. - (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame
  84. {
  85. NSInteger offset = floor((NSHeight(frame)/2 - ([[self font] ascender] + [[self font] descender])));
  86. return NSInsetRect(frame, 0.0, offset);
  87. }
  88. - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view
  89. {
  90. self.backgroundColor = [NSColor whiteColor];
  91. [super drawInteriorWithFrame:
  92. [self adjustedFrameToVerticallyCenterText:frame] inView:view];
  93. }
  94. @end