ZYCTool.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // ZYCTool.m
  3. // ttt
  4. //
  5. // Created by 张毅成 on 2018/5/14.
  6. // Copyright © 2018年 张毅成. All rights reserved.
  7. //
  8. #import "ZYCTool.h"
  9. #import <AVFoundation/AVCaptureDevice.h>
  10. #import <AVFoundation/AVMediaFormat.h>
  11. #import <Photos/PHPhotoLibrary.h>
  12. @implementation ZYCTool
  13. + (void)alertControllerOneButtonWithTitle:(NSString *)title message:(NSString *)message target:(UIViewController *)viewController defaultButtonTitle:(NSString *)defaultButtonTitle defaultAction:(returnNotarize)defaultAction {
  14. if (defaultButtonTitle.length == 0) {
  15. defaultButtonTitle = @"确认";
  16. }
  17. if (title.length == 0) {
  18. title = @"";
  19. }
  20. if (message.length == 0) {
  21. message = @"";
  22. }
  23. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  24. UIAlertAction *defaultA = [UIAlertAction actionWithTitle:defaultButtonTitle style:0 handler:^(UIAlertAction *actoin){
  25. if (defaultAction) {
  26. defaultAction();
  27. }
  28. [alertController dismissViewControllerAnimated:true completion:^{}];
  29. }];
  30. [alertController addAction:defaultA];
  31. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight];
  32. [generator prepare];
  33. [generator impactOccurred];
  34. [viewController presentViewController:alertController animated:YES completion:nil];
  35. }
  36. + (void)alertControllerTwoButtonWithTitle:(NSString *)title message:(NSString *)message target:(UIViewController *)viewController notarizeButtonTitle:(NSString *)notarizeButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle notarizeAction:(returnNotarize)notarize cancelAction:(returnCancel)cancel {
  37. if (cancelButtonTitle.length == 0) {
  38. cancelButtonTitle = @"取消";
  39. }
  40. if (notarizeButtonTitle.length == 0) {
  41. notarizeButtonTitle = @"确认";
  42. }
  43. if (title.length == 0) {
  44. title = @"";
  45. }
  46. if (message.length == 0) {
  47. message = @"";
  48. }
  49. UIAlertController *alertController = [UIAlertController alertControllerWithTitle: title message:message preferredStyle:UIAlertControllerStyleAlert];
  50. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *acton){
  51. if (cancel) {
  52. cancel();
  53. }
  54. [alertController dismissViewControllerAnimated:true completion:^{}];
  55. }];
  56. UIAlertAction *notarizeAction = [UIAlertAction actionWithTitle:notarizeButtonTitle style:0 handler:^(UIAlertAction *action){
  57. if (notarize) {
  58. notarize();
  59. }
  60. [alertController dismissViewControllerAnimated:true completion:^{}];
  61. }];
  62. [alertController addAction:cancelAction];
  63. [alertController addAction:notarizeAction];
  64. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight];
  65. [generator prepare];
  66. [generator impactOccurred];
  67. [viewController presentViewController: alertController animated:YES completion:nil];
  68. }
  69. + (void)controller:(UIViewController *)viewController CameraIsAvailable:(Available)Available OrNotAvailable:(NotAvailable)NotAvailable {
  70. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  71. if(authStatus == AVAuthorizationStatusAuthorized) {
  72. if (Available) {
  73. Available();
  74. }
  75. }else if (authStatus == AVAuthorizationStatusNotDetermined) {
  76. if (Available) {
  77. Available();
  78. }
  79. }else{
  80. [ZYCTool alertControllerTwoButtonWithTitle:@"没有权限" message:@"点击确定前往设置打开相机权限" target:viewController notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{
  81. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  82. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  83. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
  84. }
  85. } cancelAction:^{
  86. if (NotAvailable) {
  87. NotAvailable();
  88. }
  89. }];
  90. }
  91. }
  92. + (void)controller:(UIViewController *)viewController AlbumIsAvailable:(Available)Available OrNotAvailable:(NotAvailable)NotAvailable {
  93. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  94. if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied){
  95. [ZYCTool alertControllerTwoButtonWithTitle:@"没有权限" message:@"点击确定前往设置打开照片权限" target:viewController notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{
  96. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  97. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  98. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
  99. }
  100. } cancelAction:^{
  101. if (NotAvailable) {
  102. NotAvailable();
  103. }
  104. }];
  105. return;
  106. } else {
  107. if (Available) {
  108. Available();
  109. }
  110. }
  111. }
  112. + (void)countDownWithTime:(NSInteger)countTime AndCounting:(void(^)(NSInteger count))counting AndFinished:(void(^)(void))finished {
  113. __block NSInteger time = countTime; //倒计时时间
  114. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  115. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  116. dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
  117. dispatch_source_set_event_handler(timer, ^{
  118. if(time <= 0){ //倒计时结束,关闭
  119. dispatch_source_cancel(timer);
  120. dispatch_async(dispatch_get_main_queue(), ^{
  121. if (finished) {
  122. finished();
  123. }
  124. });
  125. }else{
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. if (counting) {
  128. counting(time);
  129. }
  130. });
  131. time --;
  132. }
  133. });
  134. dispatch_resume(timer);
  135. }
  136. + (void)actionSheetWithTitleArray:(NSMutableArray *)titleArray target:(UIViewController *)viewController notarizeAction:(returnNotarizeWithIdx)returnNotarizeWithIdx {
  137. UIAlertController *alertController = [UIAlertController new];
  138. [titleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  139. UIAlertAction *alertAction = [UIAlertAction actionWithTitle:obj style:0 handler:^(UIAlertAction *action){
  140. returnNotarizeWithIdx(idx);
  141. }];
  142. [alertController addAction:alertAction];
  143. }];
  144. UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction *action){
  145. [alertController dismissViewControllerAnimated:true completion:^{}];
  146. }];
  147. [alertController addAction:alertAction];
  148. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleLight];
  149. [generator prepare];
  150. [generator impactOccurred];
  151. [viewController presentViewController: alertController animated:YES completion:nil];
  152. }
  153. + (BOOL)isHaveBang {
  154. BOOL isHaveBang = false;
  155. if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
  156. return isHaveBang;
  157. }
  158. if (@available(iOS 11.0, *)) {
  159. /// 利用safeAreaInsets.bottom > 0.0来判断是否是iPhone X。
  160. UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
  161. if (mainWindow.safeAreaInsets.bottom > 0.0) {
  162. isHaveBang = YES;
  163. }
  164. }
  165. return isHaveBang;
  166. }
  167. /// 设置间距
  168. +(void)setLabelSpace:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font {
  169. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  170. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  171. paraStyle.alignment = NSTextAlignmentLeft;
  172. paraStyle.lineSpacing = space; //设置行间距
  173. paraStyle.hyphenationFactor = 1.0;
  174. paraStyle.firstLineHeadIndent = 0.0;
  175. paraStyle.paragraphSpacingBefore = 0.0;
  176. paraStyle.headIndent = 0;
  177. paraStyle.tailIndent = 0;
  178. //设置字间距 NSKernAttributeName:@1.5f
  179. NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f
  180. };
  181. NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic];
  182. label.attributedText = attributeStr;
  183. }
  184. /// 设置首行缩进和行距
  185. +(void)setLabelFirstLineHeadIndent:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font setTing:(NSUInteger)headIndent {
  186. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  187. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  188. paraStyle.alignment = NSTextAlignmentLeft;
  189. paraStyle.lineSpacing = space; //设置行间距
  190. paraStyle.hyphenationFactor = 1.0;
  191. paraStyle.firstLineHeadIndent = font.pointSize * headIndent;
  192. paraStyle.paragraphSpacingBefore = 0.0;
  193. paraStyle.headIndent = 0;
  194. paraStyle.tailIndent = 0;
  195. //设置字间距 NSKernAttributeName:@1.5f
  196. NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f
  197. };
  198. NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic];
  199. label.attributedText = attributeStr;
  200. }
  201. /// 设置首行缩进和行距 字距
  202. +(void)setLabel:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font setLineSpace:(NSUInteger)headIndent setTextSpace:(CGFloat)textspace {
  203. if (ISEmptyString( label.text)) {
  204. return;
  205. }
  206. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  207. paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  208. paraStyle.alignment = NSTextAlignmentNatural;
  209. paraStyle.lineSpacing = space; //设置行间距
  210. paraStyle.hyphenationFactor = 1.0;
  211. //paraStyle.firstLineHeadIndent = font.pointSize * headIndent;
  212. paraStyle.paragraphSpacingBefore = 0.0;
  213. paraStyle.headIndent = 0;
  214. paraStyle.tailIndent = 0;
  215. //设置字间距 NSKernAttributeName:@1.5f
  216. NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@(textspace)
  217. };
  218. NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:label.text attributes:dic];
  219. label.attributedText = attributeStr;
  220. }
  221. + (NSString *)handerResultData:(NSError *)error{
  222. NSParameterAssert(error);
  223. NSDictionary * errorInfo = error.userInfo;
  224. if ([[errorInfo allKeys] containsObject: @"com.alamofire.serialization.response.error.data"]){
  225. NSData * errorData = errorInfo[@"com.alamofire.serialization.response.error.data"];
  226. NSDictionary * errorDict = [NSJSONSerialization JSONObjectWithData: errorData options:NSJSONReadingAllowFragments error:nil];
  227. NSLog(@"App数据请求错误%@",errorDict[@"Message"]);
  228. return errorDict[@"Message"];
  229. }else{
  230. return @"服务器挂了,请联系后台";
  231. }
  232. }
  233. /**
  234. 字典判断是否为空
  235. @param dict 字典
  236. @return bool值
  237. */
  238. + (BOOL)isNullToDictionary:(NSDictionary *)dict {
  239. if (dict != nil && ![dict isKindOfClass:[NSNull class]] && dict.count != 0){
  240. return NO;
  241. }else{
  242. return YES;
  243. }
  244. }
  245. + (NSString *)getYearAndMonthAndDay{
  246. if (@available(iOS 13.0, *)) {
  247. NSDate *date = [NSDate now];
  248. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  249. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  250. return [dateFormatterA stringFromDate:date];
  251. } else {
  252. // 获取代表公历的NSCalendar对象
  253. NSCalendar *gregorian = [[NSCalendar alloc]
  254. initWithCalendarIdentifier:NSCalendarIdentifierChinese];
  255. // 获取当前日期
  256. NSDate* dt = [NSDate date];
  257. // 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息
  258. unsigned unitFlags = NSCalendarUnitYear |
  259. NSCalendarUnitMonth | NSCalendarUnitDay |
  260. NSCalendarUnitHour | NSCalendarUnitMinute |
  261. NSCalendarUnitSecond | NSCalendarUnitWeekday;
  262. // 获取不同时间字段的信息
  263. NSDateComponents* comp = [gregorian components: unitFlags
  264. fromDate:dt];
  265. // 获取各时间字段的数值
  266. NSLog(@"现在是%ld年" , comp.year);
  267. NSLog(@"现在是%ld月 " , comp.month);
  268. NSLog(@"现在是%ld日" , comp.day);
  269. NSLog(@"现在是%ld时" , comp.hour);
  270. NSLog(@"现在是%ld分" , comp.minute);
  271. NSLog(@"现在是%ld秒" , comp.second);
  272. NSLog(@"现在是星期%ld" , comp.weekday);
  273. // 再次创建一个NSDateComponents对象
  274. NSDateComponents* comp2 = [[NSDateComponents alloc]
  275. init];
  276. // 设置各时间字段的数值
  277. comp2.year = 2013;
  278. comp2.month = 4;
  279. comp2.day = 5;
  280. comp2.hour = 18;
  281. comp2.minute = 34;
  282. // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象
  283. NSDate *date = [gregorian dateFromComponents:comp2];
  284. NSLog(@"获取的日期为:%@" , date);
  285. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  286. dateFormatterA.dateFormat = @"yyyy年MM月dd日";
  287. return [dateFormatterA stringFromDate:dt];
  288. }
  289. }
  290. + (NSString *)monthAndDayAndHoursAndMinutes:(NSString *)dateString{
  291. //dateString FORMAT yyyy-MM-dd HH:mm:ss
  292. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  293. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  294. NSDate *date = [dateFormatterA dateFromString:dateString];
  295. NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init];
  296. dateFormatterB.dateFormat = @"MM-dd HH:mm";
  297. return [dateFormatterB stringFromDate:date];
  298. }
  299. + (NSString *)yearMonthAndDay:(NSString *)dateString{
  300. //dateString FORMAT yyyy-MM-dd HH:mm:ss
  301. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  302. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  303. NSDate *date = [dateFormatterA dateFromString:dateString];
  304. NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init];
  305. dateFormatterB.dateFormat = @"yyyy-MM-dd";
  306. return [dateFormatterB stringFromDate:date];
  307. }
  308. + (NSString *)yearMonthAndDayHourMinuesSecond:(NSString *)dateString{
  309. //dateString FORMAT yyyy-MM-dd HH:mm:ss
  310. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  311. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  312. NSDate *date = [dateFormatterA dateFromString:dateString];
  313. NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init];
  314. dateFormatterB.dateFormat = @"yyyy/MM/dd HH:mm";
  315. return [dateFormatterB stringFromDate:date];
  316. }
  317. + (NSString *)MonthAndDay:(NSString *)dateString
  318. {
  319. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  320. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  321. NSDate *date = [dateFormatterA dateFromString:dateString];
  322. NSDateFormatter *dateFormatterB = [[NSDateFormatter alloc] init];
  323. dateFormatterB.dateFormat = @"MM月dd日";
  324. return [dateFormatterB stringFromDate:date];
  325. }
  326. + (NSDate *)StringReturnDate:(NSString *)dateString
  327. {
  328. NSDateFormatter *dateFormatterA = [[NSDateFormatter alloc] init];
  329. dateFormatterA.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  330. NSDate *date = [dateFormatterA dateFromString:dateString];
  331. return date;
  332. }
  333. + (NSString *)getCommenttext:(NSInteger)number{
  334. if (number == 0) {
  335. return @"0";
  336. }else if (100 <= number && number <= 999){
  337. return [NSString stringWithFormat:@"%.1f百",number/100.0f];
  338. }else if (1000 <= number && number <= 9999){
  339. return [NSString stringWithFormat:@"%.1f千",number/1000.0f];
  340. }else if (10000 <= number){
  341. return [NSString stringWithFormat:@"%.1f万",number/10000.0f];
  342. }else{
  343. return [NSString stringWithFormat:@"%ld",(long)number];
  344. }
  345. return @"0";
  346. }
  347. + (NSMutableArray *)setToArray:(NSMutableSet *)set{
  348. NSEnumerator * en = [set objectEnumerator];
  349. id depart;
  350. NSMutableArray *arr = [NSMutableArray array];
  351. while (depart = [en nextObject]) {
  352. [arr addObject:depart];
  353. }
  354. return arr;
  355. }
  356. + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr KeyString:(NSString *)keyString
  357. {
  358. if (keyString.length == 0) {
  359. return [[NSMutableAttributedString alloc] initWithString:@""];
  360. }
  361. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr];
  362. [attrString addAttribute:(NSString *)NSForegroundColorAttributeName
  363. value:UIColorHex(0099FF)
  364. range:[infoStr rangeOfString:keyString]];
  365. return attrString;
  366. }
  367. + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr withSearchText:(NSString *)text
  368. {
  369. if (infoStr.length == 0) {
  370. return nil;
  371. }
  372. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr];
  373. [attrString addAttribute:(NSString *)NSForegroundColorAttributeName
  374. value:UIColorHex(EA3729)
  375. range:[infoStr rangeOfString:text]];
  376. return attrString;
  377. }
  378. + (NSMutableAttributedString *)checkOfString:(NSString *)infoStr withSearchText:(NSString *)text bgtextColor:(UIColor *)bgColor foreTextColor:(UIColor *)foreColor font:(UIFont *)font
  379. {
  380. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:infoStr];
  381. [attrString addAttribute:(NSString *)NSForegroundColorAttributeName
  382. value:bgColor
  383. range:NSMakeRange(0, infoStr.length)];
  384. [attrString addAttribute:(NSString *)NSFontAttributeName
  385. value:font
  386. range:NSMakeRange(0, infoStr.length)];
  387. [attrString addAttribute:(NSString *)NSForegroundColorAttributeName
  388. value:foreColor
  389. range:[infoStr rangeOfString:text]];
  390. return attrString;
  391. }
  392. + (NSString *)getFileNameImage:(NSString *)fileName
  393. {
  394. NSString * name = [[fileName pathExtension] lowercaseString];
  395. if ([name hasSuffix:@"doc"] || [name hasSuffix:@"docx"]) {
  396. return @"icon_word";
  397. }else if([name hasSuffix:@"xls"] || [name hasSuffix:@"xlsx"]){
  398. return @"icon_excel";
  399. }else if([name hasSuffix:@"ppt"] || [name hasSuffix:@"pptx"] || [name hasSuffix:@"pptm"]){
  400. return @"icon_ppt";
  401. }else if([name hasSuffix:@"pdf"]){
  402. return @"icon_pdf";
  403. }else if([name hasSuffix:@"zip"]){
  404. return @"icon_yasuowenjian";
  405. }else if([name hasSuffix:@"psd"]){
  406. return @"icon_psd";
  407. }else if([name hasSuffix:@"ai"]){
  408. return @"icon_ai";
  409. }else if([name hasSuffix:@"txt"]){
  410. return @"icon_txt";
  411. }else if([name hasSuffix:@"html"]){
  412. return @"icon_html";
  413. }else if([name hasSuffix:@"png"] || [name hasSuffix:@"jpg"] || [name hasSuffix:@"jpeg"] || [name hasSuffix:@"bmp"]){
  414. return nil;
  415. }else{
  416. return @"icon_weizhiwenjian";
  417. }
  418. }
  419. @end