PGDatePickManagerHeaderView.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. @property (nonatomic, assign) BOOL isSubViewLayouted;
  15. @end
  16. @implementation PGDatePickManagerHeaderView
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. if (self = [super initWithFrame:frame]) {
  19. [self setupButton];
  20. }
  21. return self;
  22. }
  23. - (void)dealloc {
  24. if (self.titleLabel) {
  25. [self.titleLabel removeObserver:self forKeyPath:@"text"];
  26. }
  27. }
  28. - (void)layoutSubviews {
  29. [super layoutSubviews];
  30. if (self.isSubViewLayouted) {
  31. return;
  32. }
  33. self.isSubViewLayouted = true;
  34. [self setupButton];
  35. self.titleLabel.frame = CGRectMake((self.bounds.size.width - self.titleLabelSize.width) / 2,
  36. 0,
  37. self.titleLabelSize.width,
  38. self.bounds.size.height);
  39. if (self.style == PGDatePickManagerStyleSheet) {
  40. self.lineView.hidden = true;
  41. [self setyle1];
  42. }else if (self.style == PGDatePickManagerStyleAlertTopButton) {
  43. [self setyle1];
  44. }else if (self.style == PGDatePickManagerStyleAlertBottomButton) {
  45. [self setyle2];
  46. }
  47. }
  48. - (void)setyle1 {
  49. CGFloat lineViewHeight = 0.5;
  50. self.lineView.frame = CGRectMake(0,
  51. self.bounds.size.height - lineViewHeight,
  52. self.bounds.size.width,
  53. lineViewHeight);
  54. CGFloat buttonWidth = 80;
  55. CGFloat buttonHeight = 30;
  56. CGFloat space = 15;
  57. self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  58. self.confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  59. self.cancelButton.frame = CGRectMake(space,
  60. (self.bounds.size.height - buttonHeight) / 2,
  61. buttonWidth,
  62. buttonHeight);
  63. self.confirmButton.frame = CGRectMake(self.bounds.size.width - buttonWidth - space,
  64. (self.bounds.size.height - buttonHeight) / 2,
  65. buttonWidth,
  66. buttonHeight);
  67. }
  68. - (void)setyle2 {
  69. CGFloat lineViewHeight = 0.5;
  70. self.lineView.frame = CGRectMake(0,
  71. 0,
  72. self.bounds.size.width,
  73. lineViewHeight);
  74. CGFloat buttonWidth = self.bounds.size.width / 2;
  75. CGFloat buttonHeight = 30;
  76. self.cancelButton.frame = CGRectMake(0,
  77. (self.bounds.size.height - buttonHeight) / 2,
  78. buttonWidth,
  79. buttonHeight);
  80. self.confirmButton.frame = CGRectMake(self.bounds.size.width / 2,
  81. (self.bounds.size.height - buttonHeight) / 2,
  82. buttonWidth,
  83. buttonHeight);
  84. self.middleLineView.frame = CGRectMake(self.bounds.size.width / 2, 5, 0.5, self.bounds.size.height - 10);
  85. }
  86. - (void)setupButton {
  87. self.cancelButton.titleLabel.font = self.cancelButtonFont;
  88. [self.cancelButton setTitleColor:self.cancelButtonTextColor forState:UIControlStateNormal];
  89. [self.cancelButton setTitle:self.cancelButtonText forState:UIControlStateNormal];
  90. [self.cancelButton addTarget:self action:@selector(cancelButtonHandler) forControlEvents:UIControlEventTouchUpInside];
  91. self.confirmButton.titleLabel.font = self.confirmButtonFont;
  92. [self.confirmButton setTitleColor:self.confirmButtonTextColor forState:UIControlStateNormal];
  93. [self.confirmButton setTitle:self.confirmButtonText forState:UIControlStateNormal];
  94. [self.confirmButton addTarget:self action:@selector(confirmButtonHandler) forControlEvents:UIControlEventTouchUpInside];
  95. }
  96. - (void)cancelButtonHandler {
  97. if (self.cancelButtonHandlerBlock) {
  98. self.cancelButtonHandlerBlock();
  99. }
  100. }
  101. - (void)confirmButtonHandler {
  102. if (self.confirmButtonHandlerBlock) {
  103. self.confirmButtonHandlerBlock();
  104. }
  105. }
  106. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
  107. UILabel *label = object;
  108. NSString *newString = change[@"new"];
  109. CGSize size = [newString sizeWithAttributes:@{NSFontAttributeName: [label font]}];
  110. self.titleLabelSize = size;
  111. }
  112. #pragma Setter
  113. - (void)setLanguage:(NSString *)language {
  114. _language = language;
  115. NSString *cancelButtonText = [NSBundle pg_localizedStringForKey:@"cancelButtonText" language:self.language];
  116. [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal];
  117. NSString *confirmButtonText = [NSBundle pg_localizedStringForKey:@"confirmButtonText" language:self.language];
  118. [self.confirmButton setTitle:confirmButtonText forState:UIControlStateNormal];
  119. }
  120. #pragma Getter
  121. - (UIView *)lineView {
  122. if (!_lineView) {
  123. UIView *view = [[UIView alloc]init];
  124. view.backgroundColor = [UIColor lightGrayColor];
  125. [self addSubview:view];
  126. _lineView = view;
  127. }
  128. return _lineView;
  129. }
  130. - (UILabel *)titleLabel {
  131. if (!_titleLabel) {
  132. UILabel *label = [[UILabel alloc]init];
  133. [self addSubview:label];
  134. label.textColor = [UIColor pg_colorWithHexString:@"#848484"];
  135. label.font = [UIFont boldSystemFontOfSize:17];
  136. [label addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
  137. _titleLabel = label;
  138. }
  139. return _titleLabel;
  140. }
  141. - (UIView *)middleLineView {
  142. if (!_middleLineView) {
  143. UIView *view = [[UIView alloc]init];
  144. view.backgroundColor = [UIColor lightGrayColor];
  145. [self addSubview:view];
  146. _middleLineView = view;
  147. }
  148. return _middleLineView;
  149. }
  150. - (UIButton *)confirmButton {
  151. if (!_confirmButton) {
  152. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  153. [self addSubview:button];
  154. _confirmButton = button;
  155. }
  156. return _confirmButton;
  157. }
  158. - (UIButton *)cancelButton {
  159. if (!_cancelButton) {
  160. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  161. [self addSubview:button];
  162. _cancelButton = button;
  163. }
  164. return _cancelButton;
  165. }
  166. - (NSString *)cancelButtonText {
  167. if (!_cancelButtonText) {
  168. NSString *cancelButtonText = [NSBundle pg_localizedStringForKey:@"cancelButtonText" language:self.language];
  169. _cancelButtonText = cancelButtonText;
  170. }
  171. return _cancelButtonText;
  172. }
  173. - (UIFont *)cancelButtonFont {
  174. if (!_cancelButtonFont) {
  175. _cancelButtonFont = [UIFont systemFontOfSize:18];
  176. }
  177. return _cancelButtonFont;
  178. }
  179. - (UIColor *)cancelButtonTextColor {
  180. if (!_cancelButtonTextColor) {
  181. _cancelButtonTextColor = [UIColor lightGrayColor];
  182. }
  183. return _cancelButtonTextColor;
  184. }
  185. - (NSString *)confirmButtonText {
  186. if (!_confirmButtonText) {
  187. NSString *confirmButtonText = [NSBundle pg_localizedStringForKey:@"confirmButtonText" language:self.language];
  188. _confirmButtonText = confirmButtonText;
  189. }
  190. return _confirmButtonText;
  191. }
  192. - (UIFont *)confirmButtonFont {
  193. if (!_confirmButtonFont) {
  194. _confirmButtonFont = [UIFont systemFontOfSize:18];
  195. }
  196. return _confirmButtonFont;
  197. }
  198. - (UIColor *)confirmButtonTextColor {
  199. if (!_confirmButtonTextColor) {
  200. _confirmButtonTextColor = [UIColor pg_colorWithHexString:@"#69BDFF"];
  201. }
  202. return _confirmButtonTextColor;
  203. }
  204. @end