Skip to content

Commit 2d480a2

Browse files
12207480tanyang
authored andcommitted
content custom
1 parent c92076a commit 2d480a2

File tree

3 files changed

+101
-35
lines changed

3 files changed

+101
-35
lines changed

TYAlertControllerDemo/TYAlertController/TYAlertView.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,27 @@ typedef enum : NSUInteger {
3131
@property (nonatomic, weak, readonly) UILabel *titleLable;
3232
@property (nonatomic, weak, readonly) UILabel *messageLabel;
3333

34+
// contentView space custom
35+
@property (nonatomic, assign) CGFloat contentViewSpace;
36+
37+
// textLabel custom
38+
@property (nonatomic, assign) CGFloat textLabelSpace;
39+
@property (nonatomic, assign) CGFloat textLabelContentViewEdge;
40+
41+
// button custom
42+
@property (nonatomic, assign) CGFloat buttonHeight;
43+
@property (nonatomic, assign) CGFloat buttonSpace;
44+
@property (nonatomic, assign) CGFloat buttonContentViewEdge;
45+
46+
// textFeild custom
3447
@property (nonatomic, strong) UIColor *textFieldBorderColor;
48+
@property (nonatomic, strong) UIColor *textFieldBackgroudColor;
49+
@property (nonatomic, strong) UIFont *textFieldFont;
50+
@property (nonatomic, assign) CGFloat textFeildHeight;
51+
@property (nonatomic, assign) CGFloat textFeildEdge;
52+
@property (nonatomic, assign) CGFloat textFeildorderWidth;
53+
@property (nonatomic, assign) CGFloat textFeildContentViewEdge;
54+
3555

3656
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message;
3757

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,29 @@ @interface TYAlertView ()
5454
@property (nonatomic, weak) UIView *textFeildContentView;
5555
@property (nonatomic, weak) NSLayoutConstraint *textFeildTopConstraint;
5656
@property (nonatomic, strong) NSMutableArray *textFeilds;
57+
@property (nonatomic, strong) NSMutableArray *textFeildSeparateViews;
5758

5859
// button content View
5960
@property (nonatomic, weak) UIView *buttonContentView;
6061
@property (nonatomic, strong) NSMutableArray *buttons;
6162
@property (nonatomic, strong) NSMutableArray *actions;
6263

63-
@property (nonatomic, assign) CGFloat contentViewSpace;
64-
@property (nonatomic, assign) CGFloat textLabelSpace;
65-
@property (nonatomic, assign) CGFloat contentViewEdge;
66-
6764
@end
6865

66+
#define kContentViewEdge 15
67+
#define kContentViewSpace 15
68+
69+
#define kTextLabelSpace 6
70+
6971
#define kButtonTagOffset 1000
72+
#define kButtonSpace 6
7073
#define KButtonHeight 44
74+
7175
#define kTextFeildOffset 10000
72-
#define kTextFeildHeight 30
73-
#define kTextFeildBorderColor [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:1];
76+
#define kTextFeildHeight 29
77+
#define kTextFeildEdge 8
78+
#define KTextFeildBorderWidth 0.5
79+
7480

7581
@implementation TYAlertView
7682

@@ -110,16 +116,31 @@ + (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message
110116
return [[self alloc]initWithTitle:title message:message];
111117
}
112118

119+
#pragma mark - configure
120+
113121
- (void)configureProperty
114122
{
115-
_contentViewSpace = 15;
116-
_contentViewEdge = 12;
117-
_textLabelSpace = 6;
123+
self.backgroundColor = [UIColor whiteColor];
124+
_contentViewSpace = kContentViewSpace;
125+
126+
_textLabelSpace = kTextLabelSpace;
127+
_textLabelContentViewEdge = kContentViewEdge;
128+
129+
_buttonHeight = KButtonHeight;
130+
_buttonSpace = kButtonSpace;
131+
_buttonContentViewEdge = kContentViewEdge;
132+
133+
_textFeildHeight = kTextFeildHeight;
134+
_textFeildEdge = kTextFeildEdge;
135+
_textFeildorderWidth = KTextFeildBorderWidth;
136+
_textFeildContentViewEdge = kContentViewEdge;
137+
138+
_textFieldBorderColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:1];
139+
_textFieldBackgroudColor = [UIColor whiteColor];
140+
_textFieldFont = [UIFont systemFontOfSize:14];
118141

119142
_buttons = [NSMutableArray array];
120143
_actions = [NSMutableArray array];
121-
122-
_textFieldBorderColor = kTextFeildBorderColor;
123144
}
124145

125146
#pragma mark - add contentview
@@ -183,7 +204,7 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textFeild))c
183204

184205
UITextField *textField = [[UITextField alloc]init];
185206
textField.tag = kTextFeildOffset + _textFeilds.count;
186-
textField.backgroundColor = [UIColor whiteColor];
207+
textField.font = _textFieldFont;
187208
textField.translatesAutoresizingMaskIntoConstraints = NO;
188209

