// // RQAlertViewManager.m // JSJP // // Created by 张嵘 on 2021/8/17. // #import "RQAlertViewManager.h" @interface RQAlertViewManager () @property (nonatomic, readwrite, strong) SCLAlertView *alert; @property (nonatomic, readwrite, strong) UITextView *messageTextView; @property (nonatomic, readwrite, strong) SCLButton *rePlayButton; @property (nonatomic, readwrite, strong) SCLButton *closeButton; @property (nonatomic, readwrite, assign) BOOL isShow; @end @implementation RQAlertViewManager @def_singleton(RQAlertViewManager); /** 弹出alertController,并且有两个个action按钮,分别有处理事件 @param title title @param message Message @param confirmTitle 左边按钮的title @param confirmAction 左边按钮的点击事件 */ - (void)showAlertWithTitle:(NSString * _Nullable )title message:(NSString * _Nullable )message confirmTitle:(NSString *)confirmTitle confirmAction:(void (^__nullable)(__kindof QMUIDialogViewController *dialogViewController))confirmAction { [self showAlertWithAlertType:RQAlertType_Default title:title message:message confirmTitle:confirmTitle cancelTitle:nil confirmAction:confirmAction cancelAction:nil]; } /** 弹出alertController,并且有两个个action按钮,分别有处理事件 @param alertType 弹窗类型 @param title title @param message Message @param confirmTitle 左边按钮的title @param cancelTitle 右边按钮的title @param confirmAction 左边按钮的点击事件 @param cancelAction 右按钮的点击事件 */ - (void)showAlertWithAlertType:(RQAlertType)alertType title:(NSString * _Nullable )title message:(NSString * _Nullable )message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString * _Nullable )cancelTitle confirmAction:(void (^__nullable)(__kindof QMUIDialogViewController *dialogViewController))confirmAction cancelAction:(void (^__nullable)(__kindof QMUIDialogViewController *dialogViewController))cancelAction { QMUIDialogViewController *dialogViewController = [[QMUIDialogViewController alloc] init]; dialogViewController.headerSeparatorColor = nil; dialogViewController.view.layer.cornerRadius = 12.f; dialogViewController.headerViewBackgroundColor = UIColor.whiteColor; CGFloat textHeight = [message heightForFont:RQRegularFont_16 width:RQ_SCREEN_WIDTH - 80.f - 32.f] + 16 > RQ_SCREEN_HEIGHT / 2.f? RQ_SCREEN_HEIGHT / 2.f : [message heightForFont:RQRegularFont_16 width:RQ_SCREEN_WIDTH - 80.f - 32.f] + 16; dialogViewController.headerViewHeight = (alertType == RQAlertType_Default)? (24.f + 22.f + 16.f + textHeight + 16.f) : 180.f; NSString *headerImageName = @""; CGSize headerImageSize = CGSizeZero; switch (alertType) { case RQAlertType_Rignt: headerImageName = @"正确"; headerImageSize = CGSizeMake((RQ_SCREEN_WIDTH - 80.f) / 3.f, ((RQ_SCREEN_WIDTH - 80.f) / 3.f) / (96.f / 71.f)); break; case RQAlertType_Warn: headerImageName = @"提示"; headerImageSize = CGSizeMake((RQ_SCREEN_WIDTH - 80.f) / 3.f, ((RQ_SCREEN_WIDTH - 80.f) / 3.f) / (96.f / 64.f)); break; case RQAlertType_Error: headerImageName = @"错误"; headerImageSize = CGSizeMake(RQ_SCREEN_WIDTH - 80.f, (RQ_SCREEN_WIDTH - 80.f) / (277.f / 117.f)); break; case RQAlertType_AD: headerImageName = @"激励广告"; headerImageSize = CGSizeMake(RQ_SCREEN_WIDTH - 80.f, (RQ_SCREEN_WIDTH - 80.f) / (290.f / 136.f)); break; default: break; } UIImageView *headerIconImageView = [[UIImageView alloc] initWithImage:RQImageNamed(headerImageName)]; headerIconImageView.contentMode = UIViewContentModeScaleAspectFill; if (RQStringIsNotEmpty(headerImageName)) { [dialogViewController.headerView addSubview:headerIconImageView]; [headerIconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset((alertType == RQAlertType_Error || alertType == RQAlertType_AD)? 0 : 40); make.centerX.mas_offset(0); make.size.mas_equalTo(headerImageSize); }]; } QMUILabel *titleLabel = [[QMUILabel alloc] qmui_initWithFont:RQSemiboldFont(20) textColor:[UIColor qmui_colorWithHexString:@"#040B26"]]; titleLabel.text = title; titleLabel.textAlignment = NSTextAlignmentCenter; [dialogViewController.headerView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(RQStringIsNotEmpty(headerImageName)? headerIconImageView.mas_bottom : dialogViewController.headerView.mas_top).mas_offset(24); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH - 80.f, 22.f)); }]; QMUILabel *messageLabel = [[QMUILabel alloc] qmui_initWithFont:RQRegularFont_16 textColor:[UIColor qmui_colorWithHexString:@"#5C6066"]]; NSMutableAttributedString *mutAttrStr = [[NSMutableAttributedString alloc] initWithString:message]; if ([message containsString:@"永久删除"]) { [mutAttrStr addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:[message rangeOfString:@"永久删除"]]; } if ([message containsString:@"VIP用户的VIP权益也将失效"]) { [mutAttrStr addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:[message rangeOfString:@"VIP用户的VIP权益也将失效"]]; } if (alertType == RQAlertType_SignOut) { messageLabel.attributedText = mutAttrStr; } else { messageLabel.text = message; } messageLabel.textAlignment = NSTextAlignmentCenter; messageLabel.numberOfLines = 0; [dialogViewController.headerView addSubview:messageLabel]; [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(16); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH - 80.f - 32.f, textHeight)); }]; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH - 80.f, 36)]; contentView.backgroundColor = UIColorWhite; dialogViewController.contentView = contentView; dialogViewController.footerSeparatorColor = nil; dialogViewController.footerViewBackgroundColor = UIColor.whiteColor; dialogViewController.footerViewHeight = (alertType == RQAlertType_AD)? (40.f + 16.f + 40.f + 30.f) : 84.f; dialogViewController.footerView.hidden = NO; CGFloat btnWidth = (RQ_SCREEN_WIDTH - 80.f - (16 * 2) - 16) / 2.f; QMUIButton *cancleBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; [cancleBtn setTitle:cancelTitle textColor:(alertType == RQAlertType_AD)? UIColor.whiteColor : [UIColor qmui_colorWithHexString:@"#5C6066"] Font:15 fotState:UIControlStateNormal]; cancleBtn.layer.borderColor = [UIColor qmui_colorWithHexString:@"#5C6066"].CGColor; cancleBtn.layer.borderWidth = (alertType == RQAlertType_AD)? 0 : 1; [cancleBtn setBackgroundImage:[UIImage qmui_imageWithColor:(alertType == RQAlertType_AD)? RQ_MAIN_COLOR : UIColor.whiteColor size:CGSizeMake(100.f, 40.f) cornerRadius:0] forState:UIControlStateNormal]; cancleBtn.layer.cornerRadius = 20; cancleBtn.layer.masksToBounds = YES; [cancleBtn setBlockForControlEvents:UIControlEventTouchUpInside block:^(id _Nonnull sender) { [dialogViewController hide]; if (cancelAction) { cancelAction(dialogViewController); } }]; if (RQStringIsNotEmpty(cancelTitle)) { [dialogViewController.footerView addSubview:cancleBtn]; if (alertType == RQAlertType_AD) { [cancleBtn setImage:RQImageNamed(cancelTitle) forState:UIControlStateNormal]; cancleBtn.spacingBetweenImageAndTitle = 8.f; [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.left.mas_offset(16); make.right.mas_offset(-16); make.size.mas_equalTo(CGSizeMake((RQ_SCREEN_WIDTH - 80.f - (16 * 2) - 16), 40.f)); }]; } else { [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.left.mas_offset(16); make.size.mas_equalTo(CGSizeMake(btnWidth, 40.f)); }]; } } QMUIButton *confirmBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; [confirmBtn setTitle:confirmTitle textColor:UIColor.whiteColor Font:15 fotState:UIControlStateNormal]; [confirmBtn setBackgroundImage:[UIImage qmui_imageWithColor:(alertType == RQAlertType_AD)? RQColorFromHexString(@"#FFA316") : RQ_MAIN_COLOR size:CGSizeMake(100.f, 40.f) cornerRadius:0] forState:UIControlStateNormal]; [confirmBtn setBackgroundImage:[UIImage qmui_imageWithColor:[UIColor qmui_colorWithHexString:@"#8A9099"] size:CGSizeMake(100.f, 40.f) cornerRadius:0] forState:UIControlStateDisabled]; confirmBtn.layer.cornerRadius = 20; confirmBtn.layer.masksToBounds = YES; [confirmBtn setBlockForControlEvents:UIControlEventTouchUpInside block:^(id _Nonnull sender) { [dialogViewController hide]; if (confirmAction) { confirmAction(dialogViewController); } }]; [dialogViewController.footerView addSubview:confirmBtn]; if (RQStringIsNotEmpty(confirmTitle)) { if (alertType == RQAlertType_AD) { [confirmBtn setImage:RQImageNamed(confirmTitle) forState:UIControlStateNormal]; confirmBtn.spacingBetweenImageAndTitle = 8.f; [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(cancleBtn.mas_bottom).mas_offset(16.f); make.left.mas_offset(16.f); make.right.mas_offset(- 16.f); make.size.mas_equalTo(CGSizeMake((RQ_SCREEN_WIDTH - 80.f - (16 * 2) - 16), 40.f)); }]; } else { if (RQStringIsNotEmpty(cancelTitle)) { [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.right.mas_offset(-16); make.size.mas_equalTo(CGSizeMake(btnWidth, 40.f)); }]; } else { [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.centerX.mas_offset(0.f); make.size.mas_equalTo(CGSizeMake(btnWidth, 40.f)); }]; } } } else { [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.centerX.mas_offset(0.f); make.size.mas_equalTo(CGSizeMake(btnWidth, 40.f)); }]; } if (alertType == RQAlertType_SignOut) { [confirmBtn setEnabled:!(alertType == RQAlertType_SignOut)]; __block NSInteger customIndex = 11; [[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] take:customIndex] subscribeNext:^(id x) { customIndex --; [confirmBtn setTitleNormal:[NSString stringWithFormat:@"%@(%lds)",confirmTitle,customIndex]]; [confirmBtn setEnabled:customIndex <= 0]; if (customIndex <= 0) { [confirmBtn setTitleNormal:confirmTitle]; } }]; } if (alertType == RQAlertType_AD) { QMUIButton *closeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; [closeBtn setBackgroundImage:RQImageNamed(@"关闭") forState:UIControlStateNormal]; [dialogViewController.headerView addSubview:closeBtn]; [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(16.f); make.right.mas_offset(-16.f); make.size.mas_equalTo(CGSizeMake(36.f, 36.f)); }]; [closeBtn setBlockForControlEvents:UIControlEventTouchUpInside block:^(id _Nonnull sender) { [dialogViewController hide]; }]; } [dialogViewController show]; } /** 弹出alertController,并且有三个action按钮 @param confirmOneAction 右边按钮的点击事件 @param confirmTwoAction 右边按钮的点击事件 @param cancleAction 取消按钮的点击事件 */ - (void)showAlertWithTitle:(NSString * _Nullable )title message:(NSString * _Nullable )message confirmTitleOne:(NSString * _Nullable )confirmTitleOne confirmTitleTwo:(NSString * _Nullable )confirmTitleTwo cancelTitle:(NSString * _Nullable )cancelTitle confirmOneAction:(void(^)(void))confirmOneAction confirmTwoAction:(void(^)(void))confirmTwoAction cancleAction:(void(^)(void))cancleAction { SCLAlertView *alert = [[SCLAlertView alloc] initWithNewWindowWidth:RQ_SCREEN_WIDTH - 80.f]; CGFloat viewTextWideth = RQ_SCREEN_WIDTH - (20 * 2) - (40 * 2); CGFloat textHeight = [message heightForFont:[UIFont systemFontOfSize:16] width:viewTextWideth] + 16 > RQ_SCREEN_HEIGHT / 2.f? RQ_SCREEN_HEIGHT / 2.f : [message heightForFont:[UIFont systemFontOfSize:16] width:viewTextWideth] + 16; [alert removeTopCircle]; alert.tintTopCircle = NO; alert.useLargerIcon = NO; alert.iconTintColor = RQ_MAIN_COLOR; alert.cornerRadius = 10; // NSString *imageName = @"提示"; // CGSize imageSize = imageSize = CGSizeMake((RQ_SCREEN_WIDTH - 80.f) / 3.f, ((RQ_SCREEN_WIDTH - 80.f) / 3.f) / (96.f / 64.f)); // UIImageView *iconImageView = [[UIImageView alloc] qmui_initWithSize:imageSize]; // iconImageView.image = RQImageNamed(imageName); // iconImageView.contentMode = UIViewContentModeScaleAspectFill; // [alert addCustomView:iconImageView]; // [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.mas_offset(20); // make.centerX.mas_offset(0); // make.size.mas_equalTo(imageSize); // }]; alert.labelTitle.textColor = RQ_MAIN_TEXT_COLOR_1; alert.labelTitle.font = [UIFont boldSystemFontOfSize:20]; [alert.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(20); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(viewTextWideth, 21.f)); }]; UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, alert.labelTitle.rq_y + alert.labelTitle.rq_height, viewTextWideth, textHeight)]; messageTextView.font = RQRegularFont_13; messageTextView.editable = NO; messageTextView.textColor = RQ_MAIN_TEXT_COLOR_3; messageTextView.textAlignment = NSTextAlignmentCenter; messageTextView.text = message; [alert addCustomView:messageTextView]; [messageTextView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(alert.labelTitle.mas_bottom).mas_offset(18); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(viewTextWideth, textHeight)); }]; SCLButton *confirmButton = [alert addButton:confirmTitleOne validationBlock:^BOOL{ return YES; } actionBlock:confirmOneAction]; confirmButton.buttonFormatBlock = ^NSDictionary* (void) { NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; buttonConfig[@"cornerRadius"] = @"20.f"; buttonConfig[@"backgroundColor"] = RQ_MAIN_COLOR; buttonConfig[@"textColor"] = RQ_MAIN_TEXT_COLOR_WHITE; buttonConfig[@"font"] = [UIFont systemFontOfSize:16]; return buttonConfig; }; [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(messageTextView.mas_bottom).mas_offset(15); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(viewTextWideth - 16, 40.f)); }]; SCLButton *confirmButton1 = [alert addButton:confirmTitleTwo validationBlock:^BOOL{ return YES; } actionBlock:confirmTwoAction]; confirmButton1.buttonFormatBlock = ^NSDictionary* (void) { NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; buttonConfig[@"cornerRadius"] = @"20.f"; buttonConfig[@"backgroundColor"] = RQ_MAIN_COLOR; buttonConfig[@"textColor"] = RQ_MAIN_TEXT_COLOR_WHITE; buttonConfig[@"font"] = [UIFont systemFontOfSize:16]; return buttonConfig; }; [confirmButton1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(confirmButton.mas_bottom).mas_offset(8); make.centerX.mas_offset(0); make.size.mas_equalTo(CGSizeMake(viewTextWideth - 16, 40.f)); }]; SCLButton *cancelButton = [alert addButton:cancelTitle validationBlock:^BOOL{ return YES; } actionBlock:cancleAction]; cancelButton.buttonFormatBlock = ^NSDictionary* (void) { NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; buttonConfig[@"cornerRadius"] = @"20.f"; buttonConfig[@"backgroundColor"] = RQ_MAIN_BACKGROUNDCOLOR; buttonConfig[@"textColor"] = RQColorFromHexString(@"#D5DDE0"); buttonConfig[@"borderWidth"] = @1.0f; buttonConfig[@"borderColor"] = RQColorFromHexString(@"#D5DDE0"); buttonConfig[@"font"] = [UIFont systemFontOfSize:16]; return buttonConfig; }; [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(confirmButton1.mas_bottom).mas_offset(8); make.centerX.mas_offset(0); make.bottom.mas_offset(-20); make.size.mas_equalTo(CGSizeMake(viewTextWideth - 16, 40.f)); }]; alert.horizontalButtons = YES; alert.showAnimationType = SCLAlertViewShowAnimationFadeIn; [alert showTitle:title subTitle:nil style:SCLAlertViewStyleCustom closeButtonTitle:nil duration:0.f]; } - (void)rq_ActivateVIPAlertWithComplete:(VoidBlock_Bool)complete { SCLAlertView *alert = [[SCLAlertView alloc] initWithNewWindowWidth:RQ_SCREEN_WIDTH - 80.f]; @weakify(alert) [alert removeTopCircle]; alert.tintTopCircle = NO; alert.useLargerIcon = NO; alert.shouldDismissOnTapOutside = YES; alert.iconTintColor = RQ_MAIN_COLOR; alert.cornerRadius = 10; alert.labelTitle.textColor = RQ_MAIN_TEXT_COLOR_1; alert.labelTitle.font = [UIFont boldSystemFontOfSize:15]; UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 250, 30)]; textFiled.borderStyle = UITextBorderStyleNone; textFiled.backgroundColor = RQ_LIST_BACKGROUNDCOLOR; textFiled.layer.cornerRadius = 15; textFiled.layer.borderWidth = 1; textFiled.layer.borderColor = RQ_MAIN_LINE_COLOR_1.CGColor; textFiled.clipsToBounds = YES; UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; UIImageView *imageView = [[UIImageView alloc]initWithImage:RQImageNamed(@"activationIcon")]; imageView.center = leftView.center; [leftView addSubview:imageView]; textFiled.leftView = imageView; textFiled.leftViewMode = UITextFieldViewModeAlways; //此处用来设置leftview现实时机 textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textFiled.placeholder = @"请输入激活码"; textFiled.tintColor = [UIColor colorWithHexString:@"#BFBFBF"]; textFiled.textColor = RQ_MAIN_TEXT_COLOR_3; textFiled.font = [UIFont systemFontOfSize:13]; [alert addCustomTextField:textFiled]; SCLButton *closeButton = [alert addButton:@"" actionBlock:^{ @strongify(alert); [alert hideView]; if (complete) { complete(NO); } }]; [closeButton setImage:RQImageNamed(@"close") forState:UIControlStateNormal]; [closeButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(14); make.right.mas_offset(-14); make.size.mas_equalTo(CGSizeMake(16.f, 16.f)); }]; SCLButton *activateBtn = [alert addButton:@"立即开通" validationBlock:^BOOL{ if (textFiled.text.length <= 0) { [MBProgressHUD rq_showTips:@"请输入激活码"]; } return textFiled.text.length > 0; } actionBlock:^{ if (complete) { complete(YES); } }]; activateBtn.buttonFormatBlock = ^NSDictionary* (void) { NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init]; buttonConfig[@"cornerRadius"] = @"15.f"; buttonConfig[@"backgroundColor"] = RQ_MAIN_COLOR; buttonConfig[@"textColor"] = RQ_MAIN_TEXT_COLOR_WHITE; buttonConfig[@"font"] = [UIFont systemFontOfSize:13]; return buttonConfig; }; [activateBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_offset(0); make.bottom.mas_offset(-20); make.size.mas_equalTo(CGSizeMake(RQ_FIT_HORIZONTAL(100.f), 30.f)); }]; alert.showAnimationType = SCLAlertViewShowAnimationFadeIn; [alert showTitle:@"提示" subTitle:@"您还未开通系统,请输入激活码开通!" style:SCLAlertViewStyleCustom closeButtonTitle:nil duration:0.f]; } @end