JKMessageBoxController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. CGFloat height = self.preferredContentSize.height > 130 ? (self.preferredContentSize.height - 130) / 2 + 25 : 25;
  53. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.preferredContentSize.width - 20, height)];
  54. _titleLabel.numberOfLines = 0;
  55. _titleLabel.font = [UIFont systemFontOfSize:14];
  56. _titleLabel.textAlignment = NSTextAlignmentCenter;
  57. }
  58. return _titleLabel;
  59. }
  60. - (UITextField *)textFiled {
  61. if (!_textFiled) {
  62. _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.titleLabel.frame) + 10, self.preferredContentSize.width - 20, 25)];
  63. _textFiled.font = [UIFont systemFontOfSize:13];
  64. _textFiled.borderStyle = UITextBorderStyleRoundedRect;
  65. _textFiled.secureTextEntry = YES;
  66. _textFiled.delegate = self;
  67. }
  68. return _textFiled;
  69. }
  70. - (UIView *)buttonBackground {
  71. if (!_buttonBackground) {
  72. _buttonBackground = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.textFiled.frame) + 20, self.preferredContentSize.width, self.preferredContentSize.height - CGRectGetMaxY(self.textFiled.frame) - 10)];
  73. [_buttonBackground addSubview:self.cancelButton];
  74. [_buttonBackground addSubview:self.yesButton];
  75. _buttonBackground.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
  76. }
  77. return _buttonBackground;
  78. }
  79. - (UIButton *)cancelButton {
  80. if (!_cancelButton) {
  81. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  82. _cancelButton.frame = CGRectMake(0, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame));
  83. [_cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
  84. [_cancelButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal];
  85. [_cancelButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted];
  86. _cancelButton.backgroundColor = [UIColor whiteColor];
  87. _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  88. // _cancelButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
  89. [_cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  90. }
  91. return _cancelButton;
  92. }
  93. - (UIButton *)yesButton {
  94. if (!_yesButton) {
  95. _yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
  96. _yesButton.frame = CGRectMake(CGRectGetMaxX(self.cancelButton.frame) + 0.5, 0.5, self.preferredContentSize.width / 2 - 0.25, CGRectGetHeight(self.buttonBackground.frame));
  97. NSString *title = self.yesButtonTitle == nil ? @"ok" : self.yesButtonTitle;
  98. [_yesButton setTitle:title forState:UIControlStateNormal];
  99. [_yesButton setTitleColor:Button_Title_Normal_Color forState:UIControlStateNormal];
  100. [_yesButton setTitleColor:Button_Title_Highlight_Color forState:UIControlStateHighlighted];
  101. _yesButton.backgroundColor = [UIColor whiteColor];
  102. _yesButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  103. // _yesButton.titleEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
  104. [_yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  105. }
  106. return _yesButton;
  107. }
  108. - (void)viewDidLoad {
  109. [super viewDidLoad];
  110. // Do any additional setup after loading the view.
  111. self.view.backgroundColor = [UIColor whiteColor];
  112. [self.view addSubview:self.titleLabel];
  113. [self.view addSubview:self.textFiled];
  114. [self.view addSubview:self.buttonBackground];
  115. }
  116. - (void)didReceiveMemoryWarning {
  117. [super didReceiveMemoryWarning];
  118. // Dispose of any resources that can be recreated.
  119. }
  120. - (void)cancelButtonClick:(UIButton *)sender {
  121. if (self.navigationController) {
  122. [self.navigationController popViewControllerAnimated:YES];
  123. } else {
  124. [self dismissViewControllerAnimated:YES completion:nil];
  125. }
  126. }
  127. - (void)yesButtonClick:(UIButton *)sender {
  128. if (self.textHandler) {
  129. self.textHandler(self.textFiled.text);
  130. }
  131. }
  132. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  133. if (self.changeHandler) {
  134. return self.changeHandler(textField,range,string);
  135. }
  136. return YES;
  137. }
  138. @end