FanweMessage.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // FanweMessage.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/2/15.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "FanweMessage.h"
  9. @implementation FanweMessage
  10. #pragma mark - ----------------------- 一个按钮的弹框 -----------------------
  11. + (MMAlertView *)alert:(NSString *)message
  12. {
  13. if (![BGUtils isBlankString:message])
  14. {
  15. MMAlertView *alertView = [[MMAlertView alloc] initWithConfirmTitle:ASLocalizedString(@"温馨提示")detail:message];
  16. [alertView show];
  17. return alertView;
  18. }
  19. else
  20. {
  21. return [MMAlertView new];
  22. }
  23. }
  24. + (MMAlertView *)alert:(NSString *)title message:(NSString *)message isHideTitle:(BOOL)isHideTitle destructiveAction:(FWVoidBlock)destructiveAction
  25. {
  26. return [FanweMessage alert:title message:message isHideTitle:isHideTitle destructiveTitle:ASLocalizedString(@"确定")destructiveAction:destructiveAction];
  27. }
  28. + (MMAlertView *)alert:(NSString *)title message:(NSString *)message isHideTitle:(BOOL)isHideTitle destructiveTitle:(NSString *)destructiveTitle destructiveAction:(FWVoidBlock)destructiveAction
  29. {
  30. if (![BGUtils isBlankString:message])
  31. {
  32. MMPopupItemHandler block = ^(NSInteger index){
  33. if (destructiveAction)
  34. {
  35. destructiveAction();
  36. }
  37. };
  38. if ([BGUtils isBlankString:title])
  39. {
  40. title = ASLocalizedString(@"温馨提示");
  41. }
  42. if (isHideTitle)
  43. {
  44. title = @"";
  45. }
  46. if ([BGUtils isBlankString:destructiveTitle])
  47. {
  48. destructiveTitle = ASLocalizedString(@"知道了");
  49. }
  50. NSArray *items = @[MMItemMake(destructiveTitle, MMItemTypeNormal, block)];
  51. MMAlertView *alertView = [[MMAlertView alloc] initWithTitle:title detail:message items:items];
  52. [alertView show];
  53. return alertView;
  54. }
  55. else
  56. {
  57. return [MMAlertView new];
  58. }
  59. }
  60. #pragma mark - ----------------------- 两个按钮的弹框 -----------------------
  61. + (MMAlertView *)alert:(NSString *)title message:(NSString *)message destructiveAction:(FWVoidBlock)destructiveAction cancelAction:(FWVoidBlock)cancelAction
  62. {
  63. return [FanweMessage alert:title message:message destructiveTitle:ASLocalizedString(@"确定")destructiveAction:destructiveAction cancelTitle:ASLocalizedString(@"取消")cancelAction:cancelAction];
  64. }
  65. + (MMAlertView *)alert:(NSString *)title message:(NSString *)message destructiveTitle:(NSString *)destructiveTitle destructiveAction:(FWVoidBlock)destructiveAction cancelTitle:(NSString *)cancelTitle cancelAction:(FWVoidBlock)cancelAction
  66. {
  67. return [FanweMessage alertType:FanweMessageTypeAlertTwoBtn title:title message:message placeholder:@"" keyboardType:UIKeyboardTypeDefault destructiveTitle:destructiveTitle destructiveAction:destructiveAction cancelTitle:cancelTitle cancelAction:cancelAction inputHandler:nil];
  68. }
  69. + (MMAlertView *)alertType:(FanweMessageType)messageType title:(NSString *)title message:(NSString *)message placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType destructiveTitle:(NSString *)destructiveTitle destructiveAction:(FWVoidBlock)destructiveAction cancelTitle:(NSString *)cancelTitle cancelAction:(FWVoidBlock)cancelAction inputHandler:(MMPopupInputHandler)inputHandler
  70. {
  71. if (![BGUtils isBlankString:message])
  72. {
  73. MMPopupItemHandler block = ^(NSInteger index){
  74. if (index == 0)
  75. {
  76. if(cancelAction)
  77. {
  78. cancelAction();
  79. }
  80. }
  81. else if (index == 1)
  82. {
  83. if (destructiveAction)
  84. {
  85. destructiveAction();
  86. }
  87. }
  88. };
  89. if ([BGUtils isBlankString:destructiveTitle])
  90. {
  91. destructiveTitle = ASLocalizedString(@"确定");
  92. }
  93. if ([BGUtils isBlankString:cancelTitle])
  94. {
  95. cancelTitle = ASLocalizedString(@"取消");
  96. }
  97. NSArray *items = @[MMItemMake(cancelTitle, MMItemTypeNormal, block), MMItemMake(destructiveTitle, MMItemTypeNormal, block)];
  98. if ([BGUtils isBlankString:title])
  99. {
  100. title = ASLocalizedString(@"温馨提示");
  101. }
  102. MMAlertView *alertView;
  103. if (messageType == FanweMessageTypeAlertTwoBtn)
  104. {
  105. alertView = [[MMAlertView alloc] initWithTitle:title detail:message items:items];
  106. }
  107. else if (messageType == FanweMessageTypeInputAlertTwoBtn)
  108. {
  109. alertView = [[MMAlertView alloc] initWithInputTitle:title detail:message placeholder:placeholder keyboardType:keyboardType items:items handler:^(NSString *text) {
  110. if (inputHandler)
  111. {
  112. inputHandler(text);
  113. }
  114. }];
  115. }
  116. [alertView show];
  117. return alertView;
  118. }
  119. else
  120. {
  121. return [MMAlertView new];
  122. }
  123. // [UIAlertView bk_showAlertViewWithTitle:ASLocalizedString(@"温馨提示")message:message cancelButtonTitle:ASLocalizedString(@"取消")otherButtonTitles:@[ASLocalizedString(@"确定")] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  124. //
  125. // if (buttonIndex == 0)
  126. // {
  127. // if(cancelAction)
  128. // {
  129. // cancelAction();
  130. // }
  131. // }
  132. // else if (buttonIndex == 1)
  133. // {
  134. // if (destructiveAction)
  135. // {
  136. // destructiveAction();
  137. // }
  138. // }
  139. //
  140. // }];
  141. }
  142. + (UIAlertController *)alertController:(NSString *)message viewController:(UIViewController *)viewController
  143. {
  144. return [FanweMessage alertController:message viewController:viewController destructiveAction:nil cancelAction:nil];
  145. }
  146. + (UIAlertController *)alertController:(NSString *)message viewController:(UIViewController *)viewController destructiveAction:(FWVoidBlock)destructiveAction cancelAction:(FWVoidBlock)cancelAction
  147. {
  148. if ([BGUtils isBlankString:message])
  149. {
  150. return [UIAlertController new];
  151. }
  152. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"提示")message:message preferredStyle:UIAlertControllerStyleAlert];
  153. // // 修改message
  154. // NSString *tmpStr = [NSString stringWithFormat:@"\n%@",message];
  155. // NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:tmpStr];
  156. // [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:kAppGrayColor1 range:NSMakeRange(0, tmpStr.length)];
  157. // [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, tmpStr.length)];
  158. // [alertControllerMessageStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n "]];
  159. //
  160. // [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
  161. if (destructiveAction)
  162. {
  163. UIAlertAction *destructive = [UIAlertAction actionWithTitle:ASLocalizedString(@"确定")style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  164. destructiveAction();
  165. }];
  166. // [destructive setValue:kAppGrayColor2 forKey:@"titleTextColor"];
  167. [alertController addAction:destructive];
  168. }
  169. if (cancelAction)
  170. {
  171. UIAlertAction *cancel = [UIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  172. cancelAction();
  173. }];
  174. // [cancel setValue:kAppGrayColor2 forKey:@"titleTextColor"];
  175. [alertController addAction:cancel];
  176. }
  177. if (viewController)
  178. {
  179. [viewController presentViewController:alertController animated:YES completion:nil];
  180. }
  181. else
  182. {
  183. id rootViewController = kCurrentWindow.rootViewController;
  184. if([rootViewController isKindOfClass:[UIWindow class]])
  185. {
  186. rootViewController = ((UIWindow *)rootViewController).rootViewController;
  187. }
  188. if([rootViewController isKindOfClass:[UINavigationController class]])
  189. {
  190. rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
  191. }
  192. if([rootViewController isKindOfClass:[UITabBarController class]])
  193. {
  194. rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
  195. }
  196. if([rootViewController isKindOfClass:[UIViewController class]])
  197. {
  198. [rootViewController presentViewController:alertController animated:YES completion:nil];
  199. }
  200. else
  201. {
  202. [FanweMessage alert:ASLocalizedString(@"温馨提示")message:message destructiveAction:destructiveAction cancelAction:destructiveAction];
  203. }
  204. }
  205. return alertController;
  206. }
  207. #pragma mark - ----------------------- 两个按钮的带输入框的弹框 -----------------------
  208. + (MMAlertView *)alertInput:(NSString *)title message:(NSString *)message placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType destructiveTitle:(NSString *)destructiveTitle destructiveAction:(MMPopupInputHandler)destructiveAction cancelTitle:(NSString *)cancelTitle cancelAction:(FWVoidBlock)cancelAction
  209. {
  210. return [FanweMessage alertType:FanweMessageTypeInputAlertTwoBtn title:title message:message placeholder:placeholder keyboardType:keyboardType destructiveTitle:destructiveTitle destructiveAction:nil cancelTitle:cancelTitle cancelAction:cancelAction inputHandler:destructiveAction];
  211. }
  212. #pragma mark - ----------------------- HUD类型弹框(无按钮) -----------------------
  213. + (void)alertHUD:(NSString *)message
  214. {
  215. if (![BGUtils isBlankString:message])
  216. {
  217. [[BGHUDHelper sharedInstance] tipMessage:message];
  218. }
  219. }
  220. + (void)alertHUD:(NSString *)message delay:(CGFloat)seconds
  221. {
  222. if (![BGUtils isBlankString:message])
  223. {
  224. [[BGHUDHelper sharedInstance] tipMessage:message delay:seconds];
  225. }
  226. }
  227. #pragma mark - ----------------------- TWMessageBar类型弹框(无按钮) -----------------------
  228. + (void)alertTWMessage:(NSString *)message
  229. {
  230. if (![BGUtils isBlankString:message])
  231. {
  232. [[TWMessageBarManager sharedInstance] showMessageWithTitle:message description:nil type:TWMessageBarMessageTypeInfo];
  233. }
  234. }
  235. @end