189210
if (configurationHandler) {
@@ -193,6 +214,17 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textFeild))c
193214
[_textFeildContentView addSubview:textField];
194215
[_textFeilds addObject:textField];
195216

217+
if (_textFeilds.count > 1) {
218+
if (_textFeildSeparateViews == nil) {
219+
_textFeildSeparateViews = [NSMutableArray array];
220+
}
221+
UIView *separateView = [[UIView alloc]init];
222+
separateView.backgroundColor = _textFieldBorderColor;
223+
separateView.translatesAutoresizingMaskIntoConstraints = NO;
224+
[_textFeildContentView addSubview:separateView];
225+
[_textFeildSeparateViews addObject:separateView];
226+
}
227+
196228
[self layoutTextFeilds];
197229
}
198230

@@ -203,20 +235,20 @@ - (void)layoutContentViews
203235
// textContentView
204236
_textContentView.translatesAutoresizingMaskIntoConstraints = NO;
205237

206-
[self addConstarintWithView:_textContentView topView:self leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(_contentViewEdge, _contentViewEdge, 0, -_contentViewEdge)];
238+
[self addConstarintWithView:_textContentView topView:self leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(_contentViewSpace, _textLabelContentViewEdge, 0, -_textLabelContentViewEdge)];
207239

208240
// textFeildContentView
209241
_textFeildContentView.translatesAutoresizingMaskIntoConstraints = NO;
210242
_textFeildTopConstraint = [self addConstarintWithTopView:_textContentView toBottomView:_textFeildContentView constarint:0];
211243

212-
[self addConstarintWithView:_textFeildContentView topView:nil leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(0, _contentViewEdge, 0, -_contentViewEdge)];
244+
[self addConstarintWithView:_textFeildContentView topView:nil leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(0, _textFeildContentViewEdge, 0, -_textFeildContentViewEdge)];
213245

214246
// buttonContentView
215247
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
216248

217249
[self addConstarintWithTopView:_textFeildContentView toBottomView:_buttonContentView constarint:_contentViewSpace];
218250

219-
[self addConstarintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edageInset:UIEdgeInsetsMake(0, _contentViewEdge, -_contentViewEdge, -_contentViewEdge)];
251+
[self addConstarintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edageInset:UIEdgeInsetsMake(0, _buttonContentViewEdge, -_contentViewSpace, -_buttonContentViewEdge)];
220252
}
221253

222254
- (void)layoutTextLabels
@@ -234,12 +266,12 @@ - (void)layoutButtons
234266
UIButton *button = _buttons.lastObject;
235267
if (_buttons.count == 1) {
236268
[button addConstraintToView:_buttonContentView edageInset:UIEdgeInsetsZero];
237-
[button addConstarintWidth:0 height:KButtonHeight];
269+
[button addConstarintWidth:0 height:_buttonHeight];
238270
}else if (_buttons.count == 2) {
239271
UIButton *firstButton = _buttons.firstObject;
240272
[_buttonContentView removeConstraintWithView:firstButton attribte:NSLayoutAttributeRight];
241273
[_buttonContentView addConstarintWithView:button topView:_buttonContentView leftView:nil bottomView:nil rightView:_buttonContentView edageInset:UIEdgeInsetsZero];
242-
[_buttonContentView addConstarintWithLeftView:firstButton toRightView:button constarint:_textLabelSpace];
274+
[_buttonContentView addConstarintWithLeftView:firstButton toRightView:button constarint:_buttonSpace];
243275
[_buttonContentView addConstarintEqualWithView:button widthToView:firstButton heightToView:firstButton];
244276
}else {
245277
if (_buttons.count == 3) {
@@ -249,12 +281,12 @@ - (void)layoutButtons
249281
[_buttonContentView removeConstraintWithView:firstBtn attribte:NSLayoutAttributeBottom];
250282
[_buttonContentView removeConstraintWithView:secondBtn attribte:NSLayoutAttributeTop];
251283
[_buttonContentView addConstarintWithView:firstBtn topView:nil leftView:nil bottomView:0 rightView:_buttonContentView edageInset:UIEdgeInsetsZero];
252-
[_buttonContentView addConstarintWithTopView:firstBtn toBottomView:secondBtn constarint:_textLabelSpace];
284+
[_buttonContentView addConstarintWithTopView:firstBtn toBottomView:secondBtn constarint:_buttonSpace];
253285

254286
}
255287
UIButton *lastSecondBtn = _buttons[_buttons.count-2];
256288
[_buttonContentView removeConstraintWithView:lastSecondBtn attribte:NSLayoutAttributeBottom];
257-
[_buttonContentView addConstarintWithTopView:lastSecondBtn toBottomView:button constarint:_textLabelSpace];
289+
[_buttonContentView addConstarintWithTopView:lastSecondBtn toBottomView:button constarint:_buttonSpace];
258290
[_buttonContentView addConstarintWithView:button topView:nil leftView:_buttonContentView bottomView:_buttonContentView rightView:_buttonContentView edageInset:UIEdgeInsetsZero];
259291
[_buttonContentView addConstarintEqualWithView:button widthToView:nil heightToView:lastSecondBtn];
260292
}
@@ -266,28 +298,34 @@ - (void)layoutTextFeilds
266298

