PGDatePickManagerHeaderView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // PGDatePickManagerHeaderView.m
  3. //
  4. // Created by piggybear on 2018/1/7.
  5. // Copyright © 2018年 piggybear. All rights reserved.
  6. //
  7. #import "PGDatePickManagerHeaderView.h"
  8. #import "NSBundle+PGDatePicker.h"
  9. #import "UIColor+PGHex.h"
  10. @interface PGDatePickManagerHeaderView()
  11. @property (nonatomic, weak) UIView *lineView;
  12. @property (nonatomic, weak) UIView *middleLineView;
  13. @property (nonatomic, assign) CGSize titleLabelSize;
  14. @end
  15. @implementation PGDatePickManagerHeaderView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setupButton];
  19. }
  20. return self;
  21. }
  22. - (void)dealloc {
  23. if (self.titleLabel) {
  24. [self.titleLabel removeObserver:self forKeyPath:@"text"];
  25. }
  26. }
  27. - (void)layoutSubviews {
  28. self.titleLabel.frame = CGRectMake((self.bounds.size.width - self.titleLabelSize.width) / 2,
  29. 0,
  30. self.titleLabelSize.width,
  31. self.bounds.size.height);
  32. if (self.style == PGDatePickManagerStyle1) {
  33. self.lineView.hidden = true;
  34. [self setyle1];
  35. }else if (self.style == PGDatePickManagerStyle2) {
  36. [self setyle1];
  37. }else if (self.style == PGDatePickManagerStyle3) {
  38. [self setyle2];
  39. }
  40. }
  41. - (void)setyle1 {
  42. CGFloat lineViewHeight = 0.5;
  43. self.lineView.frame = CGRectMake(0,
  44. self.bounds.size.height - lineViewHeight,
  45. self.bounds.size.width,
  46. lineViewHeight);
  47. CGFloat buttonWidth = 80;
  48. CGFloat buttonHeight = 30;
  49. CGFloat space = 15;
  50. self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  51. self.confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  52. self.cancelButton.frame = CGRectMake(space,
  53. (self.bounds.size.height - buttonHeight) / 2,
  54. buttonWidth,
  55. buttonHeight);
  56. self.confirmButton.frame = CGRectMake(self.bounds.size.width - buttonWidth - space,
  57. (self.bounds.size.height - buttonHeight) / 2,
  58. buttonWidth,
  59. buttonHeight);
  60. }
  61. - (void)setyle2 {
  62. CGFloat lineViewHeight = 0.5;
  63. self.lineView.frame = CGRectMake(0,
  64. 0,
  65. self.bounds.size.width,
  66. lineViewHeight);
  67. CGFloat buttonWidth = self.bounds.size.width / 2;
  68. CGFloat buttonHeight = 30;
  69. self.cancelButton.frame = CGRectMake(0,
  70. (self.bounds.size.height - buttonHeight) / 2,
  71. buttonWidth,
  72. buttonHeight);
  73. self.confirmButton.frame = CGRectMake(self.bounds.size.width / 2,
  74. (self.bounds.size.height - buttonHeight) / 2,
  75. buttonWidth,
  76. buttonHeight);
  77. self.middleLineView.frame = CGRectMake(self.bounds.size.width / 2, 5, 0.5, self.bounds.size.height - 10);
  78. }
  79. - (void)setupButton {
  80. self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:18];
  81. NSString *cancelButtonText = [NSBundle localizedStringForKey:@"cancelButtonText"];
  82. [self.cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  83. [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal];
  84. [self.cancelButton addTarget:self action:@selector(cancelButtonHandler) forControlEvents:UIControlEventTouchUpInside];
  85. self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:18];
  86. NSString *confirmButtonText = [NSBundle localizedStringForKey:@"confirmButtonText"];
  87. [self.confirmButton setTitleColor:[UIColor colorWithHexString:@"#69BDFF"] forState:UIControlStateNormal];
  88. [self.confirmButton setTitle:confirmButtonText forState:UIControlStateNormal];
  89. [self.confirmButton addTarget:self action:@selector(confirmButtonHandler) forControlEvents:UIControlEventTouchUpInside];
  90. }
  91. - (void)cancelButtonHandler {
  92. if (self.cancelButtonHandlerBlock) {
  93. self.cancelButtonHandlerBlock();
  94. }
  95. }
  96. - (void)confirmButtonHandler {
  97. if (self.confirmButtonHandlerBlock) {
  98. self.confirmButtonHandlerBlock();
  99. }
  100. }
  101. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
  102. UILabel *label = object;
  103. NSString *newString = change[@"new"];
  104. CGSize size = [newString sizeWithAttributes:@{NSFontAttributeName: [label font]}];
  105. self.titleLabelSize = size;
  106. }
  107. #pragma Getter
  108. - (UIView *)lineView {
  109. if (!_lineView) {
  110. UIView *view = [[UIView alloc]init];
  111. view.backgroundColor = [UIColor lightGrayColor];
  112. [self addSubview:view];
  113. _lineView = view;
  114. }
  115. return _lineView;
  116. }
  117. - (UILabel *)titleLabel {
  118. if (!_titleLabel) {
  119. UILabel *label = [[UILabel alloc]init];
  120. [self addSubview:label];
  121. label.textColor = [UIColor colorWithHexString:@"#848484"];
  122. label.font = [UIFont boldSystemFontOfSize:17];
  123. [label addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
  124. _titleLabel = label;
  125. }
  126. return _titleLabel;
  127. }
  128. - (UIView *)middleLineView {
  129. if (!_middleLineView) {
  130. UIView *view = [[UIView alloc]init];
  131. view.backgroundColor = [UIColor lightGrayColor];
  132. [self addSubview:view];
  133. _middleLineView = view;
  134. }
  135. return _middleLineView;
  136. }
  137. - (UIButton *)confirmButton {
  138. if (!_confirmButton) {
  139. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  140. [self addSubview:button];
  141. _confirmButton = button;
  142. }
  143. return _confirmButton;
  144. }
  145. - (UIButton *)cancelButton {
  146. if (!_cancelButton) {
  147. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  148. [self addSubview:button];
  149. _cancelButton = button;
  150. }
  151. return _cancelButton;
  152. }
  153. @end