// // JKAlertController.m // Lock // // Created by Jack on 2016/10/15. // Copyright © 2016年 mini1. All rights reserved. // #import "JKMessageBoxController.h" #import "AppDelegate.h" #define Button_Title_Normal_Color [UIColor colorWithRed:0.0 green:0.4 blue:0.8 alpha:1] #define Button_Title_Highlight_Color [UIColor colorWithRed:0.0 green:0.3 blue:0.8 alpha:1] @interface JKMessageBoxController () @property (nonatomic,strong) UILabel *titleLabel; @property (nonatomic,strong) UIView *buttonBackground; @property (nonatomic,strong) UIButton *cancelButton; @property (nonatomic,strong) UIButton *yesButton; @end @implementation JKMessageBoxController + (instancetype)messageBoxControllerWithTip:(NSString *)msg { JKMessageBoxController *alertController = [[JKMessageBoxController alloc] init]; // 设置弹出视图时视图大小位置 alertController.modalPresentationStyle = UIModalPresentationFormSheet; // 只在iPad起作用 alertController.preferredContentSize = CGSizeMake(300, 130); // 只有在 UIModalPresentationFormSheet 的时候起作用 alertController.titleLabel.text = msg; alertController.title = msg; return alertController; } + (instancetype)messageBoxControllerWithTip:(NSString *)msg ContentSize:(CGSize)size{ JKMessageBoxController *alertController = [[JKMessageBoxController alloc] init]; // 设置弹出视图时视图大小位置 alertController.modalPresentationStyle = UIModalPresentationFormSheet; // 只在iPad起作用 if (size.width <= 0 || size.height <= 0) { size = CGSizeMake(300, 130); } alertController.preferredContentSize = size; // 只有在 UIModalPresentationFormSheet 的时候起作用 alertController.titleLabel.text = msg; alertController.title = msg; return alertController; } - (void)warning:(NSString *)msg { self.textFiled.text = @""; self.titleLabel.text = msg; self.titleLabel.textColor = [UIColor redColor]; [self performSelector:@selector(resetTitleLabel) withObject:nil afterDelay:1]; } - (void)resetTitleLabel { self.titleLabel.text = self.title; self.titleLabel.textColor = [UIColor blackColor]; } - (UILabel *)titleLabel { if (!_titleLabel) { CGFloat height = self.preferredContentSize.height > 130 ? (self.preferredContentSize.height - 130) / 2 + 25 : 25; _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.preferredContentSize.width - 20, height)]; _titleLabel.numberOfLines = 0; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textAlignment = NSTextAlignmentCenter; } return _titleLabel; } - (UITextField *)textFiled { if (!_textFiled) { _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.titleLabel.frame) + 10, self.preferredContentSize.width - 20, 25)]; _textFiled.font = [UIFont systemFontOfSize:13]; _textFiled.borderStyle = UITextBorderStyleRoundedRect; _textFiled.secureTextEntry = YES; _textFiled.delegate = self; } return _textFiled; } - (UIView *)buttonBackground { if (!_buttonBackground) { _buttonBackground = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.textFiled.frame) + 20, self.preferredContentSize.width, self.preferredContentSize.height - CGRectGetMaxY(self.textFiled.frame) - 10)]; [_buttonBackground addSubview:self.cancelButton]; [_buttonBackground addSubview:self.yesButton]; _buttonBackground.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1]; } return _buttonBackground; } - (UIButton *)cancelButton { if (!_cancelButton) { _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; _cancelButton.frame = CGRectMake(0, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame)); [_cancelButton setTitle:@"cancel" forState:UIControlStateNormal]; [_cancelButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal]; [_cancelButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted]; _cancelButton.backgroundColor = [UIColor whiteColor]; _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.0]; // _cancelButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0); [_cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (UIButton *)yesButton { if (!_yesButton) { _yesButton = [UIButton buttonWithType:UIButtonTypeCustom]; _yesButton.frame = CGRectMake(CGRectGetMaxX(self.cancelButton.frame) + 0.5, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame)); NSString *title = self.yesButtonTitle == nil ? @"ok" : self.yesButtonTitle; [_yesButton setTitle:title forState:UIControlStateNormal]; [_yesButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal]; [_yesButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted]; _yesButton.backgroundColor = [UIColor whiteColor]; _yesButton.titleLabel.font = [UIFont systemFontOfSize:15.0]; // _yesButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0); [_yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside]; } return _yesButton; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.titleLabel]; [self.view addSubview:self.textFiled]; [self.view addSubview:self.buttonBackground]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)cancelButtonClick:(UIButton *)sender { if (self.navigationController) { [self.navigationController popViewControllerAnimated:YES]; } else { [self dismissViewControllerAnimated:YES completion:nil]; } } - (void)yesButtonClick:(UIButton *)sender { if (self.textHandler) { self.textHandler(self.textFiled.text); } } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (self.changeHandler) { return self.changeHandler(textField,range,string); } return YES; } @end