CPDFDrawPencilKitFuncView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // CPDFDrawPencilKitFuncView.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFDrawPencilKitFuncView.h"
  13. #import "CPDFColorUtils.h"
  14. @interface CPDFDrawPencilKitFuncView ()
  15. @property (nonatomic,strong) UIButton *eraseButton;
  16. @end
  17. @implementation CPDFDrawPencilKitFuncView
  18. - (id)initWithFrame:(CGRect)frame {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. // Initialization code
  22. self.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  23. self.layer.borderColor = [UIColor lightGrayColor].CGColor;
  24. self.layer.borderWidth = 1.0;
  25. self.layer.cornerRadius = 5.0;
  26. [self initSubViews];
  27. }
  28. return self;
  29. }
  30. - (void)layoutSubviews {
  31. [super layoutSubviews];
  32. float tWidth = 182.0;
  33. float tHeight = 54.0;
  34. self.transform = CGAffineTransformIdentity;
  35. self.frame = CGRectMake(0, 0, tWidth, tHeight);
  36. CGFloat width = self.superview.bounds.size.width-30;
  37. CGFloat scale = width/self.bounds.size.width;
  38. if (self.frame.size.width > width) {
  39. self.transform = CGAffineTransformMakeScale(scale,scale);
  40. self.center = CGPointMake(self.superview.frame.size.width/2.0,
  41. self.frame.size.height/2.0 + 22);
  42. } else {
  43. if (@available(iOS 11.0, *)) {
  44. if (self.window.safeAreaInsets.bottom > 0) {
  45. self.center = CGPointMake(self.superview.frame.size.width - self.frame.size.width/2.0 - 40,
  46. self.frame.size.height/2.0 + 42);
  47. } else{
  48. self.center = CGPointMake(self.superview.frame.size.width - self.frame.size.width/2.0 - 30,
  49. self.frame.size.height/2.0 + 22);
  50. }
  51. }
  52. }
  53. }
  54. - (void)initSubViews {
  55. float tSpace = 10.0;
  56. float tOffsetX = tSpace;
  57. float tOffsetY = 5.0;
  58. float tWidth = 44.0;
  59. UIButton* eraseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  60. eraseBtn.tag = CPDFDrawPencilKitFuncType_Eraser;
  61. eraseBtn.layer.cornerRadius = 2.0;
  62. eraseBtn.frame = CGRectMake(tOffsetX, tOffsetY, tWidth, self.height - tOffsetY*2.0);
  63. [eraseBtn setImage:[UIImage imageNamed:@"CImageNamePencilEraserOff" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  64. [eraseBtn setImage:[UIImage imageNamed:@"CImageNamePencilEraserOn" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
  65. [eraseBtn addTarget:self action:@selector(eraserBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  66. [self addSubview:eraseBtn];
  67. self.eraseButton = eraseBtn;
  68. tOffsetX = tOffsetX + eraseBtn.frame.size.width + tSpace;
  69. UIButton* clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  70. clearBtn.tag = CPDFDrawPencilKitFuncType_Cancel;
  71. clearBtn.layer.cornerRadius = 2.0;
  72. clearBtn.frame = CGRectMake(tOffsetX, tOffsetY, tWidth+10, self.height - tOffsetY*2.0);
  73. if (@available(iOS 13.0, *)) {
  74. [clearBtn setTitleColor:[UIColor labelColor] forState:UIControlStateNormal];
  75. } else {
  76. [clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  77. }
  78. [clearBtn setTitle:NSLocalizedString(@"Discard", nil) forState:UIControlStateNormal];
  79. clearBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
  80. clearBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  81. [clearBtn addTarget:self action:@selector(clearBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  82. [self addSubview:clearBtn];
  83. tOffsetX = tOffsetX + clearBtn.frame.size.width + tSpace;
  84. UIButton* saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  85. saveBtn.tag = CPDFDrawPencilKitFuncType_Done;
  86. saveBtn.layer.cornerRadius = 2.0;
  87. saveBtn.frame = CGRectMake(tOffsetX, tOffsetY, tWidth, self.frame.size.height - tOffsetY*2.0);
  88. if (@available(iOS 13.0, *)) {
  89. [saveBtn setTitleColor:[UIColor labelColor] forState:UIControlStateNormal];
  90. } else {
  91. [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  92. }
  93. [saveBtn setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
  94. saveBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
  95. [saveBtn addTarget:self action:@selector(saveBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  96. [self addSubview:saveBtn];
  97. }
  98. - (void)resetAllSubviews {
  99. for (UIView *tSubview in self.subviews) {
  100. if ([tSubview isKindOfClass:[UIButton class]]) {
  101. UIButton *tBtn = (UIButton *)tSubview;
  102. tBtn.selected = NO;
  103. tBtn.backgroundColor = [UIColor clearColor];
  104. }
  105. }
  106. }
  107. - (void)clearBtnClicked:(UIButton *)sender {
  108. [self resetAllSubviews];
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. if ([self.delegate respondsToSelector:@selector(drawPencilFuncView:cancelBtn:)]) {
  111. [self.delegate drawPencilFuncView:self cancelBtn:(UIButton *)sender];
  112. }
  113. });
  114. }
  115. - (void)saveBtnClicked:(UIButton *)sender {
  116. [self resetAllSubviews];
  117. dispatch_async(dispatch_get_main_queue(), ^{
  118. if ([self.delegate respondsToSelector:@selector(drawPencilFuncView:saveBtn:)]) {
  119. [self.delegate drawPencilFuncView:self saveBtn:(UIButton *)sender];
  120. }
  121. });
  122. }
  123. - (void)eraserBtnClicked:(UIButton *)sender {
  124. BOOL isSelected = !sender.selected;
  125. [self resetAllSubviews];
  126. sender.selected = isSelected;
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. if ([self.delegate respondsToSelector:@selector(drawPencilFuncView:eraserBtn:)]) {
  129. [self.delegate drawPencilFuncView:self eraserBtn:(UIButton *)sender];
  130. }
  131. });
  132. }
  133. - (CGFloat)width {
  134. return CGRectGetWidth([self frame]);
  135. }
  136. - (void)setWidth:(CGFloat)width {
  137. CGRect frame = [self frame];
  138. frame.size.width = width;
  139. [self setFrame:CGRectStandardize(frame)];
  140. }
  141. - (CGFloat)height {
  142. return CGRectGetHeight([self frame]);
  143. }
  144. - (void)setHeight:(CGFloat)height {
  145. CGRect frame = [self frame];
  146. frame.size.height = height;
  147. [self setFrame:CGRectStandardize(frame)];
  148. }
  149. @end