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