267299
if (_textFeilds.count == 1) {
268300
// setup textFeildContentView
269-
_textFeildContentView.backgroundColor = _textFieldBorderColor;
301+
_textFeildContentView.backgroundColor = _textFieldBackgroudColor;
270302
_textFeildContentView.layer.masksToBounds = YES;
271303
_textFeildContentView.layer.cornerRadius = 4;
272-
_textFeildContentView.layer.borderWidth = 0.5;
304+
_textFeildContentView.layer.borderWidth = _textFeildorderWidth;
273305
_textFeildContentView.layer.borderColor = _textFieldBorderColor.CGColor;
274306
_textFeildTopConstraint.constant = -_contentViewSpace;
275-
[textFeild addConstraintToView:_textFeildContentView edageInset:UIEdgeInsetsZero];
276-
[textFeild addConstarintWidth:0 height:kTextFeildHeight];
307+
[textFeild addConstraintToView:_textFeildContentView edageInset:UIEdgeInsetsMake(_textFeildorderWidth, _textFeildEdge, -_textFeildorderWidth, -_textFeildEdge)];
308+
[textFeild addConstarintWidth:0 height:_textFeildHeight];
277309
}else {
278-
UITextField *lastSecondTextFeild = _textFeilds[_textFeilds.count-2];
310+
// textFeild
311+
UITextField *lastSecondTextFeild = _textFeilds[_textFeilds.count - 2];
279312
[_textFeildContentView removeConstraintWithView:lastSecondTextFeild attribte:NSLayoutAttributeBottom];
280-
[_textFeildContentView addConstarintWithTopView:lastSecondTextFeild toBottomView:textFeild constarint:0.5];
281-
[_textFeildContentView addConstarintWithView:textFeild topView:nil leftView:_textFeildContentView bottomView:_textFeildContentView rightView:_textFeildContentView edageInset:UIEdgeInsetsZero];
313+
[_textFeildContentView addConstarintWithTopView:lastSecondTextFeild toBottomView:textFeild constarint:_textFeildorderWidth];
314+
[_textFeildContentView addConstarintWithView:textFeild topView:nil leftView:_textFeildContentView bottomView:_textFeildContentView rightView:_textFeildContentView edageInset:UIEdgeInsetsMake(0, _textFeildEdge, -_textFeildorderWidth, -_textFeildEdge)];
282315
[_textFeildContentView addConstarintEqualWithView:textFeild widthToView:nil heightToView:lastSecondTextFeild];
316+
317+
// separateview
318+
UIView *separateView = _textFeildSeparateViews[_textFeilds.count - 2];
319+
[_textFeildContentView addConstarintWithView:separateView topView:nil leftView:_textFeildContentView bottomView:nil rightView:_textFeildContentView edageInset:UIEdgeInsetsZero];
320+
[_textFeildContentView addConstarintWithTopView:separateView toBottomView:textFeild constarint:0];
321+
[separateView addConstarintWidth:0 height:_textFeildorderWidth];
283322
}
284323
}
285324

286325
#pragma mark - action
287326

288327
- (void)actionButtonClicked:(UIButton *)button
289328
{
290-
NSLog(@"actionButtonClicked tag:%ld",button.tag);
291329
TYAlertAction *action = _actions[button.tag - kButtonTagOffset];
292330

293331
if (action.handler) {

TYAlertControllerDemo/ViewController.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (IBAction)showAlertView:(id)sender {
2828
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 120)];
2929
view.backgroundColor = [UIColor blueColor];
3030
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
31-
alertView.backgroundColor = [UIColor whiteColor];
31+
3232
__typeof (self) __weak weakSelf = self;
3333
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
3434

@@ -37,9 +37,9 @@ - (IBAction)showAlertView:(id)sender {
3737

3838
}]];
3939

40-
[alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
41-
42-
}]];
40+
// [alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
41+
//
42+
// }]];
4343
// [alertView addAction:[TYAlertAction actionWithTitle:@"默认2" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
4444
//
4545
// }]];
@@ -49,9 +49,6 @@ - (IBAction)showAlertView:(id)sender {
4949
}];
5050
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
5151

52-
}];
53-
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
54-
5552
}];
5653

5754
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
@@ -62,7 +59,18 @@ - (IBAction)showActionSheet:(id)sender {
6259
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 120)];
6360
view.backgroundColor = [UIColor blueColor];
6461
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
65-
alertView.backgroundColor = [UIColor whiteColor];
62+
63+
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
64+
65+
}]];
66+
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
67+
68+
}]];
69+
70+
[alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
71+
72+
}]];
73+
6674
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleActionSheet];
6775
[self presentViewController:alertController animated:YES completion:nil];
6876
}

0 commit comments

Comments
 (0)