// // ShowNewGroupAlert.m // smartRhino // // Created by armin on 2019/11/4. // Copyright © 2019 tederen. All rights reserved. // #import "ShowNewGroupAlert.h" @interface ShowNewGroupAlert() @property (copy, nonatomic) void(^confirmBlock)(NSString *groupName); @property (copy, nonatomic) void(^cancelBlock)(void); @property (strong,nonatomic) IBOutlet UILabel *showContentTitle; @property (strong,nonatomic) IBOutlet UIView *showContentBgView; @property (strong,nonatomic) IBOutlet UIView *showTextFieldBgView; @property (strong,nonatomic) IBOutlet UITextField *showTextField; @property (weak, nonatomic) IBOutlet UIButton *sureBtn; @end @implementation ShowNewGroupAlert static UINib *ViewNib = nil; + (instancetype)selectView{ if (ViewNib == nil) { ViewNib = [UINib nibWithNibName:@"ShowNewGroupAlert" bundle:nil]; } ShowNewGroupAlert *alert = [[ViewNib instantiateWithOwner:nil options:nil] lastObject]; return alert; } /** * 初始化 */ +(instancetype)initShowNewGroupAlertWithTitle:(NSString *)title placeholder:(NSString *)placeholder confirm:(void(^)(NSString *groupName))confirmBlock cancle:(void(^)(void))cancleBlock{ ShowNewGroupAlert *alert = [self selectView]; [alert setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; if(title.length > 0){ alert.showContentTitle.text = title; }else{ [alert.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; } [alert.showContentBgView setRadius:12 corners:UIRectCornerAllCorners]; [alert.showTextFieldBgView setRadius:2.0 borderColor:RGB(229, 229, 229) borderWidth:.5]; alert.showTextField.text = @""; alert.showTextField.delegate = alert; [alert.showTextField addTarget:alert action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; alert.showTextField.clearButtonMode = UITextFieldViewModeWhileEditing; alert.showTextField.placeholder = placeholder; alert.showTextField.textColor = RGB(51, 51, 51); NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:alert.showTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:alert.showTextField.font}]; alert.showTextField.attributedPlaceholder = attrStringONE; [alert setStatusBarStyle:UIStatusBarStyleDefault]; [alert setGesture:YES]; [alert setConfirmBlock:confirmBlock]; [alert setCancelBlock:cancleBlock]; return alert; } +(instancetype)initShowNewGroupAlertWithTitle:(NSString *)title changeStr:(NSString *)changeStr confirm:(void(^)(NSString *groupName))confirmBlock cancle:(void(^)(void))cancleBlock{ ShowNewGroupAlert *alert = [self selectView]; [alert setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; if(title.length > 0){ alert.showContentTitle.text = title; }else{ [alert.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; } [alert.showContentBgView setRadius:12 corners:UIRectCornerAllCorners]; [alert.showTextFieldBgView setRadius:2.0 borderColor:RGB(229, 229, 229) borderWidth:.5]; alert.showTextField.delegate = alert; alert.showTextField.clearButtonMode = UITextFieldViewModeWhileEditing; alert.showTextField.textColor = RGB(51, 51, 51); [alert.showTextField addTarget:alert action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:alert.showTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:alert.showTextField.font}]; alert.showTextField.attributedPlaceholder = attrStringONE; alert.showTextField.text = changeStr; [alert setStatusBarStyle:UIStatusBarStyleDefault]; [alert setGesture:YES]; [alert setConfirmBlock:confirmBlock]; [alert setCancelBlock:cancleBlock]; return alert; } -(IBAction)userDidBackClickAction:(id)sender{ [self dismissWithAnimation:kAlertAnimationBottom]; if(self.cancelBlock){ self.cancelBlock(); } } -(IBAction)userDidSureClickAction:(id)sender{ if(self.showTextField.text.length == 0){ [ShowtipTool showErrorWithStatus:@"请输入工作组名称"]; }else{ if (self.showTextField.text.length > 12) { SHOWERROR(@"文件夹名称最多12个字"); return; } [self dismissWithAnimation:kAlertAnimationBottom]; if(self.confirmBlock){ self.confirmBlock(self.showTextField.text); } } } - (void)show{ [super show]; WS(weakSelf); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.showTextField becomeFirstResponder]; }); } /****************************************************/ #pragma mark - UITextFieldDelegate /****************************************************/ - (void)textFieldDidChange:(UITextField *)textField { if (textField.text.length > 0) { [self.sureBtn setTitleColor:UIColorHex(0x0F7FD9) forState:UIControlStateNormal]; }else{ [self.sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; } } - (void)textFieldDidEndEditing:(UITextField *)textField { if (textField.text.length > 12) { textField.text = [textField.text substringToIndex:12]; SHOWERROR(@"文件夹名称最多12个字"); return; } } @end