// // MessageAlert.m // XFilesPro // // Created by 青秀斌 on 14-8-15. // Copyright (c) 2014年 深圳元度科技有限公司. All rights reserved. // #if ! __has_feature(objc_arc) #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). #endif #import "MessageAlert.h" @interface MessageAlert () @property (nonatomic, strong) UIScrollView *messageView; @property (nonatomic, strong) NSMutableArray *textFields; @property (nonatomic, strong) NSMutableArray *buttons; @property (nonatomic, strong) NSMutableArray *actions; @end @implementation MessageAlert @dynamic setTitle; @dynamic setMessage; @dynamic addTextField; @dynamic addBlueButton; @dynamic addRedButton; @dynamic showAlert; @dynamic dismissAlert; @dynamic addTextFieldTwo; /**********************************************************************/ #pragma mark - OverWrite Methods /**********************************************************************/ - (void)awakeFromNib{ self.textFields = [NSMutableArray array]; self.buttons = [NSMutableArray array]; self.actions = [NSMutableArray array]; [super awakeFromNib]; } - (void)layoutSubviews{ CGFloat localtionY = 20; if (self.titleLabel != nil) { CGRect frame = self.titleLabel.frame; frame.origin.y = localtionY; [self.titleLabel setFrame:frame]; localtionY += frame.size.height + 10; } if (self.messageView != nil) { CGRect frame = self.messageView.frame; frame.origin.y = localtionY; [self.messageView setFrame:frame]; localtionY += frame.size.height + 15; } if (self.textFields.count > 0) { CGRect frame = CGRectInset(self.bounds, 20, 20);frame.size.height = 30; for (UITextField *textField in self.textFields) { frame.origin.y = localtionY; [textField setFrame:frame]; localtionY += frame.size.height + 5; } localtionY += 10; } if (self.buttons.count > 0) { CGRect frame = CGRectInset(self.bounds, 20, 20);frame.size.height = 35; if (self.buttons.count == 2) { frame.origin.y = localtionY; frame.size.width = (frame.size.width-8)/2; UIButton *button = [self.buttons objectAtIndex:0]; [button setFrame:frame]; button = [self.buttons objectAtIndex:1]; frame.origin.x = frame.origin.x + frame.size.width + 8; [button setFrame:frame]; localtionY += frame.size.height + 20; } else { for (UIButton *button in self.buttons) { frame.origin.y = localtionY; [button setFrame:frame]; localtionY += frame.size.height + 5; } localtionY += 15; } } CGRect alertFrame = self.frame; alertFrame.size.height = localtionY; [self setFrame:alertFrame]; [self setCenter:[self centerWithAnimation:kAlertAnimationFade]]; } /**********************************************************************/ #pragma mark - Private Methods /**********************************************************************/ - (AlertBlockF)setTitle{ return ^MessageAlert *(NSString *format,...) { if (format) { va_list args; va_start(args, format); NSString *title = [[NSString alloc] initWithFormat:format arguments:args]; UIFont *font = [UIFont boldSystemFontOfSize:17]; CGRect frame = CGRectInset(self.bounds, 20, 20); CGSize size = [title sizeWithFont:font byWidth:frame.size.width]; frame.size.height = size.height<20?20:size.height; if (_titleLabel == nil) { _titleLabel = [[UILabel alloc] init]; _titleLabel.numberOfLines = 99; _titleLabel.font = font; _titleLabel.textColor = [UIColor blackColor]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.backgroundColor = [UIColor clearColor]; } _titleLabel.text = title; _titleLabel.frame = frame; [self addSubview:_titleLabel]; } else { [_titleLabel removeFromSuperview]; _titleLabel = nil; } return self; }; } - (AlertBlockF)setMessage{ return ^MessageAlert *(NSString *format,...){ if (format) { va_list args; va_start(args, format); NSString *message = [[NSString alloc] initWithFormat:format arguments:args]; UIFont *font = [UIFont systemFontOfSize:15]; CGRect frame = CGRectInset(self.bounds, 20, 20); CGSize size = [message sizeWithFont:font byWidth:frame.size.width]; frame.size.height = MAX(size.height, 20); if (_messageView == nil) { _messageView = [[UIScrollView alloc] init]; [_messageView addSubview:_messageLabel]; } _messageView.contentSize = CGSizeMake(frame.size.width, frame.size.height); _messageView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, MIN(frame.size.height, 120)); if (_messageLabel == nil) { _messageLabel = [[UILabel alloc] init]; _messageLabel.numberOfLines = 99; _messageLabel.font = font; _messageLabel.textColor = [UIColor blackColor]; _messageLabel.textAlignment = NSTextAlignmentCenter; _messageLabel.backgroundColor = [UIColor clearColor]; } _messageLabel.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); _messageLabel.text = message; [_messageView addSubview:_messageLabel]; [self addSubview:_messageView]; va_end(args); } else { [_messageLabel removeFromSuperview]; _messageLabel = nil; [_messageView removeFromSuperview]; _messageView = nil; } return self; }; } - (AlertBlockT)addTextField{ return ^MessageAlert *(NSString *placeholder, NSString *text){ CGRect frame = CGRectInset(self.bounds, 20, 20); UITextField *textField = [[UITextField alloc] initWithFrame:frame]; textField.backgroundColor = RGB(237, 237, 237); textField.font = [UIFont systemFontOfSize:15]; textField.rightViewMode=UITextFieldViewModeAlways; textField.textAlignment = NSTextAlignmentLeft; textField.borderStyle = UITextBorderStyleNone; textField.placeholder = placeholder; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textField.text = text; textField.delegate = self; [self addSubview:textField]; [self.textFields addObject:textField]; return self; }; } - (AlertBlockH)addTextFieldTwo{ return ^MessageAlert *(NSString *placeholder, NSString *text, NSString *unit){ CGRect frame = CGRectInset(self.bounds, 20, 20); UITextField *textField = [[UITextField alloc] initWithFrame:frame]; textField.backgroundColor = RGB(237, 237, 237); textField.font = [UIFont systemFontOfSize:15]; textField.rightViewMode=UITextFieldViewModeAlways; textField.textAlignment = NSTextAlignmentLeft; textField.borderStyle = UITextBorderStyleNone; textField.placeholder = placeholder; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textField.text = text; textField.delegate = self; UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; [rightLabel setTextColor:[UIColor darkGrayColor]]; [rightLabel setBackgroundColor:textField.backgroundColor]; [rightLabel setText:unit]; [textField setRightView:rightLabel]; [self addSubview:textField]; [self.textFields addObject:textField]; return self; }; } - (AlertBlockB)addRedButton{ return ^MessageAlert *(NSString *title, void (^action)(MessageAlert *)) { CGRect frame = CGRectInset(self.bounds, 20, 20); UIButton *button = [[UIButton alloc] initWithFrame:frame]; button.backgroundColor = [UIColor clearColor]; button.titleLabel.textColor = [UIColor whiteColor]; button.titleLabel.font = [UIFont boldSystemFontOfSize:15]; [button setTitle:title forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage rButtonNormalImage] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage rButtonHighlightImage] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; [self.buttons addObject:button]; if (action != nil) { [self.actions addObject:action]; } else { [self.actions addObject:^(MessageAlert *alert){ [alert dismiss]; }]; } return self; }; } - (AlertBlockB)addBlueButton{ return ^MessageAlert *(NSString *title, void (^action)(MessageAlert *)) { CGRect frame = CGRectInset(self.bounds, 20, 20); UIButton *button = [[UIButton alloc] initWithFrame:frame]; button.backgroundColor = [UIColor clearColor]; button.titleLabel.textColor = [UIColor whiteColor]; button.titleLabel.font = [UIFont boldSystemFontOfSize:15]; [button setTitle:title forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage bButtonNormalImage] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage bButtonHighlightImage] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; [self.buttons addObject:button]; if (action != nil) { [self.actions addObject:action]; } else { [self.actions addObject:^(MessageAlert *alert){ [alert dismiss]; }]; } return self; }; } - (AlertBlockA)showAlert{ return ^(AlertAnimation animation){ [self showWithAnimation:animation]; return self; }; } - (AlertBlockA)dismissAlert{ return ^(AlertAnimation animation){ [self dismissWithAnimation:animation]; return self; }; } /**********************************************************************/ #pragma mark - Public Methods /**********************************************************************/ + (instancetype)alert{ return [[MessageAlert alloc] initWithFrame:CGRectMake(0, 0, 270, 75)]; } - (UITextField *)textFieldAtIndex:(NSUInteger)index{ if (index