酷店

KDPublicMethod.m 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // KDPublicMethod.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPublicMethod.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @implementation KDPublicMethod
  11. +(NSMutableAttributedString *)sethanggaoWithStr:(NSString *)text linSpacing:(CGFloat)line
  12. {
  13. NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:text];
  14.   NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  15. [paragraphStyle setLineSpacing:line];//设置行间距
  16.    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
  17. return attributedString;
  18. }
  19. +(NSString *)getNowTimeTimestamp{
  20. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  21. NSTimeInterval a=[dat timeIntervalSince1970];
  22. NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
  23. ;
  24. return timeString;
  25. }
  26. + (NSString *)md5:(NSString *)str
  27. {
  28. const char *cStr = [str UTF8String];
  29. unsigned char result[16];
  30. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  31. return [NSString stringWithFormat:
  32. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  33. result[0], result[1], result[2], result[3],
  34. result[4], result[5], result[6], result[7],
  35. result[8], result[9], result[10], result[11],
  36. result[12], result[13], result[14], result[15]
  37. ];
  38. }
  39. #pragma mark---b保存聊天记录
  40. +(void)saveMessage:(NSDictionary *)message
  41. {
  42. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  43. //yes为显示金额,no为隐藏
  44. [defaults setObject: message forKey:@"message"];
  45. [defaults synchronize];
  46. }
  47. +(NSDictionary *)getMessage
  48. {
  49. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  50. return [defaults objectForKey:@"message"];
  51. }
  52. /**
  53. json字符串转字典
  54. */
  55. + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
  56. {
  57. if (jsonString == nil) {
  58. return nil;
  59. }
  60. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  61. NSError *err;
  62. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  63. options:NSJSONReadingMutableContainers
  64. error:&err];
  65. if(err)
  66. {
  67. NSLog(@"json解析失败:%@",err);
  68. return nil;
  69. }
  70. return dic;
  71. }
  72. /**
  73. 字典转json字符串
  74. */
  75. + (NSString *)convertToJsonData:(NSDictionary *)dict
  76. {
  77. NSError *error;
  78. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  79. NSString *jsonString;
  80. if (!jsonData) {
  81. NSLog(@"%@",error);
  82. }else{
  83. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  84. }
  85. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  86. NSRange range = {0,jsonString.length};
  87. //去掉字符串中的空格
  88. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  89. NSRange range2 = {0,mutStr.length};
  90. //去掉字符串中的换行符
  91. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  92. return mutStr;
  93. }
  94. + (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont*)font
  95. {
  96. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
  97. label.text = title;
  98. label.font = font;
  99. label.numberOfLines = 0;
  100. [label sizeToFit];
  101. CGFloat height = label.frame.size.height;
  102. return ceil(height);
  103. }
  104. +(BOOL)is_hiddenMoney
  105. {
  106. //存储第一次打开APP,是否显示客服页面,
  107. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  108. NSString *oneStep =[defaults objectForKey:@"hidden"];
  109. if (oneStep.integerValue == 1) {
  110. return YES;
  111. }
  112. return NO;
  113. }
  114. + (BOOL)JumpOpenURL:(NSString *)url {
  115. return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
  116. }
  117. /**
  118. 判断是否安装淘宝app
  119. */
  120. + (BOOL)isInstallTB {
  121. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
  122. return YES;
  123. }else {
  124. return NO;
  125. }
  126. }
  127. +(void) jumpKwaiWithURL:(NSString *)str{
  128. NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  129. NSString *encodeUrl = (NSString *)[encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet lowercaseLetterCharacterSet]];
  130. NSString *schemeUrlString = [NSString stringWithFormat:@"%@%@", @"kwai://webview?url=", encodeUrl];
  131. NSURL *schemeUrl = [NSURL URLWithString:schemeUrlString];
  132. if ([self JumpOpenURL:schemeUrlString]) {
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. [[UIApplication sharedApplication] openURL:schemeUrl];
  135. });
  136. }else{
  137. [MBProgressHUD showMessage:KWaiMessage];
  138. }
  139. }
  140. #pragma mark---计算时间
  141. +(NSString *)CalculateCurrentDate:(NSString *)dateStr
  142. {
  143. //转date
  144. NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
  145. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  146. NSDate *betweenDate=[formatter dateFromString:dateStr];
  147. // 时区相差8个小时 加上这个时区即是北京时间
  148. NSTimeInterval inter=[[NSDate date] timeIntervalSinceDate:betweenDate];
  149. if (inter<60) {//60秒内
  150. return [NSString stringWithFormat:@"%.0f秒",inter];
  151. }else if (inter<3600)
  152. {
  153. return [NSString stringWithFormat:@"%.0f分",inter/60];//返回分
  154. }else
  155. {
  156. return [NSString stringWithFormat:@"%.0f小时",inter/3600];//返回小时
  157. }
  158. return @"刚刚";
  159. }
  160. @end