LZAlertWindow.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // LZAlertWindow.m
  3. // TIANYAN
  4. //
  5. // Created by CY on 2021/4/23.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "LZAlertWindow.h"
  9. #import "UIViewController+TFPresent.h"
  10. @implementation LZAlertAction
  11. + (instancetype)actionWithTitle:(NSString *)title handler:(void (^)(LZAlertAction *action))handler{
  12. return [[self alloc] initWithTitle:title handler:handler];
  13. }
  14. - (instancetype)initWithTitle:(NSString *)title handler:(void (^)(LZAlertAction *action))handler{
  15. if (self = [super init]) {
  16. self.title = title;
  17. self.handler = handler;
  18. [self youpaifinitialization];
  19. }
  20. return self;
  21. }
  22. - (void)youpaifinitialization{
  23. self.bgColor = [UIColor whiteColor];
  24. self.cornerRadius = 24.0f;
  25. self.color = [UIColor blackColor];
  26. self.font = LCFont(17.0f);
  27. }
  28. @end
  29. @interface LZAlertWindow ()
  30. @property (nonatomic, copy) void (^contentStyleBlock)(UILabel *contentL);
  31. @end
  32. @implementation LZAlertWindow
  33. + (instancetype)alertWithTitle:(NSString *)title content:(NSString *)content action:(NSArray<LZAlertAction *> *)actions{
  34. return [[LZAlertWindow alloc] initWithTitle:title content:content action:actions];
  35. }
  36. - (instancetype)initWithTitle:(NSString *)title content:(NSString *)content action:(NSArray<LZAlertAction *> *)actions{
  37. return [self initWithTitle:title content:content action:actions contentStyleBlock:nil];
  38. }
  39. + (instancetype)alertWithTitle:(NSString *)title content:(NSString *)content action:(NSArray<LZAlertAction *> *)actions contentStyleBlock:(void (^)(UILabel *))contentStyleBlock{
  40. return [[LZAlertWindow alloc] initWithTitle:title content:content action:actions contentStyleBlock:contentStyleBlock];
  41. }
  42. - (instancetype)initWithTitle:(NSString *)title content:(NSString *)content action:(NSArray<LZAlertAction *> *)actions contentStyleBlock:(void (^)(UILabel *))contentStyleBlock{
  43. if (self = [super init]) {
  44. self.zTitle = title;
  45. self.content = content;
  46. self.actions = actions;
  47. self.contentStyleBlock = contentStyleBlock;
  48. [self youpaifinitialization];
  49. }
  50. return self;
  51. }
  52. - (void)youpaifinitialization{
  53. self.bgColor = self.bgColor;
  54. self.cornerRadius = 10.0f;
  55. self.titleColor = LZ273145Color;
  56. self.titleFont = LCBoldFont(19.0f);
  57. self.contentColor = LZ273145Color;
  58. self.contentFont = LCFont(15.0f);
  59. self.contentHorizontalSpace = 14.0f;
  60. self.contentTextAlignment = NSTextAlignmentLeft;
  61. }
  62. - (void)viewDidLoad {
  63. [super viewDidLoad];
  64. self.baseView.hidden = YES;
  65. [self youpaifsetupUI];
  66. }
  67. - (void)youpaifsetupUI{
  68. UIView *bgV = [[UIView alloc] init];
  69. bgV.backgroundColor = [UIColor whiteColor];
  70. bgV.layer.cornerRadius = self.cornerRadius;
  71. bgV.clipsToBounds = YES;
  72. [self.view addSubview:bgV];
  73. [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.left.offset(32.0f);
  75. make.right.offset(-32.0f);
  76. make.centerY.equalTo(self.view);
  77. }];
  78. UILabel *titleL = nil;
  79. if (self.zTitle.length != 0) {
  80. titleL = [[UILabel alloc] init];
  81. titleL.font = self.titleFont;
  82. titleL.textColor = self.titleColor;
  83. titleL.textAlignment = NSTextAlignmentCenter;
  84. titleL.text = self.zTitle;
  85. [bgV addSubview:titleL];
  86. [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.top.offset(31.0f);
  88. make.left.offset(14.0f);
  89. make.right.offset(-14.0f);
  90. }];
  91. }
  92. UILabel *contentL = [[UILabel alloc] init];
  93. contentL.font = self.contentFont;
  94. contentL.textColor = self.contentColor;
  95. contentL.numberOfLines = 0;
  96. NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:self.content];
  97. NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  98. paragraphStyle1.alignment=NSTextAlignmentJustified;
  99. NSDictionary * dic =@{
  100. NSParagraphStyleAttributeName:paragraphStyle1,
  101. NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone],
  102. };
  103. [attributedString1 setAttributes:dic range:NSMakeRange(0, attributedString1.length)];
  104. [contentL setAttributedText:attributedString1];
  105. contentL.textAlignment = self.contentTextAlignment;
  106. [bgV addSubview:contentL];
  107. if (self.contentStyleBlock != nil) {
  108. self.contentStyleBlock(contentL);
  109. }
  110. if (titleL == nil) {
  111. [contentL mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.top.offset(45.0f);
  113. make.left.offset(40.0f);
  114. make.right.offset(-40.0f);
  115. }];
  116. }else{
  117. [contentL mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.top.equalTo(titleL.mas_bottom).offset(20.0f);
  119. make.left.offset(self.contentHorizontalSpace);
  120. make.right.offset(-self.contentHorizontalSpace);
  121. }];
  122. }
  123. NSMutableArray *btns = [NSMutableArray array];
  124. for (NSInteger i = 0; i < self.actions.count; i ++) {
  125. LZAlertAction *action = self.actions[i];
  126. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  127. btn.layer.cornerRadius = action.cornerRadius;
  128. btn.clipsToBounds = YES;
  129. btn.backgroundColor = action.bgColor;
  130. [btn setTitleColor:action.color forState:UIControlStateNormal];
  131. [btn setTitle:action.title forState:UIControlStateNormal];
  132. btn.titleLabel.font = action.font;
  133. btn.tag = i;
  134. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  135. [bgV addSubview:btn];
  136. [btns addObject:btn];
  137. }
  138. if (btns.count <= 2) {
  139. if (btns.count > 1) {
  140. [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:14.0f leadSpacing:14.0f tailSpacing:14.0f];
  141. [btns mas_makeConstraints:^(MASConstraintMaker *make) {
  142. make.top.equalTo(contentL.mas_bottom).offset(31.0f);
  143. make.bottom.offset(-20.0f);
  144. make.height.offset(48.0f);
  145. }];
  146. }else if(btns.count == 1){
  147. UIButton *btn = btns[0];
  148. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.left.offset(42.0f);
  150. make.right.offset(-42.0f);
  151. make.top.equalTo(contentL.mas_bottom).offset(31.0f);
  152. make.bottom.offset(-20.0f);
  153. make.height.offset(48.0f);
  154. }];
  155. }
  156. }else{
  157. UIView *frontV = nil;
  158. for (NSInteger i = 0; i < btns.count; i++) {
  159. UIButton *btn = btns[i];
  160. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.left.offset(36.0f);
  162. make.right.offset(-36.0f);
  163. make.height.offset(48.0f);
  164. if (frontV == nil) {
  165. make.top.equalTo(contentL.mas_bottom).offset(31.0f);
  166. }else{
  167. make.top.equalTo(frontV.mas_bottom).offset(10.0f);
  168. }
  169. if (i == btns.count - 1) {
  170. make.bottom.offset(-20.0f);
  171. }
  172. }];
  173. frontV = btn;
  174. }
  175. }
  176. }
  177. - (void)btnClick:(UIButton *)sender{
  178. [self dismissViewControllerAnimated:YES completion:^{
  179. LZAlertAction *action = self.actions[sender.tag];
  180. if (action.handler != nil) {
  181. action.handler(action);
  182. }
  183. }];
  184. }
  185. @end