// // SXManager.m // Project // // Created by 石勋 on 16/4/5. // Copyright © 2016年 石勋. All rights reserved. // #import "SXAlert.h" #import "Header.h" @implementation SXAlert + (void)showMessage:(NSString *)message parent:(UIViewController *)parentController finished:(void(^)(void))finished{ UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message]; [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, message.length)]; [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, message.length)]; [alertcontroller setValue:alertControllerMessageStr forKey:@"attributedMessage"]; UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { //调用block if (finished) { finished(); } }]; //字体颜色 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) { [action setValue:SXUIColorFromRGB(0x38a2f0) forKey:@"titleTextColor"]; }else { } // [alertcontroller addAction:action]; UIViewController * result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray * windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if (tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } UIView * frontView = [[window subviews] objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) result = nextResponder; else result = window.rootViewController.presentedViewController; //如果当前parentController是UIViewController,就移除它,保证当前页面只有一个UIViewController if ([parentController isKindOfClass:[UIAlertController class]]) { [parentController dismissViewControllerAnimated:NO completion:nil]; parentController = result; } [parentController presentViewController:alertcontroller animated:YES completion:nil]; } + (void)showOptionstitle:(NSString *)title Message:(NSString *)message parent:(UIViewController *)parentController leftTitle:(NSString *)leftTItle rightTitle:(NSString *)rightTitle leftClick:(void(^)(void))leftClick rightClick:(void(^)(void))rightClick{ //这是获取当前控制器 UIViewController * result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray * windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if (tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } UIView * frontView = [[window subviews] objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) result = nextResponder; else result = window.rootViewController.presentedViewController; //-------------------结束------------------- UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; //title字体 NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title]; [alertControllerStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, title.length)]; [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, title.length)]; [alertcontroller setValue:alertControllerStr forKey:@"attributedTitle"]; //消息颜色字体大小 NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message]; [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:SXUIColorFromRGB(0x666666) range:NSMakeRange(0, message.length)]; [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, message.length)]; [alertcontroller setValue:alertControllerMessageStr forKey:@"attributedMessage"]; if (leftTItle == nil) { }else { UIAlertAction * leftAction = [UIAlertAction actionWithTitle:leftTItle style:0 handler:^(UIAlertAction *action) { if (leftClick) { leftClick(); } }]; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) { [leftAction setValue:SXUIColorFromRGB(0x333333) forKey:@"titleTextColor"]; }else { } [alertcontroller addAction:leftAction]; } UIAlertAction * rightAction = [UIAlertAction actionWithTitle:rightTitle style:0 handler:^(UIAlertAction *action) { if (rightClick) { rightClick(); } }]; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.3) { [rightAction setValue:SXUIColorFromRGB(0xff3824) forKey:@"titleTextColor"]; }else { } [alertcontroller addAction:rightAction]; //如果当前parentController是UIViewController,就移除它,保证当前页面只有一个UIViewController if ([parentController isKindOfClass:[UIAlertController class]]) { [parentController dismissViewControllerAnimated:NO completion:nil]; parentController = result; } [parentController presentViewController:alertcontroller animated:YES completion:nil]; } @end