ShowNewGroupAlert.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // ShowNewGroupAlert.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/4.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "ShowNewGroupAlert.h"
  9. @interface ShowNewGroupAlert()<UITextFieldDelegate>
  10. @property (copy, nonatomic) void(^confirmBlock)(NSString *groupName);
  11. @property (copy, nonatomic) void(^cancelBlock)(void);
  12. @property (strong,nonatomic) IBOutlet UILabel *showContentTitle;
  13. @property (strong,nonatomic) IBOutlet UIView *showContentBgView;
  14. @property (strong,nonatomic) IBOutlet UIView *showTextFieldBgView;
  15. @property (strong,nonatomic) IBOutlet UITextField *showTextField;
  16. @property (weak, nonatomic) IBOutlet UIButton *sureBtn;
  17. @end
  18. @implementation ShowNewGroupAlert
  19. static UINib *ViewNib = nil;
  20. + (instancetype)selectView{
  21. if (ViewNib == nil) {
  22. ViewNib = [UINib nibWithNibName:@"ShowNewGroupAlert" bundle:nil];
  23. }
  24. ShowNewGroupAlert *alert = [[ViewNib instantiateWithOwner:nil options:nil] lastObject];
  25. return alert;
  26. }
  27. /**
  28. * 初始化
  29. */
  30. +(instancetype)initShowNewGroupAlertWithTitle:(NSString *)title
  31. placeholder:(NSString *)placeholder
  32. confirm:(void(^)(NSString *groupName))confirmBlock
  33. cancle:(void(^)(void))cancleBlock{
  34. ShowNewGroupAlert *alert = [self selectView];
  35. [alert setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  36. if(title.length > 0){
  37. alert.showContentTitle.text = title;
  38. }else{
  39. [alert.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  40. }
  41. [alert.showContentBgView setRadius:12 corners:UIRectCornerAllCorners];
  42. [alert.showTextFieldBgView setRadius:2.0 borderColor:RGB(229, 229, 229) borderWidth:.5];
  43. alert.showTextField.text = @"";
  44. alert.showTextField.delegate = alert;
  45. [alert.showTextField addTarget:alert action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  46. alert.showTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  47. alert.showTextField.placeholder = placeholder;
  48. alert.showTextField.textColor = RGB(51, 51, 51);
  49. NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:alert.showTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:alert.showTextField.font}];
  50. alert.showTextField.attributedPlaceholder = attrStringONE;
  51. [alert setStatusBarStyle:UIStatusBarStyleDefault];
  52. [alert setGesture:YES];
  53. [alert setConfirmBlock:confirmBlock];
  54. [alert setCancelBlock:cancleBlock];
  55. return alert;
  56. }
  57. +(instancetype)initShowNewGroupAlertWithTitle:(NSString *)title
  58. changeStr:(NSString *)changeStr
  59. confirm:(void(^)(NSString *groupName))confirmBlock
  60. cancle:(void(^)(void))cancleBlock{
  61. ShowNewGroupAlert *alert = [self selectView];
  62. [alert setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  63. if(title.length > 0){
  64. alert.showContentTitle.text = title;
  65. }else{
  66. [alert.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  67. }
  68. [alert.showContentBgView setRadius:12 corners:UIRectCornerAllCorners];
  69. [alert.showTextFieldBgView setRadius:2.0 borderColor:RGB(229, 229, 229) borderWidth:.5];
  70. alert.showTextField.delegate = alert;
  71. alert.showTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  72. alert.showTextField.textColor = RGB(51, 51, 51);
  73. [alert.showTextField addTarget:alert action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  74. NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:alert.showTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:alert.showTextField.font}];
  75. alert.showTextField.attributedPlaceholder = attrStringONE;
  76. alert.showTextField.text = changeStr;
  77. [alert setStatusBarStyle:UIStatusBarStyleDefault];
  78. [alert setGesture:YES];
  79. [alert setConfirmBlock:confirmBlock];
  80. [alert setCancelBlock:cancleBlock];
  81. return alert;
  82. }
  83. -(IBAction)userDidBackClickAction:(id)sender{
  84. [self dismissWithAnimation:kAlertAnimationBottom];
  85. if(self.cancelBlock){
  86. self.cancelBlock();
  87. }
  88. }
  89. -(IBAction)userDidSureClickAction:(id)sender{
  90. if(self.showTextField.text.length == 0){
  91. [ShowtipTool showErrorWithStatus:@"请输入工作组名称"];
  92. }else{
  93. if (self.showTextField.text.length > 12) {
  94. SHOWERROR(@"文件夹名称最多12个字");
  95. return;
  96. }
  97. [self dismissWithAnimation:kAlertAnimationBottom];
  98. if(self.confirmBlock){
  99. self.confirmBlock(self.showTextField.text);
  100. }
  101. }
  102. }
  103. - (void)show{
  104. [super show];
  105. WS(weakSelf);
  106. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  107. [weakSelf.showTextField becomeFirstResponder];
  108. });
  109. }
  110. /****************************************************/
  111. #pragma mark - UITextFieldDelegate
  112. /****************************************************/
  113. - (void)textFieldDidChange:(UITextField *)textField
  114. {
  115. if (textField.text.length > 0) {
  116. [self.sureBtn setTitleColor:UIColorHex(0x0F7FD9) forState:UIControlStateNormal];
  117. }else{
  118. [self.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  119. }
  120. }
  121. - (void)textFieldDidEndEditing:(UITextField *)textField
  122. {
  123. if (textField.text.length > 12) {
  124. textField.text = [textField.text substringToIndex:12];
  125. SHOWERROR(@"文件夹名称最多12个字");
  126. return;
  127. }
  128. }
  129. @end