TheAlertCtrl.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // TheAlertCtrl.m
  3. // FornoWorld
  4. //
  5. // Created by RD on 2021/12/10.
  6. //
  7. #import "TheAlertCtrl.h"
  8. #import "SPAlertController.h"
  9. @interface TheAlertCtrl ()<UIPickerViewDataSource,UIPickerViewDelegate>
  10. @property (nonatomic, weak) UIAlertController *ac;
  11. @property (nonatomic, strong) NSMutableArray *timerData;
  12. @end
  13. @implementation TheAlertCtrl
  14. + (instancetype)rds_alertWithTitle:(NSString *)title message:(NSString *)message{
  15. TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
  16. UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
  17. message:message
  18. preferredStyle:UIAlertControllerStyleAlert];
  19. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  20. }];
  21. [okAction setValue:kDarkTextColor forKey:@"_titleTextColor"];
  22. [ac addAction:okAction];
  23. ctrl.ac = ac;
  24. // 修改标题字体颜色
  25. // if (!kNULLString(title)) {
  26. // NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
  27. // [titleAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, title.length)];
  28. // [titleAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_SemiBold size:18] range:NSMakeRange(0, title.length)];
  29. // [ac setValue:titleAtt forKey:@"attributedTitle"];
  30. // }
  31. // 修改msg字体颜色
  32. // if (!kNULLString(message)) {
  33. // NSMutableAttributedString *msgAtt = [[NSMutableAttributedString alloc] initWithString:message];
  34. // [msgAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, message.length)];
  35. // [msgAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_Regular size:16] range:NSMakeRange(0, message.length)];
  36. //
  37. // NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  38. // //paragraph.lineSpacing = 5;// 字体的行间距
  39. // //paragraph.firstLineHeadIndent = 25.0f;//首行缩进
  40. // paragraph.alignment = NSTextAlignmentLeft;
  41. //
  42. // [msgAtt addAttribute:NSParagraphStyleAttributeName
  43. // value:paragraph
  44. // range:NSMakeRange(0, message.length)];
  45. //
  46. // [ac setValue:msgAtt forKey:@"attributedMessage"];
  47. // }
  48. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  49. NSAssert(window, @"The window is empty");
  50. // 更新UI要在主线程执行
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. [window.rootViewController presentViewController:ac animated:YES completion:nil];
  53. });
  54. return ctrl;
  55. }
  56. + (instancetype)rds_alertWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)calcel sure:(NSString *)sure cancelColor:(UIColor *)cancelColor sureColor:(UIColor *)sureColor cancelAction:(void (^)(void))cancelActionBlock sureAction:(void (^)(void))sureActionBlock{
  57. // if(!kNULLString(message)){
  58. // message = [NSString stringWithFormat:@"\n%@",message];
  59. // }
  60. TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
  61. UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
  62. message:message
  63. preferredStyle:UIAlertControllerStyleAlert];
  64. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:calcel style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  65. if(cancelActionBlock) {
  66. cancelActionBlock();
  67. }
  68. }];
  69. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  70. if (sureActionBlock) {
  71. sureActionBlock();
  72. }
  73. }];
  74. // 修改按钮字体颜色
  75. if(cancelColor){
  76. [cancelAction setValue:cancelColor forKey:@"_titleTextColor"];
  77. }
  78. if(sureColor){
  79. [sureAction setValue:sureColor forKey:@"_titleTextColor"];
  80. }
  81. [ac addAction:cancelAction];
  82. [ac addAction:sureAction];
  83. ctrl.ac = ac;
  84. // ac.view.backgroundColor = UIColor.whiteColor;
  85. // 修改标题字体颜色
  86. // NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
  87. // [titleAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, title.length)];
  88. // [titleAtt addAttribute:NSFontAttributeName value:[UIFont rds_titiFontStyle:FontStyle_SemiBold size:18] range:NSMakeRange(0, title.length)];
  89. // [ac setValue:titleAtt forKey:@"attributedTitle"];
  90. // 修改msg字体颜色
  91. // NSMutableAttributedString *msgAtt = [[NSMutableAttributedString alloc] initWithString:message];
  92. // [msgAtt addAttribute:NSForegroundColorAttributeName value:RDSGrayColor range:NSMakeRange(0, message.length)];
  93. // [msgAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, message.length)];
  94. // [ac setValue:msgAtt forKey:@"attributedMessage"];
  95. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  96. NSAssert(window, @"The window is empty");
  97. // 更新UI要在主线程执行
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. [window.rootViewController presentViewController:ac animated:YES completion:nil];
  100. });
  101. return ctrl;
  102. }
  103. + (instancetype)rds_timerAlertSureAction:(void (^)(void))sureActionBlock{
  104. TheAlertCtrl *ctrl = [[TheAlertCtrl alloc] init];
  105. SPAlertController *alertController = [SPAlertController alertControllerWithTitle:@"定时关闭" message:@"" preferredStyle:SPAlertControllerStyleAlert animationType:SPAlertAnimationTypeDefault];
  106. UIPickerView *timerPick = [[UIPickerView alloc] init];
  107. timerPick.delegate = ctrl;
  108. timerPick.dataSource = ctrl;
  109. NSMutableArray *data = [NSMutableArray array];
  110. for (CGFloat i = 0.5; i<24; i+=0.5) {
  111. [data addObject:@(i)];
  112. }
  113. ctrl.timerData = data;
  114. [timerPick selectRow:0 inComponent:0 animated:NO];
  115. // 插入一个view
  116. [alertController insertComponentView:timerPick];
  117. SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"取消" style:SPAlertActionStyleDefault handler:nil];
  118. SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"确定" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
  119. }];
  120. [alertController addAction:action1];
  121. [alertController addAction:action2];
  122. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  123. NSAssert(window, @"The window is empty");
  124. // 更新UI要在主线程执行
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. [timerPick reloadAllComponents];
  127. [window.rootViewController presentViewController:alertController animated:YES completion:nil];
  128. });
  129. return ctrl;
  130. }
  131. #pragma mark pickerView delegate
  132. //滚动选中的行
  133. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  134. }
  135. //每行显示的内容
  136. -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  137. NSString *str = [NSString stringWithFormat:@"%@",_timerData[row]];
  138. return str;
  139. }
  140. //列宽
  141. //-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
  142. //
  143. // return 100;
  144. //}
  145. #pragma mark pickerView dataSource
  146. // 多少组
  147. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  148. return 1;
  149. }
  150. // 行
  151. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  152. return _timerData.count;
  153. }
  154. - (void)rds_show {
  155. RDS_WEAKSELF(weakSelf)
  156. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  157. NSAssert(window, @"The window is empty");
  158. // 更新UI要在主线程执行
  159. dispatch_async(dispatch_get_main_queue(), ^{
  160. [window.rootViewController presentViewController:weakSelf.ac animated:YES completion:nil];
  161. });
  162. }
  163. - (void)rds_dismiss {
  164. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  165. NSAssert(window, @"The window is empty");
  166. // 更新UI要在主线程执行
  167. dispatch_async(dispatch_get_main_queue(), ^{
  168. [window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  169. });
  170. }
  171. @end