酷店

KDPublicMethod.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. KDPAccountModel *model =[KDPAccountTool account];
  107. if (model.is_hidden.integerValue == 1) {
  108. return YES;
  109. }
  110. return NO;
  111. }
  112. + (BOOL)JumpOpenURL:(NSString *)url {
  113. return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
  114. }
  115. /**
  116. 判断是否安装淘宝app
  117. */
  118. + (BOOL)isInstallTB {
  119. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
  120. return YES;
  121. }else {
  122. return NO;
  123. }
  124. }
  125. +(void) jumpKwaiWithURL:(NSString *)str{
  126. NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  127. NSString *encodeUrl = (NSString *)[encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet lowercaseLetterCharacterSet]];
  128. NSString *schemeUrlString = [NSString stringWithFormat:@"%@%@", @"kwai://webview?url=", encodeUrl];
  129. NSURL *schemeUrl = [NSURL URLWithString:schemeUrlString];
  130. if ([self JumpOpenURL:schemeUrlString]) {
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. [[UIApplication sharedApplication] openURL:schemeUrl];
  133. });
  134. }else{
  135. [MBProgressHUD showMessage:KWaiMessage];
  136. }
  137. }
  138. #pragma mark---计算时间
  139. +(NSString *)CalculateCurrentDate:(NSString *)dateStr
  140. {
  141. //转date
  142. NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
  143. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  144. NSDate *betweenDate=[formatter dateFromString:dateStr];
  145. // 时区相差8个小时 加上这个时区即是北京时间
  146. NSTimeInterval inter=[[NSDate date] timeIntervalSinceDate:betweenDate];
  147. if (inter<60) {//60秒内
  148. return [NSString stringWithFormat:@"%.0f秒",inter];
  149. }else if (inter<3600)
  150. {
  151. return [NSString stringWithFormat:@"%.0f分",inter/60];//返回分
  152. }else
  153. {
  154. return [NSString stringWithFormat:@"%.0f小时",inter/3600];//返回小时
  155. }
  156. return @"刚刚";
  157. }
  158. @end