// // ZYCTool.m // ttt // // Created by 张毅成 on 2018/5/14. // Copyright © 2018年 张毅成. All rights reserved. // #import "ZYCTool.h" #import #import #import @implementation ZYCTool + (void)alertControllerOneButtonWithTitle:(NSString *)title message:(NSString *)message target:(UIViewController *)viewController defaultButtonTitle:(NSString *)defaultButtonTitle defaultAction:(returnNotarize)defaultAction { if (defaultButtonTitle.length == 0) { defaultButtonTitle = @"确认"; } if (title.length == 0) { title = @""; } if (message.length == 0) { message = @""; } UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *defaultA = [UIAlertAction actionWithTitle:defaultButtonTitle style:0 handler:^(UIAlertAction *actoin){ if (defaultAction) { defaultAction(); } [alertController dismissViewControllerAnimated:true completion:^{}]; }]; [alertController addAction:defaultA]; UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight]; [generator prepare]; [generator impactOccurred]; [viewController presentViewController:alertController animated:YES completion:nil]; } + (void)alertControllerTwoButtonWithTitle:(NSString *)title message:(NSString *)message target:(UIViewController *)viewController notarizeButtonTitle:(NSString *)notarizeButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle notarizeAction:(returnNotarize)notarize cancelAction:(returnCancel)cancel { if (cancelButtonTitle.length == 0) { cancelButtonTitle = @"取消"; } if (notarizeButtonTitle.length == 0) { notarizeButtonTitle = @"确认"; } if (title.length == 0) { title = @""; } if (message.length == 0) { message = @""; } UIAlertController *alertController = [UIAlertController alertControllerWithTitle: title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *acton){ if (cancel) { cancel(); } [alertController dismissViewControllerAnimated:true completion:^{}]; }]; UIAlertAction *notarizeAction = [UIAlertAction actionWithTitle:notarizeButtonTitle style:0 handler:^(UIAlertAction *action){ if (notarize) { notarize(); } [alertController dismissViewControllerAnimated:true completion:^{}]; }]; [alertController addAction:cancelAction]; [alertController addAction:notarizeAction]; UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight]; [generator prepare]; [generator impactOccurred]; [viewController presentViewController: alertController animated:YES completion:nil]; } + (void)controller:(UIViewController *)viewController CameraIsAvailable:(Available)Available OrNotAvailable:(NotAvailable)NotAvailable { AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if(authStatus == AVAuthorizationStatusAuthorized) { if (Available) { Available(); } }else if (authStatus == AVAuthorizationStatusNotDetermined) { if (Available) { Available(); } }else{ [ZYCTool alertControllerTwoButtonWithTitle:@"没有权限" message:@"点击确定前往设置打开相机权限" target:viewController notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{ NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}]; } } cancelAction:^{ if (NotAvailable) { NotAvailable(); } }]; } } + (void)controller:(UIViewController *)viewController AlbumIsAvailable:(Available)Available OrNotAvailable:(NotAvailable)NotAvailable { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied){ [ZYCTool alertControllerTwoButtonWithTitle:@"没有权限" message:@"点击确定前往设置打开照片权限" target:viewController notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{ NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}]; } } cancelAction:^{ if (NotAvailable) { NotAvailable(); } }]; return; } else { if (Available) { Available(); } } } + (void)countDownWithTime:(NSInteger)countTime AndCounting:(void(^)(NSInteger count))counting AndFinished:(void(^)(void))finished { __block NSInteger time = countTime; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(timer, ^{ if(time <= 0){ //倒计时结束,关闭 dispatch_source_cancel(timer); dispatch_async(dispatch_get_main_queue(), ^{ if (finished) { finished(); } }); }else{ dispatch_async(dispatch_get_main_queue(), ^{ if (counting) { counting(time); } }); time --; } }); dispatch_resume(timer); } + (void)actionSheetWithTitleArray:(NSMutableArray *)titleArray target:(UIViewController *)viewController notarizeAction:(returnNotarizeWithIdx)returnNotarizeWithIdx { UIAlertController *alertController = [UIAlertController new]; [titleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { UIAlertAction *alertAction = [UIAlertAction actionWithTitle:obj style:0 handler:^(UIAlertAction *action){ returnNotarizeWithIdx(idx); }]; [alertController addAction:alertAction]; }]; UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction *action){ [alertController dismissViewControllerAnimated:true completion:^{}]; }]; [alertController addAction:alertAction]; UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight]; [generator prepare]; [generator impactOccurred]; [viewController presentViewController: alertController animated:YES completion:nil]; } + (BOOL)isHaveBang { BOOL isHaveBang = false; if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) { return isHaveBang; } if (@available(iOS 11.0, *)) { /// 利用safeAreaInsets.bottom > 0.0来判断是否是iPhone X。 UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window]; if (mainWindow.safeAreaInsets.bottom > 0.0) { isHaveBang = YES; } } return isHaveBang; } /// 设置间距 +(void)setLabelSpace:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font { if (label.text.length == 0) { label.text = @""; return; } NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init]; paraStyle.lineBreakMode = NSLineBreakByCharWrapping; paraStyle.alignment = NSTextAlignmentLeft; paraStyle.lineSpacing = space; //设置行间距 paraStyle.hyphenationFactor = 1.0; paraStyle.firstLineHeadIndent = 0.0; paraStyle.paragraphSpacingBefore = 0.0; paraStyle.headIndent = 0; paraStyle.tailIndent = 0; //设置字间距 NSKernAttributeName:@1.5f NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f }; NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic]; label.attributedText = attributeStr; } /// 设置首行缩进和行距 +(void)setLabelFirstLineHeadIndent:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font setTing:(NSUInteger)headIndent { if (label.text.length == 0) { label.text = @""; return; } NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init]; paraStyle.lineBreakMode = NSLineBreakByCharWrapping; paraStyle.alignment = NSTextAlignmentLeft; paraStyle.lineSpacing = space; //设置行间距 paraStyle.hyphenationFactor = 1.0; paraStyle.firstLineHeadIndent = font.pointSize * headIndent; paraStyle.paragraphSpacingBefore = 0.0; paraStyle.headIndent = 0; paraStyle.tailIndent = 0; //设置字间距 NSKernAttributeName:@1.5f NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f }; NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic]; label.attributedText = attributeStr; } /// 设置首行缩进和行距 字距 +(void)setLabel:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font setLineSpace:(NSUInteger)headIndent setTextSpace:(CGFloat)textspace { if (ISEmptyString( label.text)) { return; } NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init]; paraStyle.lineBreakMode = NSLineBreakByCharWrapping; paraStyle.alignment = NSTextAlignmentNatural; paraStyle.lineSpacing = space; //设置行间距 paraStyle.hyphenationFactor = 1.0; //paraStyle.firstLineHeadIndent = font.pointSize * headIndent; paraStyle.paragraphSpacingBefore = 0.0; paraStyle.headIndent = 0; paraStyle.tailIndent = 0; //设置字间距 NSKernAttributeName:@1.5f NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@(textspace) }; NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic]; label.attributedText = attributeStr; } + (NSString *)handerResultData:(NSError *)error{ NSParameterAssert(error); NSDictionary * errorInfo = error.userInfo; if ([[errorInfo allKeys] containsObject: @"com.alamofire.serialization.response.error.data"]){ NSData * errorData = errorInfo[@"com.alamofire.serialization.response.error.data"]; NSDictionary * errorDict = [NSJSONSerialization JSONObjectWithData: errorData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"App数据请求错误%@",errorDict[@"Message"]); return errorDict[@"Message"]; }else{ return @"服务器挂了,请联系后台"; } } /** 字典判断是否为空 @param dict 字典 @return bool值 */ + (BOOL)isNullToDictionary:(NSDictionary *)dict { if (dict != nil && ![dict isKindOfClass:[NSNull class]] && dict.count != 0){ return NO; }else{ return YES; } } + (NSString *)getYearAndMonthAndDay{ if (@available(iOS 13.0, *)) { NSDate *date = [NSDate now]; NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss"; return [dateFormatterA stringFromDate:date]; } else { // 获取代表公历的NSCalendar对象 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese]; // 获取当前日期 NSDate* dt = [NSDate date]; // 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday; // 获取不同时间字段的信息 NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt]; // 获取各时间字段的数值 NSLog(@"现在是%ld年" , comp.year); NSLog(@"现在是%ld月 " , comp.month); NSLog(@"现在是%ld日" , comp.day); NSLog(@"现在是%ld时" , comp.hour); NSLog(@"现在是%ld分" , comp.minute); NSLog(@"现在是%ld秒" , comp.second); NSLog(@"现在是星期%ld" , comp.weekday); // 再次创建一个NSDateComponents对象 NSDateComponents* comp2 = [[NSDateComponents alloc] init]; // 设置各时间字段的数值 comp2.year = 2013; comp2.month = 4; comp2.day = 5; comp2.hour = 18; comp2.minute = 34; // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象 NSDate *date = [gregorian dateFromComponents:comp2]; NSLog(@"获取的日期为:%@" , date); NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy年MM月dd日"; return [dateFormatterA stringFromDate:dt]; } } + (NSString *)monthAndDayAndHoursAndMinutes:(NSString *)dateString{ //dateString FORMAT yyyy-MM-dd HH:mm:ss NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [dateFormatterA dateFromString:dateString]; NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init]; dateFormatterB.dateFormat = @"MM-dd HH:mm"; return [dateFormatterB stringFromDate:date]; } + (NSString *)yearMonthAndDay:(NSString *)dateString{ //dateString FORMAT yyyy-MM-dd HH:mm:ss NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [dateFormatterA dateFromString:dateString]; NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init]; dateFormatterB.dateFormat = @"yyyy-MM-dd"; return [dateFormatterB stringFromDate:date]; } + (NSString *)yearMonthAndDayHourMinuesSecond:(NSString *)dateString{ //dateString FORMAT yyyy-MM-dd HH:mm:ss NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [dateFormatterA dateFromString:dateString]; NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init]; dateFormatterB.dateFormat = @"yyyy/MM/dd HH:mm"; return [dateFormatterB stringFromDate:date]; } + (NSString *)MonthAndDay:(NSString *)dateString { NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd"; NSString * subData = [dateString substringToIndex:10]; NSDate *date = [dateFormatterA dateFromString:subData]; NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init]; dateFormatterB.dateFormat = @"MM月dd日"; return [dateFormatterB stringFromDate:date]; } + (NSDate *)StringReturnDate:(NSString *)dateString { NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init]; dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [dateFormatterA dateFromString:dateString]; return date; } + (NSString *)getCommenttext:(NSInteger)number{ if (number == 0) { return @"0"; }else if (100 <= number && number <= 999){ return [NSString stringWithFormat:@"%.1f百",number/100.0f]; }else if (1000 <= number && number <= 9999){ return [NSString stringWithFormat:@"%.1f千",number/1000.0f]; }else if (10000 <= number){ return [NSString stringWithFormat:@"%.1f万",number/10000.0f]; }else{ return [NSString stringWithFormat:@"%ld",(long)number]; } return @"0"; } + (NSMutableArray *)setToArray:(NSMutableSet *)set{ NSEnumerator * en = [set objectEnumerator]; id depart; NSMutableArray *arr = [NSMutableArray array]; while (depart = [en nextObject]) { [arr addObject:depart]; } return arr; } + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr KeyString:(NSString *)keyString { if (keyString.length == 0) { return [[NSMutableAttributedString alloc] initWithString:@""]; } NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr]; [attrString addAttribute:(NSString *)NSForegroundColorAttributeName value:UIColorHex(0099FF) range:[infoStr rangeOfString:keyString]]; return attrString; } + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr withSearchText:(NSString *)text { if (infoStr.length == 0) { return [[NSMutableAttributedString alloc] initWithString:@""]; } if (text.length == 0) { return [[NSMutableAttributedString alloc] initWithString:infoStr]; } NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr]; [attrString addAttribute:(NSString *)NSForegroundColorAttributeName value:UIColorHex(EA3729) range:[infoStr rangeOfString:text]]; return attrString; } + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr withSearchText:(NSString *)text withColor:(UIColor *)color { if (infoStr.length == 0) { return nil; } if (text.length == 0) { return [[NSMutableAttributedString alloc] initWithString:infoStr]; } NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr]; [attrString addAttribute:(NSString *)NSForegroundColorAttributeName value:color range:[infoStr rangeOfString:text]]; return attrString; } + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr withSearchText:(NSString *)text bgtextColor:(UIColor *)bgColor foreTextColor:(UIColor *)foreColor font:(UIFont *)font { if (infoStr.length == 0) { return [[NSMutableAttributedString alloc] initWithString:@""]; } if (text.length == 0) { return [[NSMutableAttributedString alloc] initWithString:infoStr]; } NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr]; [attrString addAttribute:(NSString *)NSForegroundColorAttributeName value:bgColor range:NSMakeRange(0, infoStr.length)]; [attrString addAttribute:(NSString *)NSFontAttributeName value:font range:NSMakeRange(0, infoStr.length)]; [attrString addAttribute:(NSString *)NSForegroundColorAttributeName value:foreColor range:[infoStr rangeOfString:text]]; return attrString; } + (NSString *)getFileNameImage:(NSString *)fileName { NSString * name = [[fileName pathExtension] lowercaseString]; if ([name hasSuffix:@"doc"] || [name hasSuffix:@"docx"]) { return @"icon_word"; }else if([name hasSuffix:@"xls"] || [name hasSuffix:@"xlsx"]){ return @"icon_excel"; }else if([name hasSuffix:@"ppt"] || [name hasSuffix:@"pptx"] || [name hasSuffix:@"pptm"]){ return @"icon_ppt"; }else if([name hasSuffix:@"pdf"]){ return @"icon_pdf"; }else if([name hasSuffix:@"zip"]){ return @"icon_yasuowenjian"; }else if([name hasSuffix:@"psd"]){ return @"icon_psd"; }else if([name hasSuffix:@"ai"]){ return @"icon_ai"; }else if([name hasSuffix:@"txt"]){ return @"icon_txt"; }else if([name hasSuffix:@"html"]){ return @"icon_html"; }else if([name hasSuffix:@"png"] || [name hasSuffix:@"jpg"] || [name hasSuffix:@"jpeg"] || [name hasSuffix:@"bmp"]){ return nil; }else{ return @"icon_weizhiwenjian"; } } @end