JKMessageBoxController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // JKAlertController.m
  3. // Lock
  4. //
  5. // Created by Jack on 2016/10/15.
  6. // Copyright © 2016年 mini1. All rights reserved.
  7. //
  8. #import "JKMessageBoxController.h"
  9. #import "AppDelegate.h"
  10. #define Button_Title_Normal_Color [UIColor colorWithRed:0.0 green:0.4 blue:0.8 alpha:1]
  11. #define Button_Title_Highlight_Color [UIColor colorWithRed:0.0 green:0.3 blue:0.8 alpha:1]
  12. @interface JKMessageBoxController ()<UITextFieldDelegate>
  13. @property (nonatomic,strong) UILabel *titleLabel;
  14. @property (nonatomic,strong) UIView *buttonBackground;
  15. @property (nonatomic,strong) UIButton *cancelButton;
  16. @property (nonatomic,strong) UIButton *yesButton;
  17. @end
  18. @implementation JKMessageBoxController
  19. + (instancetype)messageBoxControllerWithTip:(NSString *)msg {
  20. JKMessageBoxController *alertController = [[JKMessageBoxController alloc] init];
  21. // 设置弹出视图时视图大小位置
  22. alertController.modalPresentationStyle = UIModalPresentationFormSheet; // 只在iPad起作用
  23. alertController.preferredContentSize = CGSizeMake(300, 130); // 只有在 UIModalPresentationFormSheet 的时候起作用
  24. alertController.titleLabel.text = msg;
  25. alertController.title = msg;
  26. return alertController;
  27. }
  28. + (instancetype)messageBoxControllerWithTip:(NSString *)msg ContentSize:(CGSize)size{
  29. JKMessageBoxController *alertController = [[JKMessageBoxController alloc] init];
  30. // 设置弹出视图时视图大小位置
  31. alertController.modalPresentationStyle = UIModalPresentationFormSheet; // 只在iPad起作用
  32. if (size.width <= 0 || size.height <= 0) {
  33. size = CGSizeMake(300, 130);
  34. }
  35. alertController.preferredContentSize = size; // 只有在 UIModalPresentationFormSheet 的时候起作用
  36. alertController.titleLabel.text = msg;
  37. alertController.title = msg;
  38. return alertController;
  39. }
  40. - (void)warning:(NSString *)msg {
  41. self.textFiled.text = @"";
  42. self.titleLabel.text = msg;
  43. self.titleLabel.textColor = [UIColor redColor];
  44. [self performSelector:@selector(resetTitleLabel) withObject:nil afterDelay:1];
  45. }
  46. - (void)resetTitleLabel {
  47. self.titleLabel.text = self.title;
  48. self.titleLabel.textColor = [UIColor blackColor];
  49. }
  50. - (UILabel *)titleLabel {
  51. if (!_titleLabel) {
  52. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.preferredContentSize.width - 20, 25)];
  53. _titleLabel.font = [UIFont systemFontOfSize:14];
  54. _titleLabel.textAlignment = NSTextAlignmentCenter;
  55. }
  56. return _titleLabel;
  57. }
  58. - (UITextField *)textFiled {
  59. if (!_textFiled) {
  60. _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.titleLabel.frame) + 10, self.preferredContentSize.width - 20, 25)];
  61. _textFiled.font = [UIFont systemFontOfSize:13];
  62. _textFiled.borderStyle = UITextBorderStyleRoundedRect;
  63. _textFiled.secureTextEntry = YES;
  64. _textFiled.delegate = self;
  65. }
  66. return _textFiled;
  67. }
  68. - (UIView *)buttonBackground {
  69. if (!_buttonBackground) {
  70. _buttonBackground = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.textFiled.frame) + 20, self.preferredContentSize.width, self.preferredContentSize.height - CGRectGetMaxY(self.textFiled.frame) - 10)];
  71. [_buttonBackground addSubview:self.cancelButton];
  72. [_buttonBackground addSubview:self.yesButton];
  73. _buttonBackground.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
  74. }
  75. return _buttonBackground;
  76. }
  77. - (UIButton *)cancelButton {
  78. if (!_cancelButton) {
  79. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  80. _cancelButton.frame = CGRectMake(0, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame));
  81. [_cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
  82. [_cancelButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal];
  83. [_cancelButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted];
  84. _cancelButton.backgroundColor = [UIColor whiteColor];
  85. _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  86. _cancelButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
  87. [_cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  88. }
  89. return _cancelButton;
  90. }
  91. - (UIButton *)yesButton {
  92. if (!_yesButton) {
  93. _yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
  94. _yesButton.frame = CGRectMake(CGRectGetMaxX(self.cancelButton.frame) + 0.5, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame));
  95. NSString *title = self.yesButtonTitle == nil ? @"ok" : self.yesButtonTitle;
  96. [_yesButton setTitle:title forState:UIControlStateNormal];
  97. [_yesButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal];
  98. [_yesButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted];
  99. _yesButton.backgroundColor = [UIColor whiteColor];
  100. _yesButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  101. _yesButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
  102. [_yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  103. }
  104. return _yesButton;
  105. }
  106. - (void)viewDidLoad {
  107. [super viewDidLoad];
  108. // Do any additional setup after loading the view.
  109. self.view.backgroundColor = [UIColor whiteColor];
  110. [self.view addSubview:self.titleLabel];
  111. [self.view addSubview:self.textFiled];
  112. [self.view addSubview:self.buttonBackground];
  113. }
  114. - (void)didReceiveMemoryWarning {
  115. [super didReceiveMemoryWarning];
  116. // Dispose of any resources that can be recreated.
  117. }
  118. - (void)cancelButtonClick:(UIButton *)sender {
  119. if (self.navigationController) {
  120. [self.navigationController popViewControllerAnimated:YES];
  121. } else {
  122. [self dismissViewControllerAnimated:YES completion:nil];
  123. }
  124. }
  125. - (void)yesButtonClick:(UIButton *)sender {
  126. if (self.textHandler) {
  127. self.textHandler(self.textFiled.text);
  128. }
  129. }
  130. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  131. if (self.changeHandler) {
  132. return self.changeHandler(textField,range,string);
  133. }
  134. return YES;
  135. }
  136. @end