MBProgressHUD+NHAdd.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // MBProgressHUD+NHAdd.m
  3. // NHHUDExtendDemo
  4. //
  5. // Created by 肖雨 on 2017/8/18.
  6. // Copyright © 2017年 肖雨. All rights reserved.
  7. //
  8. #import "MBProgressHUD+NHAdd.h"
  9. #import "MBProgressHUD_NHExtend.h"
  10. #import "MBProgressHUD.h"
  11. #import <objc/message.h>
  12. CGFloat const delayTime = 1.2;
  13. #define kLoadImage(name) [UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", (name)]]
  14. @implementation MBProgressHUD (NHAdd)
  15. static char cancelationKey;
  16. NS_INLINE MBProgressHUD *createNew(UIView *view) {
  17. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  18. return [MBProgressHUD showHUDAddedTo:view animated:YES];
  19. }
  20. NS_INLINE MBProgressHUD *settHUD(UIView *view, NSString *title, BOOL autoHidden) {
  21. MBProgressHUD *hud = createNew(view);
  22. //文字
  23. hud.label.text = title;
  24. //支持多行
  25. hud.label.numberOfLines = 0;
  26. // 隐藏时候从父控件中移除
  27. hud.removeFromSuperViewOnHide = YES;
  28. //设置默认风格
  29. if (NHDefaultHudStyle == 1) {
  30. hud.hudContentStyle(NHHUDContentBlackStyle);
  31. } else if (NHDefaultHudStyle == 2) {
  32. hud.hudContentStyle(NHHUDContentCustomStyle);
  33. }
  34. if (autoHidden) {
  35. // x秒之后消失
  36. [hud hideAnimated:YES afterDelay:delayTime];
  37. }
  38. return hud;
  39. }
  40. + (MBProgressHUD *)showOnlyLoadToView:(UIView *)view {
  41. return settHUD(view, nil, NO);
  42. }
  43. + (void)showOnlyTextToView:(UIView *)view title:(NSString *)title {
  44. MBProgressHUD *hud = settHUD(view, title, YES);
  45. hud.mode = MBProgressHUDModeText;
  46. }
  47. + (void)showOnlyTextToView:(UIView *)view title:(NSString *)title detail:(NSString *)detail {
  48. MBProgressHUD *hud = settHUD(view, title, YES);
  49. hud.detailsLabel.text = detail;
  50. hud.mode = MBProgressHUDModeText;
  51. }
  52. + (void)showSuccess:(NSString *)success toView:(UIView *)view {
  53. MBProgressHUD *hud = settHUD(view, success, YES);
  54. hud.mode = MBProgressHUDModeCustomView;
  55. hud.customView = [[UIImageView alloc] initWithImage:kLoadImage(@"success.png")];
  56. }
  57. + (void)showError:(NSString *)error toView:(UIView *)view {
  58. MBProgressHUD *hud = settHUD(view, error, YES);
  59. hud.mode = MBProgressHUDModeCustomView;
  60. hud.customView = [[UIImageView alloc] initWithImage:kLoadImage(@"error.png")];
  61. }
  62. + (void)showTitleToView:(UIView *)view postion:(NHHUDPostion)postion title:(NSString *)title {
  63. MBProgressHUD *hud = settHUD(view, title, YES);
  64. hud.mode = MBProgressHUDModeText;
  65. hud.hudPostion(postion);
  66. }
  67. //纯标题 + 详情 + 自定背景风格 - 自动消失
  68. + (void)showDetailToView:(UIView *)view
  69. postion:(NHHUDPostion)postion
  70. title:(NSString *)title
  71. detail:(NSString *)detail {
  72. MBProgressHUD *hud = settHUD(view, title, YES);
  73. hud.detailsLabel.text = detail;
  74. hud.mode = MBProgressHUDModeText;
  75. hud.hudPostion(postion);
  76. }
  77. + (void)showTitleToView:(UIView *)view
  78. postion:(NHHUDPostion)postion
  79. contentStyle:(NHHUDContentStyle)contentStyle
  80. title:(NSString *)title {
  81. MBProgressHUD *hud = settHUD(view, title, YES);
  82. hud.mode = MBProgressHUDModeText;
  83. hud.hudContentStyle(contentStyle);
  84. hud.hudPostion(postion);
  85. }
  86. + (MBProgressHUD *)showLoadToView:(UIView *)view title:(NSString *)title {
  87. MBProgressHUD *hud = settHUD(view, title, NO);
  88. hud.mode = MBProgressHUDModeIndeterminate;
  89. return hud;
  90. }
  91. + (MBProgressHUD *)showLoadToView:(UIView *)view contentStyle:(NHHUDContentStyle)contentStyle title:(NSString *)title {
  92. MBProgressHUD *hud = settHUD(view, title, NO);
  93. hud.hudContentStyle(contentStyle);
  94. return hud;
  95. }
  96. + (void)showTitleToView:(UIView *)view
  97. contentStyle:(NHHUDContentStyle)contentStyle
  98. title:(NSString *)title
  99. afterDelay:(NSTimeInterval)delay {
  100. MBProgressHUD *hud = settHUD(view, title, NO);
  101. hud.mode = MBProgressHUDModeText;
  102. hud.hudContentStyle(contentStyle);
  103. [hud hideAnimated:YES afterDelay:delay];
  104. }
  105. + (MBProgressHUD *)showDownToView:(UIView *)view
  106. progressStyle:(NHHUDProgressStyle)progressStyle
  107. title:(NSString *)title
  108. progress:(NHCurrentHud)progress {
  109. MBProgressHUD *hud = settHUD(view, title, NO);
  110. if (progressStyle == NHHUDProgressDeterminateHorizontalBar) {
  111. hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  112. } else if (progressStyle == NHHUDProgressDeterminate) {
  113. hud.mode = MBProgressHUDModeDeterminate;
  114. } else if (progressStyle == NHHUDProgressAnnularDeterminate) {
  115. hud.mode = MBProgressHUDModeAnnularDeterminate;
  116. }
  117. if (progress) {
  118. progress(hud);
  119. }
  120. return hud;
  121. }
  122. + (MBProgressHUD *)showDownToView:(UIView *)view
  123. progressStyle:(NHHUDProgressStyle)progressStyle
  124. title:(NSString *)title
  125. cancelTitle:(NSString *)cancelTitle
  126. progress:(NHCurrentHud)progress
  127. cancelation:(NHCancelation)cancelation {
  128. MBProgressHUD *hud = settHUD(view, title, NO);
  129. if (progressStyle == NHHUDProgressDeterminateHorizontalBar) {
  130. hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  131. } else if (progressStyle == NHHUDProgressDeterminate) {
  132. hud.mode = MBProgressHUDModeDeterminate;
  133. } else if (progressStyle == NHHUDProgressAnnularDeterminate) {
  134. hud.mode = MBProgressHUDModeAnnularDeterminate;
  135. }
  136. [hud.button setTitle:cancelTitle ?: NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
  137. [hud.button addTarget:hud action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
  138. hud.cancelation = cancelation;
  139. if (progress) {
  140. progress(hud);
  141. }
  142. return hud;
  143. }
  144. + (void)showCustomView:(UIImage *)image toView:(UIView *)toView title:(NSString *)title {
  145. MBProgressHUD *hud = settHUD(toView, title, YES);
  146. // Set the custom view mode to show any view.
  147. hud.mode = MBProgressHUDModeCustomView;
  148. // Set an image view with a checkmark.
  149. hud.customView = [[UIImageView alloc] initWithImage:image];
  150. // Looks a bit nicer if we make it square.
  151. hud.square = YES;
  152. }
  153. + (MBProgressHUD *)showModelSwitchToView:(UIView *)toView
  154. title:(NSString *)title
  155. configHud:(NHCurrentHud)configHud {
  156. MBProgressHUD *hud = settHUD(toView, title, NO);
  157. // Will look best, if we set a minimum size.
  158. hud.minSize = CGSizeMake(150.f, 100.f);
  159. if (configHud) {
  160. configHud(hud);
  161. }
  162. return hud;
  163. }
  164. + (MBProgressHUD *)showDownNSProgressToView:(UIView *)view title:(NSString *)title {
  165. MBProgressHUD *hud = settHUD(view, title, NO);
  166. hud.mode = MBProgressHUDModeDeterminate;
  167. return hud;
  168. }
  169. + (MBProgressHUD *)showDownWithNSProgress:(NSProgress *)Progress
  170. toView:(UIView *)view title:(NSString *)title
  171. configHud:(NHCurrentHud)configHud {
  172. MBProgressHUD *hud = settHUD(view, title, NO);
  173. if (configHud) {
  174. configHud(hud);
  175. }
  176. return hud;
  177. }
  178. + (MBProgressHUD *)showLoadToView:(UIView *)view
  179. backgroundColor:(UIColor *)backgroundColor
  180. title:(NSString *)title {
  181. MBProgressHUD *hud = settHUD(view, title, NO);
  182. hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
  183. hud.backgroundView.backgroundColor = backgroundColor;
  184. return hud;
  185. }
  186. + (MBProgressHUD *)showLoadToView:(UIView *)view
  187. contentColor:(UIColor *)contentColor
  188. title:(NSString *)title {
  189. MBProgressHUD *hud = settHUD(view, title, NO);
  190. hud.contentColor = contentColor;
  191. return hud;
  192. }
  193. + (MBProgressHUD *)showLoadToView:(UIView *)view
  194. contentColor:(UIColor *)contentColor
  195. backgroundColor:(UIColor *)backgroundColor
  196. title:(NSString *)title {
  197. MBProgressHUD *hud = settHUD(view, title, NO);
  198. hud.contentColor = contentColor;
  199. hud.backgroundView.color = backgroundColor;
  200. return hud;
  201. }
  202. + (MBProgressHUD *)showLoadToView:(UIView *)view
  203. titleColor:(UIColor *)titleColor
  204. bezelViewColor:(UIColor *)bezelViewColor
  205. backgroundColor:(UIColor *)backgroundColor
  206. title:(NSString *)title {
  207. MBProgressHUD *hud = settHUD(view, title, NO);
  208. hud.label.textColor = titleColor;
  209. hud.bezelView.backgroundColor = bezelViewColor;
  210. hud.backgroundView.color = backgroundColor;
  211. return hud;
  212. }
  213. + (MBProgressHUD *)createHudToView:(UIView *)view title:(NSString *)title configHud:(NHCurrentHud)configHud {
  214. MBProgressHUD *hud = settHUD(view, title, YES);
  215. if (configHud) {
  216. configHud(hud);
  217. }
  218. return hud;
  219. }
  220. + (void)hideHUDForView:(UIView *)view {
  221. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  222. [self hideHUDForView:view animated:YES];
  223. }
  224. + (void)hideHUD {
  225. [self hideHUDForView:nil];
  226. }
  227. #pragma mark -- sett // gett
  228. - (void)didClickCancelButton {
  229. if (self.cancelation) {
  230. self.cancelation(self);
  231. }
  232. }
  233. - (void)setCancelation:(NHCancelation)cancelation {
  234. objc_setAssociatedObject(self, &cancelationKey, cancelation, OBJC_ASSOCIATION_COPY);
  235. }
  236. - (NHCancelation)cancelation {
  237. return objc_getAssociatedObject(self, &cancelationKey);
  238. }
  239. - (MBProgressHUD *(^)(UIColor *))hudBackgroundColor {
  240. return ^(UIColor *hudBackgroundColor) {
  241. self.backgroundView.color = hudBackgroundColor;
  242. return self;
  243. };
  244. }
  245. - (MBProgressHUD *(^)(UIView *))toView {
  246. return ^(UIView *view){
  247. return self;
  248. };
  249. }
  250. - (MBProgressHUD *(^)(NSString *))title {
  251. return ^(NSString *title){
  252. self.label.text = title;
  253. return self;
  254. };
  255. }
  256. - (MBProgressHUD *(^)(NSString *))details {
  257. return ^(NSString *details){
  258. self.detailsLabel.text = details;
  259. return self;
  260. };
  261. }
  262. - (MBProgressHUD *(^)(NSString *))customIcon {
  263. return ^(NSString *customIcon) {
  264. self.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:customIcon]];
  265. return self;
  266. };
  267. }
  268. - (MBProgressHUD *(^)(UIColor *))titleColor {
  269. return ^(UIColor *titleColor){
  270. self.label.textColor = titleColor;
  271. self.detailsLabel.textColor = titleColor;
  272. return self;
  273. };
  274. }
  275. - (MBProgressHUD *(^)(UIColor *))progressColor {
  276. return ^(UIColor *progressColor) {
  277. UIColor *titleColor = self.label.textColor;
  278. self.contentColor = progressColor;
  279. self.label.textColor = titleColor;
  280. self.detailsLabel.textColor = titleColor;
  281. return self;
  282. };
  283. }
  284. - (MBProgressHUD *(^)(UIColor *))allContentColors {
  285. return ^(UIColor *allContentColors) {
  286. self.contentColor = allContentColors;
  287. return self;
  288. };
  289. }
  290. - (MBProgressHUD *(^)(UIColor *))bezelBackgroundColor {
  291. return ^(UIColor *bezelViewColor){
  292. self.bezelView.backgroundColor = bezelViewColor;
  293. return self;
  294. };
  295. }
  296. - (MBProgressHUD *(^)(NHHUDContentStyle))hudContentStyle {
  297. return ^(NHHUDContentStyle hudContentStyle){
  298. if (hudContentStyle == NHHUDContentBlackStyle) {
  299. self.contentColor = [UIColor whiteColor];
  300. self.bezelView.backgroundColor = [UIColor blackColor];
  301. self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
  302. } else if (hudContentStyle == NHHUDContentCustomStyle) {
  303. self.contentColor = NHCustomHudStyleContentColor;
  304. self.bezelView.backgroundColor = NHCustomHudStyleBackgrandColor;
  305. self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
  306. } else if (hudContentStyle == NHHUDContentDefaultStyle){
  307. self.contentColor = [UIColor blackColor];
  308. self.bezelView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
  309. self.bezelView.style = MBProgressHUDBackgroundStyleBlur;
  310. }
  311. return self;
  312. };
  313. }
  314. - (MBProgressHUD *(^)(NHHUDPostion))hudPostion {
  315. return ^(NHHUDPostion hudPostion){
  316. if (hudPostion == NHHUDPostionTop) {
  317. self.offset = CGPointMake(0.f, -MBProgressMaxOffset);
  318. } else if (hudPostion == NHHUDPostionCenten) {
  319. self.offset = CGPointMake(0.f, 0.f);
  320. } else {
  321. self.offset = CGPointMake(0.f, MBProgressMaxOffset);
  322. }
  323. return self;
  324. };
  325. }
  326. - (MBProgressHUD *(^)(NHHUDProgressStyle))hudProgressStyle {
  327. return ^(NHHUDProgressStyle hudProgressStyle){
  328. if (hudProgressStyle == NHHUDProgressDeterminate) {
  329. self.mode = MBProgressHUDModeDeterminate;
  330. } else if (hudProgressStyle == NHHUDProgressAnnularDeterminate) {
  331. self.mode = MBProgressHUDModeAnnularDeterminate;
  332. } else if (hudProgressStyle == NHHUDProgressCancelationDeterminate) {
  333. self.mode = MBProgressHUDModeDeterminate;
  334. } else if (hudProgressStyle == NHHUDProgressDeterminateHorizontalBar) {
  335. self.mode = MBProgressHUDModeDeterminateHorizontalBar;
  336. }
  337. return self;
  338. };
  339. }
  340. @end