// // LZAlertWindow.m // TIANYAN // // Created by CY on 2021/4/23. // Copyright © 2021 leo. All rights reserved. // #import "LZAlertWindow.h" #import "UIViewController+TFPresent.h" @implementation LZAlertAction + (instancetype)actionWithTitle:(NSString *)title handler:(void (^)(LZAlertAction *action))handler{ return [[self alloc] initWithTitle:title handler:handler]; } - (instancetype)initWithTitle:(NSString *)title handler:(void (^)(LZAlertAction *action))handler{ if (self = [super init]) { self.title = title; self.handler = handler; [self youpaifinitialization]; } return self; } - (void)youpaifinitialization{ self.bgColor = [UIColor whiteColor]; self.cornerRadius = 24.0f; self.color = [UIColor blackColor]; self.font = LCFont(17.0f); } @end @interface LZAlertWindow () @property (nonatomic, copy) void (^contentStyleBlock)(UILabel *contentL); @end @implementation LZAlertWindow + (instancetype)alertWithTitle:(NSString *)title content:(NSString *)content action:(NSArray *)actions{ return [[LZAlertWindow alloc] initWithTitle:title content:content action:actions]; } - (instancetype)initWithTitle:(NSString *)title content:(NSString *)content action:(NSArray *)actions{ return [self initWithTitle:title content:content action:actions contentStyleBlock:nil]; } + (instancetype)alertWithTitle:(NSString *)title content:(NSString *)content action:(NSArray *)actions contentStyleBlock:(void (^)(UILabel *))contentStyleBlock{ return [[LZAlertWindow alloc] initWithTitle:title content:content action:actions contentStyleBlock:contentStyleBlock]; } - (instancetype)initWithTitle:(NSString *)title content:(NSString *)content action:(NSArray *)actions contentStyleBlock:(void (^)(UILabel *))contentStyleBlock{ if (self = [super init]) { self.zTitle = title; self.content = content; self.actions = actions; self.contentStyleBlock = contentStyleBlock; [self youpaifinitialization]; } return self; } - (void)youpaifinitialization{ self.bgColor = self.bgColor; self.cornerRadius = 10.0f; self.titleColor = LZ273145Color; self.titleFont = LCBoldFont(19.0f); self.contentColor = LZ273145Color; self.contentFont = LCFont(15.0f); self.contentHorizontalSpace = 14.0f; self.contentTextAlignment = NSTextAlignmentLeft; } - (void)viewDidLoad { [super viewDidLoad]; self.baseView.hidden = YES; [self youpaifsetupUI]; } - (void)youpaifsetupUI{ UIView *bgV = [[UIView alloc] init]; bgV.backgroundColor = [UIColor whiteColor]; bgV.layer.cornerRadius = self.cornerRadius; bgV.clipsToBounds = YES; [self.view addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(32.0f); make.right.offset(-32.0f); make.centerY.equalTo(self.view); }]; UILabel *titleL = nil; if (self.zTitle.length != 0) { titleL = [[UILabel alloc] init]; titleL.font = self.titleFont; titleL.textColor = self.titleColor; titleL.textAlignment = NSTextAlignmentCenter; titleL.text = self.zTitle; [bgV addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(31.0f); make.left.offset(14.0f); make.right.offset(-14.0f); }]; } UILabel *contentL = [[UILabel alloc] init]; contentL.font = self.contentFont; contentL.textColor = self.contentColor; contentL.numberOfLines = 0; NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:self.content]; NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; paragraphStyle1.alignment=NSTextAlignmentJustified; NSDictionary * dic =@{ NSParagraphStyleAttributeName:paragraphStyle1, NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone], }; [attributedString1 setAttributes:dic range:NSMakeRange(0, attributedString1.length)]; [contentL setAttributedText:attributedString1]; contentL.textAlignment = self.contentTextAlignment; [bgV addSubview:contentL]; if (self.contentStyleBlock != nil) { self.contentStyleBlock(contentL); } if (titleL == nil) { [contentL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(45.0f); make.left.offset(40.0f); make.right.offset(-40.0f); }]; }else{ [contentL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(20.0f); make.left.offset(self.contentHorizontalSpace); make.right.offset(-self.contentHorizontalSpace); }]; } NSMutableArray *btns = [NSMutableArray array]; for (NSInteger i = 0; i < self.actions.count; i ++) { LZAlertAction *action = self.actions[i]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.layer.cornerRadius = action.cornerRadius; btn.clipsToBounds = YES; btn.backgroundColor = action.bgColor; [btn setTitleColor:action.color forState:UIControlStateNormal]; [btn setTitle:action.title forState:UIControlStateNormal]; btn.titleLabel.font = action.font; btn.tag = i; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [bgV addSubview:btn]; [btns addObject:btn]; } if (btns.count <= 2) { if (btns.count > 1) { [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:14.0f leadSpacing:14.0f tailSpacing:14.0f]; [btns mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(contentL.mas_bottom).offset(31.0f); make.bottom.offset(-20.0f); make.height.offset(48.0f); }]; }else if(btns.count == 1){ UIButton *btn = btns[0]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(42.0f); make.right.offset(-42.0f); make.top.equalTo(contentL.mas_bottom).offset(31.0f); make.bottom.offset(-20.0f); make.height.offset(48.0f); }]; } }else{ UIView *frontV = nil; for (NSInteger i = 0; i < btns.count; i++) { UIButton *btn = btns[i]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(36.0f); make.right.offset(-36.0f); make.height.offset(48.0f); if (frontV == nil) { make.top.equalTo(contentL.mas_bottom).offset(31.0f); }else{ make.top.equalTo(frontV.mas_bottom).offset(10.0f); } if (i == btns.count - 1) { make.bottom.offset(-20.0f); } }]; frontV = btn; } } } - (void)btnClick:(UIButton *)sender{ [self dismissViewControllerAnimated:YES completion:^{ LZAlertAction *action = self.actions[sender.tag]; if (action.handler != nil) { action.handler(action); } }]; } @end