/**
* 0.040000===0.04
* 去掉浮点数后面多余的0
*/
+(NSString*)YhyremoveFloatAllZero:(NSString*)string
{
NSString * testNumber = string;
NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
return outNumber;
}
/**
* 删除字符串内含有的特定字符(由if条件控制)
*
*/
+(NSString *) YhyDeleteStringCharacter:(NSString *)str
{
NSMutableString *str1 = [NSMutableString stringWithString:str];
for (int i = 0; i < str1.length; i++) {
unichar c = [str1 characterAtIndex:i];
NSRange range = NSMakeRange(i, 1);
if (c == '"' || c == '.' || c == ',' || c == '(' || c == ')'|| c == '-'|| c == ' ') { //此处可以是任何字符
[str1 deleteCharactersInRange:range];
--i;
}
}
NSString *newstr = [NSString stringWithString:str1];
return newstr;
}
/**
* MD5对密码加密
*
*/
+(NSString *)YhyMD5WithString:(NSString *)string
{
//首先把OC字符串转成C语言形式的字符串
const char *cStr = [string UTF8String];
//调用函数,进行加密
//参数1:要加密的C语言形式的字符串
//参数2:要加密的字符串长度
//参数3:用来接收加密后的数据指针
unsigned long length = strlen(cStr);
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)length, result);
NSMutableString *secuteString = [NSMutableString string];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[secuteString appendFormat:@"%02X",result[i]];
}
return secuteString;
}
#define CC_MD5_DIGEST_LENGTH 16
+(NSString *)YhyMD5WithStringXiaoxie:(NSString *)string
{
const char *cStr = [string UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
{
[result appendFormat:@"%02x", digest[i]];
}
return [result lowercaseString];
}
/**
* 为Lable,Button,TextFile加边框线
*
*/
+(void)YhySetBordeLab:(UILabel *)labelname
{
labelname.layer.borderColor = [UIColor lightGrayColor].CGColor;
labelname.layer.borderWidth = 1.0;
/**
* 设置四周圆角
*/
labelname.layer.cornerRadius = 5;//圆角半径
labelname.layer.masksToBounds = YES;
}
+(void)YhySetBoadeBtn:(UIButton *)BtnName
{
[BtnName.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[BtnName.layer setBorderWidth:1];
BtnName.layer.cornerRadius = 5;
BtnName.layer.masksToBounds = YES;
}
+(void)YhySetBoadeFile:(UITextField *)file
{
[file.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[file.layer setBorderWidth:1];
file.layer.cornerRadius = 5;
file.layer.masksToBounds = YES;
}
/******label随字体改变宽度******/
+ (CGSize)YhyLabBtnsizeWithString:(NSString *)string font:(UIFont *)font
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)//限制最大高度
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
attributes:@{NSFontAttributeName: font}//传人的字体字典
context:nil];
return rect.size;
}
/**
* 设置圆形按钮,或四角圆弧按钮
*
* @param Btn 按钮本身
* @param radius 按钮本身宽度*0.5
*/
+(void)YhySetCircularBtn:(UIButton *)Btn And:(CGFloat ) radius
{
//30这个数值是圆的半径,只要达到,按钮宽,高的一半,就会产生圆形,利用这个属性,也可设置四角为圆角的按钮
Btn.layer.cornerRadius = radius;
//这个不设置,效果就不会出来
Btn.layer.masksToBounds = YES;
}
/**
* 时间字符串转date
*
* @param timeString 时间字符串
*
* @return 时间date
*/
+(NSDate *)YhytimeStringTransformNsdate:(NSString *) timeString
{
// NSString * javatimeString = [NSString stringWithFormat:@"%@000",timeString];
//字符串转date
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateStyle:NSDateFormatterMediumStyle];
[formater setTimeStyle:NSDateFormatterShortStyle];
//字符串转date总是少8个小时,解决办法
[formater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formater setDateFormat:@"yyyyMMdd"];
NSDate* timeStringdate = [formater dateFromString:timeString];
YhyLog(@"%@",timeStringdate);
return timeStringdate;
}
/**
* date转时间戳
*
* @param timestringDate 时间date
*
* @return 时间戳
*/
+(NSString *)YhyDateTransformSHIjianCHUO:(NSDate *)timestringDate
{
//date转时间戳
NSString *timeCHUO = [NSString stringWithFormat:@"%ld", (long)[timestringDate timeIntervalSince1970]*1000];
return timeCHUO;
}
/**
* 时间字符串直接转时间戳
*
* @param timeString 时间字符串
*
* @return 时间戳
*/
+(NSString *)YhytimeStringTransformSHIjianCHUO:(NSString *)timeString
{
return [YhyToolViewController YhyDateTransformSHIjianCHUO:[YhyToolViewController YhytimeStringTransformNsdate:timeString]];
}
/**
* 时间戳转字符串
*
* @param CHUOstring 时间戳
* @param isjava 是java后台(1,0)
*
* @return 时间字符串
*/
+(NSString *)YhySHIjianCHUOTransformString:(NSString *)CHUOstring andISjava :(BOOL )isjava
{
// 时间戳转时间字符串(更准一点),如果字符串是java(后台)返回的话,需要除1000,ios自己的就不需要了
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *qdatea;
if (isjava) {
qdatea = [NSDate dateWithTimeIntervalSince1970:[CHUOstring doubleValue]/1000];
}else
{
qdatea = [NSDate dateWithTimeIntervalSince1970:[CHUOstring doubleValue]];
}
NSString *timeString = [formatter stringFromDate:qdatea];
return timeString;
}
/**
* 偏好存储,存
*
* @param upString 要存的字符串
* @param Key 钥匙
*/
+(void)YhyNSuserDfalutesUP:(nullable id)upString Forkey :(NSString *)Key
{
NSUserDefaults * YhyDefaluts = [NSUserDefaults standardUserDefaults];
[YhyDefaluts setObject:upString forKey:Key];
/**
* 数据即时写入。
*/
[YhyDefaluts synchronize];
}
/**
*偏好存储,取
*
* @param Key 钥匙
*
* @return 返回之前通过该钥匙存入的字符串
*/
+(nullable id)YhyNSuserDfalutesDownForkey :(NSString *)Key
{
NSUserDefaults * YhyDefaluts = [NSUserDefaults standardUserDefaults];
// NSString *downstring = [YhyDefaluts objectForKey:Key];
return [YhyDefaluts objectForKey:Key];
}
/**
* 弹出键盘,移动视图
*
* @param yhyself 控制器self
*/
+(void)YhyKyeBoardUPMoveView: (UIViewController * )yhyController
{
//键盘高度216
//滑动效果(动画)
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动,以使下面腾出地方用于软键盘的显示
yhyController.view.frame = CGRectMake(0.0f, -216.0f, YhymainscreenSize.width, YhymainscreenSize.height);//64-216
[UIView commitAnimations];
}
/**
* 退出键盘,移动视图
*
* @param yhyself 控制器self
*/
+(void)YhyKyeBoardDownMoveView: (UIViewController * )yhyController
{
//滑动效果
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//恢复屏幕
yhyController.view.frame = CGRectMake(0.0f, 0.0f, YhymainscreenSize.width, YhymainscreenSize.height);//64-216
[UIView commitAnimations];
}
///**
// * 证书加载,https,AFN
// *
// */
//+(AFSecurityPolicy*)YhyAFNcustomSecurityPolicy
//{ // /先导入证书
// NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"doctor" ofType:@"cer"];//证书的路径,证书一定要加入工程
//// YhyLog(@"证书路径%@",cerPath);
// NSData *certData = [NSData dataWithContentsOfFile:cerPath];
//// YhyLog(@"证书%@",certData);
// // AFSSLPinningModeCertificate 使用证书验证模式
// AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//
// // allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO
// // 如果是需要验证自建证书,需要设置为YES
// securityPolicy.allowInvalidCertificates = YES;
//
// //validatesDomainName 是否需要验证域名,默认为YES;
// //假如证书的域名与你请求的域名不一致,需把该项设置为NO;如设成NO的话,即服务器使用其他可信任机构颁发的证书,也可以建立连接,这个非常危险,建议打开。
// //置为NO,主要用于这种情况:客户端请求的是子域名,而证书上的是另外一个域名。因为SSL证书上的域名是独立的,假如证书上注册的域名是www.google.com,那么mail.google.com是无法验证通过的;当然,有钱可以注册通配符的域名*.google.com,但这个还是比较贵的。
// //如置为NO,建议自己添加对应域名的校验逻辑。
// securityPolicy.validatesDomainName = NO;
//
// securityPolicy.pinnedCertificates = @[certData];
// return securityPolicy;
//}
/**
* 正则身份证格式判断
*
*/
+ (BOOL)YhyIDCardNumber:(NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0)
{
flag = NO;
return flag;
}
NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
flag = [identityCardPredicate evaluateWithObject:identityCard];
//如果通过该验证,说明身份证格式正确,但准确性还需计算
if(flag)
{
if(identityCard.length==18)
{
//将前17位加权因子保存在数组里
NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//这是除以11后,可能产生的11位余数、验证码,也保存成数组
NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
//用来保存前17位各自乖以加权因子后的总和
NSInteger idCardWiSum = 0;
for(int i = 0;i < 17;i++)
{
NSInteger subStrIndex = [[identityCard substringWithRange:NSMakeRange(i, 1)] integerValue];
NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
idCardWiSum+= subStrIndex * idCardWiIndex;
}
//计算出校验码所在数组的位置
NSInteger idCardMod=idCardWiSum%11;
//得到最后一位身份证号码
NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2)
{
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
{
return flag;
}
else
{
flag = NO;
return flag;
}
}
}
else
{
flag = NO;
return flag;
}
}
else
{
return flag;
}
}
// 正则判断手机号码地址格式
+ (BOOL)YhyPhoneNumber:(NSString *)mobileNum {
// 电信号段:133/153/180/181/189/177
// 联通号段:130/131/132/155/156/185/186/145/176
// 移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
// 虚拟运营商:170
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];
}
/**
*
* 计算年龄
*/
+ (int)YhyAge:(NSString *)ageString
{
//计算年龄
NSString *birth = ageString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//生日
NSDate *birthDay = [dateFormatter dateFromString:birth];
//当前时间
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSDate *currentDate = [dateFormatter dateFromString:currentDateStr];
// NSLog(@"currentDate %@ birthDay %@",currentDateStr,birth);
NSTimeInterval time=[currentDate timeIntervalSinceDate:birthDay];
int age = ((int)time)/(3600*24*365);
NSLog(@" %d 岁",age);
return age;
}
/**
* 压缩图片
*
* @param image 需要压缩的图片
* @param newSize 图片想要设置的宽高
* @param biLi 压缩力度(0~1,一般使用3~8)
*
* @return 直接返回data数据(这里是用上传图片时做的处理)
*/
+ (NSData *)YhyImageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize yaSuoBiLiFloat:(float)biLi
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, biLi);
}
//计算出大小
+(NSString *)YhyJiSuanFileSizeWithInterge:(NSInteger)size
{
// 1k = 1024, 1m = 1024k
if (size < 1024) {// 小于1k
return [NSString stringWithFormat:@"缓存%ldB",(long)size];
}else if (size < 1024 * 1024){// 小于1m
CGFloat aFloat = size/1024;
return [NSString stringWithFormat:@"缓存%.0fK",aFloat];
}else if (size < 1024 * 1024 * 1024){// 小于1G
CGFloat aFloat = size/(1024 * 1024);
return [NSString stringWithFormat:@"缓存%.1fM",aFloat];
}else{
CGFloat aFloat = size/(1024*1024*1024);
return [NSString stringWithFormat:@"缓存%.1fG",aFloat];
}
}
+(NSString *)YhyBenDiShiJian
{
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
// NSLog(@"dateString:%@",dateString);
return dateString;
}