QMUITips.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Tencent is pleased to support the open source community by making QMUI_iOS available.
  3. * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. //
  9. // QMUITips.m
  10. // qmui
  11. //
  12. // Created by QMUI Team on 15/12/25.
  13. //
  14. #import "QMUITips.h"
  15. #import "QMUICore.h"
  16. #import "QMUIToastContentView.h"
  17. #import "QMUIToastBackgroundView.h"
  18. #import "NSString+QMUI.h"
  19. const NSInteger QMUITipsAutomaticallyHideToastSeconds = -1;
  20. @interface QMUITips ()
  21. @property(nonatomic, strong) UIView *contentCustomView;
  22. @end
  23. @implementation QMUITips
  24. - (void)showLoading {
  25. [self showLoading:nil hideAfterDelay:0];
  26. }
  27. - (void)showLoadingHideAfterDelay:(NSTimeInterval)delay {
  28. [self showLoading:nil hideAfterDelay:delay];
  29. }
  30. - (void)showLoading:(NSString *)text {
  31. [self showLoading:text hideAfterDelay:0];
  32. }
  33. - (void)showLoading:(NSString *)text hideAfterDelay:(NSTimeInterval)delay {
  34. [self showLoading:text detailText:nil hideAfterDelay:delay];
  35. }
  36. - (void)showLoading:(NSString *)text detailText:(NSString *)detailText {
  37. [self showLoading:text detailText:detailText hideAfterDelay:0];
  38. }
  39. - (void)showLoading:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  40. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  41. [indicator startAnimating];
  42. self.contentCustomView = indicator;
  43. [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
  44. }
  45. - (void)showWithText:(NSString *)text {
  46. [self showWithText:text detailText:nil hideAfterDelay:0];
  47. }
  48. - (void)showWithText:(NSString *)text hideAfterDelay:(NSTimeInterval)delay {
  49. [self showWithText:text detailText:nil hideAfterDelay:delay];
  50. }
  51. - (void)showWithText:(NSString *)text detailText:(NSString *)detailText {
  52. [self showWithText:text detailText:detailText hideAfterDelay:0];
  53. }
  54. - (void)showWithText:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  55. self.contentCustomView = nil;
  56. [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
  57. }
  58. - (void)showSucceed:(NSString *)text {
  59. [self showSucceed:text detailText:nil hideAfterDelay:0];
  60. }
  61. - (void)showSucceed:(NSString *)text hideAfterDelay:(NSTimeInterval)delay {
  62. [self showSucceed:text detailText:nil hideAfterDelay:delay];
  63. }
  64. - (void)showSucceed:(NSString *)text detailText:(NSString *)detailText {
  65. [self showSucceed:text detailText:detailText hideAfterDelay:0];
  66. }
  67. - (void)showSucceed:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  68. self.contentCustomView = [[UIImageView alloc] initWithImage:[[QMUIHelper imageWithName:@"QMUI_tips_done"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
  69. [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
  70. }
  71. - (void)showError:(NSString *)text {
  72. [self showError:text detailText:nil hideAfterDelay:0];
  73. }
  74. - (void)showError:(NSString *)text hideAfterDelay:(NSTimeInterval)delay {
  75. [self showError:text detailText:nil hideAfterDelay:delay];
  76. }
  77. - (void)showError:(NSString *)text detailText:(NSString *)detailText {
  78. [self showError:text detailText:detailText hideAfterDelay:0];
  79. }
  80. - (void)showError:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  81. self.contentCustomView = [[UIImageView alloc] initWithImage:[[QMUIHelper imageWithName:@"QMUI_tips_error"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
  82. [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
  83. }
  84. - (void)showInfo:(NSString *)text {
  85. [self showInfo:text detailText:nil hideAfterDelay:0];
  86. }
  87. - (void)showInfo:(NSString *)text hideAfterDelay:(NSTimeInterval)delay {
  88. [self showInfo:text detailText:nil hideAfterDelay:delay];
  89. }
  90. - (void)showInfo:(NSString *)text detailText:(NSString *)detailText {
  91. [self showInfo:text detailText:detailText hideAfterDelay:0];
  92. }
  93. - (void)showInfo:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  94. self.contentCustomView = [[UIImageView alloc] initWithImage:[[QMUIHelper imageWithName:@"QMUI_tips_info"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
  95. [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
  96. }
  97. - (void)showTipWithText:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
  98. QMUIToastContentView *contentView = (QMUIToastContentView *)self.contentView;
  99. contentView.customView = self.contentCustomView;
  100. contentView.textLabelText = text ?: @"";
  101. contentView.detailTextLabelText = detailText ?: @"";
  102. [self showAnimated:YES];
  103. if (delay == QMUITipsAutomaticallyHideToastSeconds) {
  104. [self hideAnimated:YES afterDelay:[QMUITips smartDelaySecondsForTipsText:text]];
  105. } else if (delay > 0) {
  106. [self hideAnimated:YES afterDelay:delay];
  107. }
  108. [self postAccessibilityAnnouncement:text detailText:detailText];
  109. }
  110. - (void)postAccessibilityAnnouncement:(NSString *)text detailText:(NSString *)detailText {
  111. NSString *announcementString = nil;
  112. if (text) {
  113. announcementString = text;
  114. }
  115. if (detailText) {
  116. announcementString = announcementString ? [text stringByAppendingFormat:@", %@", detailText] : detailText;
  117. }
  118. if (announcementString) {
  119. // 发送一个让VoiceOver播报的Announcement,帮助视障用户获取toast内的信息,但是这个播报会被即时打断而不生效,所以在这里延时1秒发送此通知。
  120. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  121. UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, announcementString);
  122. });
  123. }
  124. }
  125. + (NSTimeInterval)smartDelaySecondsForTipsText:(NSString *)text {
  126. NSUInteger length = text.qmui_lengthWhenCountingNonASCIICharacterAsTwo;
  127. if (length <= 20) {
  128. return 1.5;
  129. } else if (length <= 40) {
  130. return 2.0;
  131. } else if (length <= 50) {
  132. return 2.5;
  133. } else {
  134. return 3.0;
  135. }
  136. }
  137. + (QMUITips *)showLoadingInView:(UIView *)view {
  138. return [self showLoading:nil detailText:nil inView:view hideAfterDelay:0];
  139. }
  140. + (QMUITips *)showLoading:(NSString *)text inView:(UIView *)view {
  141. return [self showLoading:text detailText:nil inView:view hideAfterDelay:0];
  142. }
  143. + (QMUITips *)showLoadingInView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  144. return [self showLoading:nil detailText:nil inView:view hideAfterDelay:delay];
  145. }
  146. + (QMUITips *)showLoading:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  147. return [self showLoading:text detailText:nil inView:view hideAfterDelay:delay];
  148. }
  149. + (QMUITips *)showLoading:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view {
  150. return [self showLoading:text detailText:detailText inView:view hideAfterDelay:0];
  151. }
  152. + (QMUITips *)showLoading:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  153. QMUITips *tips = [self createTipsToView:view];
  154. [tips showLoading:text detailText:detailText hideAfterDelay:delay];
  155. return tips;
  156. }
  157. + (QMUITips *)showWithText:(nullable NSString *)text {
  158. return [self showWithText:text detailText:nil inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  159. }
  160. + (QMUITips *)showWithText:(nullable NSString *)text detailText:(nullable NSString *)detailText {
  161. return [self showWithText:text detailText:detailText inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  162. }
  163. + (QMUITips *)showWithText:(NSString *)text inView:(UIView *)view {
  164. return [self showWithText:text detailText:nil inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  165. }
  166. + (QMUITips *)showWithText:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  167. return [self showWithText:text detailText:nil inView:view hideAfterDelay:delay];
  168. }
  169. + (QMUITips *)showWithText:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view {
  170. return [self showWithText:text detailText:detailText inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  171. }
  172. + (QMUITips *)showWithText:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  173. QMUITips *tips = [self createTipsToView:view];
  174. [tips showWithText:text detailText:detailText hideAfterDelay:delay];
  175. return tips;
  176. }
  177. + (QMUITips *)showSucceed:(nullable NSString *)text {
  178. return [self showSucceed:text detailText:nil inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  179. }
  180. + (QMUITips *)showSucceed:(nullable NSString *)text detailText:(nullable NSString *)detailText {
  181. return [self showSucceed:text detailText:detailText inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  182. }
  183. + (QMUITips *)showSucceed:(NSString *)text inView:(UIView *)view {
  184. return [self showSucceed:text detailText:nil inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  185. }
  186. + (QMUITips *)showSucceed:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  187. return [self showSucceed:text detailText:nil inView:view hideAfterDelay:delay];
  188. }
  189. + (QMUITips *)showSucceed:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view {
  190. return [self showSucceed:text detailText:detailText inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  191. }
  192. + (QMUITips *)showSucceed:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  193. QMUITips *tips = [self createTipsToView:view];
  194. [tips showSucceed:text detailText:detailText hideAfterDelay:delay];
  195. return tips;
  196. }
  197. + (QMUITips *)showError:(nullable NSString *)text {
  198. return [self showError:text detailText:nil inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  199. }
  200. + (QMUITips *)showError:(nullable NSString *)text detailText:(nullable NSString *)detailText {
  201. return [self showError:text detailText:detailText inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  202. }
  203. + (QMUITips *)showError:(NSString *)text inView:(UIView *)view {
  204. return [self showError:text detailText:nil inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  205. }
  206. + (QMUITips *)showError:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  207. return [self showError:text detailText:nil inView:view hideAfterDelay:delay];
  208. }
  209. + (QMUITips *)showError:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view {
  210. return [self showError:text detailText:detailText inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  211. }
  212. + (QMUITips *)showError:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  213. QMUITips *tips = [self createTipsToView:view];
  214. [tips showError:text detailText:detailText hideAfterDelay:delay];
  215. return tips;
  216. }
  217. + (QMUITips *)showInfo:(nullable NSString *)text {
  218. return [self showInfo:text detailText:nil inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  219. }
  220. + (QMUITips *)showInfo:(nullable NSString *)text detailText:(nullable NSString *)detailText {
  221. return [self showInfo:text detailText:detailText inView:DefaultTipsParentView hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  222. }
  223. + (QMUITips *)showInfo:(NSString *)text inView:(UIView *)view {
  224. return [self showInfo:text detailText:nil inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  225. }
  226. + (QMUITips *)showInfo:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  227. return [self showInfo:text detailText:nil inView:view hideAfterDelay:delay];
  228. }
  229. + (QMUITips *)showInfo:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view {
  230. return [self showInfo:text detailText:detailText inView:view hideAfterDelay:QMUITipsAutomaticallyHideToastSeconds];
  231. }
  232. + (QMUITips *)showInfo:(NSString *)text detailText:(NSString *)detailText inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay {
  233. QMUITips *tips = [self createTipsToView:view];
  234. [tips showInfo:text detailText:detailText hideAfterDelay:delay];
  235. return tips;
  236. }
  237. + (QMUITips *)createTipsToView:(UIView *)view {
  238. QMUITips *tips = [[QMUITips alloc] initWithView:view];
  239. [view addSubview:tips];
  240. tips.removeFromSuperViewWhenHide = YES;
  241. return tips;
  242. }
  243. + (void)hideAllTipsInView:(UIView *)view {
  244. [self hideAllToastInView:view animated:NO];
  245. }
  246. + (void)hideAllTips {
  247. [self hideAllToastInView:nil animated:NO];
  248. }
  249. @end