MBProgressHUD+AppCategory.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // MBProgressHUD+AppCategory.m
  3. // Talk
  4. //
  5. // Created by qitewei on 2025/4/18.
  6. //
  7. #import "MBProgressHUD+AppCategory.h"
  8. const NSInteger AppHUDAutomaticallyHideSeconds = -1;
  9. @implementation MBProgressHUD (AppCategory)
  10. #pragma mark - Loading
  11. + (MBProgressHUD *)showLoadingInView:(UIView *)view {
  12. return [self showLoadingWithText:nil detailText:nil inView:view hideAfterDelay:0];
  13. }
  14. + (MBProgressHUD *)showLoadingInView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  15. return [self showLoadingWithText:nil detailText:nil inView:view hideAfterDelay:delay];
  16. }
  17. + (MBProgressHUD *)showLoadingWithText:(NSString * _Nullable)text inView:(UIView *)view {
  18. return [self showLoadingWithText:text detailText:nil inView:view hideAfterDelay:0];
  19. }
  20. + (MBProgressHUD *)showLoadingWithText:(NSString * _Nullable)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  21. return [self showLoadingWithText:text detailText:nil inView:view hideAfterDelay:delay];
  22. }
  23. + (MBProgressHUD *)showLoadingWithText:(NSString * _Nullable)text detailText:(NSString * _Nullable)detailText inView:(UIView *)view {
  24. return [self showLoadingWithText:text detailText:detailText inView:view hideAfterDelay:0];
  25. }
  26. + (MBProgressHUD *)showLoadingWithText:(NSString * _Nullable)text detailText:(NSString * _Nullable)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  27. MBProgressHUD *hud = [self MBProgressHUDToView:view];
  28. hud.mode = MBProgressHUDModeIndeterminate;
  29. hud.label.text = text;
  30. hud.detailsLabel.text = detailText;
  31. [hud showAnimated:YES];
  32. if (delay == AppHUDAutomaticallyHideSeconds) {
  33. CGFloat delay = [self secondsToHideWithText:text];
  34. [hud hideAnimated:YES afterDelay:delay];
  35. } else if (delay > 0) {
  36. [hud hideAnimated:YES afterDelay:delay];
  37. }
  38. return hud;
  39. }
  40. #pragma mark - Text
  41. + (MBProgressHUD *)showWithText:(NSString *)text {
  42. return [self showWithText:text detailText:nil inView:WINDOW hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  43. }
  44. + (MBProgressHUD *)showWithText:(NSString *)text inView:(UIView *)view {
  45. return [self showWithText:text detailText:nil inView:view hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  46. }
  47. + (MBProgressHUD *)showWithText:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  48. return [self showWithText:text detailText:nil inView:view hideAfterDelay:delay];
  49. }
  50. + (MBProgressHUD *)showWithText:(NSString *)text detailText:(NSString * _Nullable)detailText {
  51. return [self showWithText:text detailText:detailText inView:WINDOW hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  52. }
  53. + (MBProgressHUD *)showWithText:(NSString *)text detailText:(NSString * _Nullable)detailText inView:(UIView *)view {
  54. return [self showWithText:text detailText:detailText inView:view hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  55. }
  56. + (MBProgressHUD *)showWithText:(NSString *)text detailText:(NSString * _Nullable)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  57. if (text.length == 0) {
  58. return nil;
  59. }
  60. MBProgressHUD *hud = [self MBProgressHUDToView:view];
  61. hud.mode = MBProgressHUDModeText;
  62. hud.label.text = text;
  63. hud.detailsLabel.text = detailText;
  64. hud.userInteractionEnabled = NO;
  65. [hud showAnimated:YES];
  66. if (delay == AppHUDAutomaticallyHideSeconds) {
  67. CGFloat delay = [self secondsToHideWithText:text];
  68. [hud hideAnimated:YES afterDelay:delay];
  69. } else if (delay > 0) {
  70. [hud hideAnimated:YES afterDelay:delay];
  71. }
  72. return hud;
  73. // return [self showInfo:text detailText:detailText inView:view hideAfterDelay:delay];
  74. }
  75. #pragma mark - 带图标的提示
  76. + (MBProgressHUD *)showWithText:(NSString * _Nullable)text icon:(NSString * _Nullable)icon {
  77. return [self showWithText:text icon:icon inView:WINDOW hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  78. }
  79. + (MBProgressHUD *)showWithText:(NSString * _Nullable)text icon:(NSString * _Nullable)icon inView:(UIView *)view {
  80. return [self showWithText:text icon:icon inView:view hideAfterDelay:AppHUDAutomaticallyHideSeconds];
  81. }
  82. + (MBProgressHUD *)showWithText:(NSString * _Nullable)text icon:(NSString * _Nullable)icon inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  83. MBProgressHUD *hud = [self MBProgressHUDToView:view];
  84. hud.mode = MBProgressHUDModeCustomView;
  85. hud.customView = [self createCustomViewWithImageName:icon
  86. labelText:text];
  87. [hud showAnimated:YES];
  88. if (delay == AppHUDAutomaticallyHideSeconds) {
  89. CGFloat delay = [self secondsToHideWithText:text];
  90. [hud hideAnimated:YES afterDelay:delay];
  91. } else if (delay > 0) {
  92. [hud hideAnimated:YES afterDelay:delay];
  93. }
  94. return hud;
  95. }
  96. + (void)hideAllTipsInView:(UIView *)view {
  97. [MBProgressHUD hideHUDForView:view animated:NO];
  98. }
  99. + (void)hideAllTips {
  100. [self hideAllTipsInView:WINDOW];
  101. }
  102. + (MBProgressHUD *)MBProgressHUDToView:(UIView *)view {
  103. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view ?: WINDOW];
  104. hud.removeFromSuperViewOnHide = YES;
  105. // hud.contentColor = UIColor.blackColor;
  106. hud.margin = 10;
  107. hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
  108. // hud.bezelView.color = [UIColor.blackColor colorWithAlphaComponent:0.2f];
  109. hud.bezelView.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
  110. if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  111. return [UIColor colorWithWhite:1.0 alpha:0.8];
  112. } else {
  113. return [UIColor colorWithWhite:0.0 alpha:0.8];
  114. }
  115. }];
  116. // 指示器和文字颜色:浅色模式下为白色,深色模式下为黑色
  117. hud.contentColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
  118. return traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? [UIColor whiteColor] : [UIColor blackColor];
  119. }];
  120. hud.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  121. hud.label.numberOfLines = 0;
  122. hud.label.preferredMaxLayoutWidth = SCREEN_WIDTH - 140;
  123. hud.detailsLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 140;
  124. [view addSubview:hud];
  125. return hud;
  126. }
  127. + (NSTimeInterval)secondsToHideWithText:(NSString *)text {
  128. NSUInteger length = 0;
  129. for (NSUInteger i = 0, l = text.length; i < l; i++) {
  130. unichar character = [text characterAtIndex:i];
  131. if (isascii(character)) {
  132. length += 1;
  133. } else {
  134. length += 2;
  135. }
  136. }
  137. if (length < 20) {
  138. return 2.0;
  139. } else if (length < 40) {
  140. return 2.5;
  141. } else if (length < 50) {
  142. return 3.0;
  143. } else {
  144. return 3.5;
  145. }
  146. }
  147. #pragma mark - 自定义View
  148. + (UIView *)createCustomViewWithImageName:(NSString *)imageName
  149. labelText:(NSString *)labelText {
  150. UIView *view = [[UIView alloc] init];
  151. UIImageView *icon = [[UIImageView alloc] initWithImage:kImageMake(imageName)];
  152. UILabel *label = [[UILabel alloc] init];
  153. label.font = SYSFONT(13);
  154. label.text = labelText;
  155. label.textColor = UIColor.whiteColor;
  156. label.numberOfLines = 0;
  157. [view addSubview:icon];
  158. [view addSubview:label];
  159. [icon mas_makeConstraints:^(MASConstraintMaker *make) {
  160. make.left.mas_equalTo(10);
  161. make.centerY.mas_equalTo(0);
  162. make.size.mas_equalTo(CGSizeMake(24, 24));
  163. }];
  164. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  165. make.left.equalTo(icon.mas_right).offset(8);
  166. make.right.mas_equalTo(-10);
  167. make.top.bottom.inset(3);
  168. }];
  169. return view;
  170. }
  171. @end