123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // KDPublicMethod.m
- // KuDianProject
- //
- // Created by 学丽 on 2019/7/4.
- // Copyright © 2019 KDP. All rights reserved.
- //
- #import "KDPublicMethod.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation KDPublicMethod
- +(NSMutableAttributedString *)sethanggaoWithStr:(NSString *)text linSpacing:(CGFloat)line
- {
- NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:text];
- NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setLineSpacing:line];//设置行间距
-
- [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
-
- return attributedString;
-
- }
- +(NSString *)getNowTimeTimestamp{
-
-
- NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
-
- NSTimeInterval a=[dat timeIntervalSince1970];
-
- NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
-
- ;
-
- return timeString;
-
- }
- + (NSString *)md5:(NSString *)str
- {
- const char *cStr = [str UTF8String];
- unsigned char result[16];
- CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
- return [NSString stringWithFormat:
- @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
- result[0], result[1], result[2], result[3],
- result[4], result[5], result[6], result[7],
- result[8], result[9], result[10], result[11],
- result[12], result[13], result[14], result[15]
- ];
- }
- #pragma mark---b保存聊天记录
- +(void)saveMessage:(NSDictionary *)message
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- //yes为显示金额,no为隐藏
- [defaults setObject: message forKey:@"message"];
- [defaults synchronize];
- }
- +(NSDictionary *)getMessage
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- return [defaults objectForKey:@"message"];
- }
- /**
- json字符串转字典
- */
- + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
- {
- if (jsonString == nil) {
- return nil;
- }
-
- NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
- NSError *err;
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
- options:NSJSONReadingMutableContainers
- error:&err];
- if(err)
- {
- NSLog(@"json解析失败:%@",err);
- return nil;
- }
- return dic;
- }
- /**
- 字典转json字符串
- */
- + (NSString *)convertToJsonData:(NSDictionary *)dict
- {
-
- NSError *error;
-
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
-
- NSString *jsonString;
-
- if (!jsonData) {
-
- NSLog(@"%@",error);
-
- }else{
-
- jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
-
- }
-
- NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
-
- NSRange range = {0,jsonString.length};
-
- //去掉字符串中的空格
-
- [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
-
- NSRange range2 = {0,mutStr.length};
-
- //去掉字符串中的换行符
-
- [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
-
- return mutStr;
-
- }
- + (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont*)font
- {
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
- label.text = title;
- label.font = font;
- label.numberOfLines = 0;
- [label sizeToFit];
- CGFloat height = label.frame.size.height;
- return ceil(height);
- }
- +(BOOL)is_hiddenMoney
- {
- KDPAccountModel *model =[KDPAccountTool account];
- if (model.is_hidden.integerValue == 1) {
- return YES;
- }
- return NO;
-
- }
- + (BOOL)JumpOpenURL:(NSString *)url {
- return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
- }
- /**
- 判断是否安装淘宝app
- */
- + (BOOL)isInstallTB {
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
- return YES;
- }else {
- return NO;
- }
- }
- +(void) jumpKwaiWithURL:(NSString *)str{
-
- NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSString *encodeUrl = (NSString *)[encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet lowercaseLetterCharacterSet]];
-
- NSString *schemeUrlString = [NSString stringWithFormat:@"%@%@", @"kwai://webview?url=", encodeUrl];
- NSURL *schemeUrl = [NSURL URLWithString:schemeUrlString];
- if ([self JumpOpenURL:schemeUrlString]) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [[UIApplication sharedApplication] openURL:schemeUrl];
- });
- }else{
- [MBProgressHUD showMessage:KWaiMessage];
- }
-
-
-
- }
- #pragma mark---计算时间
- +(NSString *)CalculateCurrentDate:(NSString *)dateStr
- {
- //转date
- NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- NSDate *betweenDate=[formatter dateFromString:dateStr];
- // 时区相差8个小时 加上这个时区即是北京时间
- NSTimeInterval inter=[[NSDate date] timeIntervalSinceDate:betweenDate];
- if (inter<60) {//60秒内
- return [NSString stringWithFormat:@"%.0f秒",inter];
- }else if (inter<3600)
- {
- return [NSString stringWithFormat:@"%.0f分",inter/60];//返回分
- }else
- {
- return [NSString stringWithFormat:@"%.0f小时",inter/3600];//返回小时
- }
-
- return @"刚刚";
- }
- @end
|