123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- //
- // 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 ()<UITextFieldDelegate>
- @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<self.textFields.count) {
- return [self.textFields objectAtIndex:index];
- }
- return nil;
- }
- - (UIButton *)buttonAtIndex:(NSUInteger)index{
- if (index<self.buttons.count) {
- return [self.buttons objectAtIndex:index];
- }
- return nil;
- }
- /**********************************************************************/
- #pragma mark - UIButton Action
- /**********************************************************************/
- - (void)buttonAction:(UIButton *)button {
- NSInteger index = [self.buttons indexOfObject:button];
- void (^action)(MessageAlert *) = [self.actions objectAtIndex:index];
- action(self);
- }
- /**********************************************************************/
- #pragma mark - UITextFieldDelegate
- /**********************************************************************/
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
- if ([string isEqualToString:@"\n"]) {
- [textField resignFirstResponder];
- return YES;
- }
- if ([string isEqualToString:@""]) {
- return YES;
- }
-
- return YES;
- }
- @end
|