SXAlert.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // SXManager.m
  3. // Project
  4. //
  5. // Created by 石勋 on 16/4/5.
  6. // Copyright © 2016年 石勋. All rights reserved.
  7. //
  8. #import "SXAlert.h"
  9. #import "Header.h"
  10. @implementation SXAlert
  11. + (void)showMessage:(NSString *)message parent:(UIViewController *)parentController finished:(void(^)(void))finished{
  12. UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
  13. NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
  14. [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, message.length)];
  15. [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, message.length)];
  16. [alertcontroller setValue:alertControllerMessageStr forKey:@"attributedMessage"];
  17. UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  18. //调用block
  19. if (finished) {
  20. finished();
  21. }
  22. }];
  23. //字体颜色
  24. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) {
  25. [action setValue:SXUIColorFromRGB(0x38a2f0) forKey:@"titleTextColor"];
  26. }else {
  27. }
  28. //
  29. [alertcontroller addAction:action];
  30. UIViewController * result = nil;
  31. UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  32. if (window.windowLevel != UIWindowLevelNormal)
  33. {
  34. NSArray * windows = [[UIApplication sharedApplication] windows];
  35. for(UIWindow * tmpWin in windows)
  36. {
  37. if (tmpWin.windowLevel == UIWindowLevelNormal)
  38. {
  39. window = tmpWin;
  40. break;
  41. }
  42. }
  43. }
  44. UIView * frontView = [[window subviews] objectAtIndex:0];
  45. id nextResponder = [frontView nextResponder];
  46. if ([nextResponder isKindOfClass:[UIViewController class]])
  47. result = nextResponder;
  48. else
  49. result = window.rootViewController.presentedViewController;
  50. //如果当前parentController是UIViewController,就移除它,保证当前页面只有一个UIViewController
  51. if ([parentController isKindOfClass:[UIAlertController class]]) {
  52. [parentController dismissViewControllerAnimated:NO completion:nil];
  53. parentController = result;
  54. }
  55. [parentController presentViewController:alertcontroller animated:YES completion:nil];
  56. }
  57. + (void)showOptionstitle:(NSString *)title Message:(NSString *)message parent:(UIViewController *)parentController leftTitle:(NSString *)leftTItle rightTitle:(NSString *)rightTitle leftClick:(void(^)(void))leftClick rightClick:(void(^)(void))rightClick{
  58. //这是获取当前控制器
  59. UIViewController * result = nil;
  60. UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  61. if (window.windowLevel != UIWindowLevelNormal)
  62. {
  63. NSArray * windows = [[UIApplication sharedApplication] windows];
  64. for(UIWindow * tmpWin in windows)
  65. {
  66. if (tmpWin.windowLevel == UIWindowLevelNormal)
  67. {
  68. window = tmpWin;
  69. break;
  70. }
  71. }
  72. }
  73. UIView * frontView = [[window subviews] objectAtIndex:0];
  74. id nextResponder = [frontView nextResponder];
  75. if ([nextResponder isKindOfClass:[UIViewController class]])
  76. result = nextResponder;
  77. else
  78. result = window.rootViewController.presentedViewController;
  79. //-------------------结束-------------------
  80. UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  81. //title字体
  82. NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
  83. [alertControllerStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, title.length)];
  84. [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, title.length)];
  85. [alertcontroller setValue:alertControllerStr forKey:@"attributedTitle"];
  86. //消息颜色字体大小
  87. NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
  88. [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, message.length)];
  89. [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, message.length)];
  90. [alertcontroller setValue:alertControllerMessageStr forKey:@"attributedMessage"];
  91. if (leftTItle == nil) {
  92. }else {
  93. UIAlertAction * leftAction = [UIAlertAction actionWithTitle:leftTItle style:0 handler:^(UIAlertAction *action) {
  94. if (leftClick) {
  95. leftClick();
  96. }
  97. }];
  98. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) {
  99. [leftAction setValue:SXUIColorFromRGB(0x333333) forKey:@"titleTextColor"];
  100. }else {
  101. }
  102. [alertcontroller addAction:leftAction];
  103. }
  104. UIAlertAction * rightAction = [UIAlertAction actionWithTitle:rightTitle style:0 handler:^(UIAlertAction *action) {
  105. if (rightClick) {
  106. rightClick();
  107. }
  108. }];
  109. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) {
  110. [rightAction setValue:SXUIColorFromRGB(0xff3824) forKey:@"titleTextColor"];
  111. }else {
  112. }
  113. [alertcontroller addAction:rightAction];
  114. //如果当前parentController是UIViewController,就移除它,保证当前页面只有一个UIViewController
  115. if ([parentController isKindOfClass:[UIAlertController class]]) {
  116. [parentController dismissViewControllerAnimated:NO completion:nil];
  117. parentController = result;
  118. }
  119. [parentController presentViewController:alertcontroller animated:YES completion:nil];
  120. }
  121. @end