//根据日期算出周几
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate
{
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
//转化为时间字符串
+ (NSString *)dateStringFromNumberTimer:(NSString *)timerStr {
//转化为Double
double t = [timerStr doubleValue];
//计算出距离1970的NSDate
NSDate *date = [NSDate dateWithTimeIntervalSince1970:t];
//转化为 时间格式化字符串
//NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//转化为 时间字符串
return [df stringFromDate:date];
}
//动态 计算行高
//根据字符串的实际内容的多少 在固定的宽度和字体的大小,动态的计算出实际的高度
+ (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size{
if ([MZLUtility getCurrentIOS] >= 7.0) {
//iOS7之后
/*
第一个参数: 预设空间 宽度固定 高度预设 一个最大值
第二个参数: 行间距 如果超出范围是否截断
第三个参数: 属性字典 可以设置字体大小
*/
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:size]};
CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
//返回计算出的行高
return rect.size.height;
}else {
//iOS7之前
/*
1.第一个参数 设置的字体固定大小
2.预设 宽度和高度 宽度是固定的 高度一般写成最大值
3.换行模式 字符换行
*/
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:CGSizeMake(textWidth, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];
return textSize.height;//返回 计算出得行高
}
}
//获取iOS版本号
+ (double)getCurrentIOS {
return [[[UIDevice currentDevice] systemVersion] doubleValue];
}
+ (CGSize)getScreenSize {
return [[UIScreen mainScreen] bounds].size;
}
//获得当前系统时间到指定时间的时间差字符串,传入目标时间字符串和格式
+(NSString*)stringNowToDate:(NSString*)toDate formater:(NSString*)formatStr
{
NSDateFormatter *formater=[[NSDateFormatter alloc] init];
if (formatStr) {
[formater setDateFormat:formatStr];
}
else{
[formater setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]];
}
NSDate *date=[formater dateFromString:toDate];
return [self stringNowToDate:date];
}
//获得到指定时间的时间差字符串,格式在此方法内返回前自己根据需要格式化
+(NSString*)stringNowToDate:(NSDate*)toDate
{
//创建日期 NSCalendar对象
NSCalendar *cal = [NSCalendar currentCalendar];
//得到当前时间
NSDate *today = [NSDate date];
//用来得到具体的时差,位运算
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ;
if (toDate && today) {//不为nil进行转化
NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:toDate options:0 ];
//NSString *dateStr=[NSString stringWithFormat:@"%d年%d月%d日%d时%d分%d秒",[d year],[d month], [d day], [d hour], [d minute], [d second]];
NSString *dateStr=[NSString stringWithFormat:@"%02ld:%02ld:%02ld",[d hour], [d minute], [d second]];
return dateStr;
}
return @"";
}
//获取 一个文件 在沙盒Library/Caches/ 目录下的路径
+ (NSString *)getFullPathWithFile:(NSString *)urlName {
//先获取 沙盒中的Library/Caches/路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *myCacheDirectory = [docPath stringByAppendingPathComponent:@"MyCaches"];
//检测MyCaches 文件夹是否存在
if (![[NSFileManager defaultManager] fileExistsAtPath:myCacheDirectory]) {
//不存在 那么创建
[[NSFileManager defaultManager] createDirectoryAtPath:myCacheDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
//用md5进行 加密 转化为 一串十六进制数字 (md5加密可以把一个字符串转化为一串唯一的用十六进制表示的串)
NSString * newName = MD5Hash(urlName);
//拼接路径
return [myCacheDirectory stringByAppendingPathComponent:newName];
}
//检测 缓存文件 是否超时
+ (BOOL)isTimeOutWithFile:(NSString *)filePath timeOut:(double)timeOut {
//获取文件的属性
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
//获取文件的上次的修改时间
NSDate *lastModfyDate = fileDict.fileModificationDate;
//算出时间差 获取当前系统时间 和 lastModfyDate时间差
NSTimeInterval sub = [[NSDate date] timeIntervalSinceDate:lastModfyDate];
if (sub < 0) {
sub = -sub;
}
//比较是否超时
if (sub > timeOut) {
//如果时间差 大于 设置的超时时间 那么就表示超时
return YES;
}
return NO;
}
//清除缓存
+ (void) resetCache {
[[NSFileManager defaultManager] removeItemAtPath:[MZLCache cacheDirectory] error:nil];
}
//缓存目录
+ (NSString*) cacheDirectory {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MZLCaches"];
return cacheDirectory;
}
//删除指定的缓存
+ (NSData*) objectForKey:(NSString*)key {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filename = [self.cacheDirectory stringByAppendingPathComponent:key];
if ([fileManager fileExistsAtPath:filename])
{
NSDate *modificationDate = [[fileManager attributesOfItemAtPath:filename error:nil] objectForKey:NSFileModificationDate];
if ([modificationDate timeIntervalSinceNow] > cacheTime) {
[fileManager removeItemAtPath:filename error:nil];
} else {
NSData *data = [NSData dataWithContentsOfFile:filename];
return data;
}
}
return nil;
}
//创建指定缓存
+ (void) setObject:(NSData*)data forKey:(NSString*)key {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filename = [self.cacheDirectory stringByAppendingPathComponent:key];
BOOL isDir = YES;
if (![fileManager fileExistsAtPath:self.cacheDirectory isDirectory:&isDir]) {
[fileManager createDirectoryAtPath:self.cacheDirectory withIntermediateDirectories:NO attributes:nil error:nil];
}
NSError *error;
@try {
[data writeToFile:filename options:NSDataWritingAtomic error:&error];
}
@catch (NSException * e) {
//TODO: error handling maybe
}
}
// 获取某个路径下文件大小
+ (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
// 获取缓存大小
+ (float ) folderSizeAtPath:(NSString*) folderPath{
NSFileManager* manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:folderPath])
return 0;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [MZLCache fileSizeAtPath:fileAbsolutePath];
}
return folderSize/(1024.0*1024.0);
}
//MD5加密字符串
NSString * MD5Hash(NSString *aString) {
const char *cStr = [aString 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]];
}
/**
* 手机号码验证
*
* @param mobileNumbel 传入的手机号码
*
* @return 格式正确返回true 错误 返回fals
*/
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189,181(增加)
* 新增:14_,17_
*/
NSString * MOBIL = @"^1(3[0-9]|4[0-9]|5[0-35-9]|7[0-9]|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189,181(增加)
22 */
NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:mobileNum]
|| [regextestcm evaluateWithObject:mobileNum]
|| [regextestct evaluateWithObject:mobileNum]
|| [regextestcu evaluateWithObject:mobileNum])) {
return YES;
}
return NO;
}
/**
* 密码验证
*
* @param password 传入的密码 6-16位 大小写数字
*
* @return 格式正确返回true 错误 返回fals
*/
+ (BOOL)isPassword:(NSString *)password
{
NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
initWithPattern:@"^[a-zA-Z0-9]{6,16}$"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:password
options:NSMatchingReportProgress
range:NSMakeRange(0, password.length)];
if(numberofMatch > 0)
return YES;
return NO;
}
/**
* 邮箱验证
*
* @param email 传入的邮箱
*
* @return 格式正确返回true 错误 返回fals
*/
+ (BOOL) isEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if ([emailTest evaluateWithObject:email]) {
return YES;
}
return NO;
}
/**
* 身份证号验证
*
* @param identityCard 传入的身份证号
*
* @return 格式正确返回true 错误 返回fals
*/
+ (BOOL) isIdentityCard: (NSString *)identityCard
{
NSString *idnum = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",idnum];
return [identityCardPredicate evaluateWithObject:identityCard];
}
/** 银行卡号有效性问题Luhn算法
* 现行 16 位银联卡现行卡号开头 6 位是 622126~622925 之间的,7 到 15 位是银行自定义的,
* 可能是发卡分行,发卡网点,发卡序号,第 16 位是校验码。
* 16 位卡号校验位采用 Luhm 校验方法计算:
* 1,将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2
* 2,将奇位乘积的个十位全部相加,再加上所有偶数位上的数字
* 3,将加法和加上校验位能被 10 整除。
*/
+ (BOOL) isBankCardluhmCheck:(NSString *)bankNumber
{
NSString * lastNum = [[bankNumber substringFromIndex:(bankNumber.length-1)] copy];//取出最后一位
NSString * forwardNum = [[bankNumber substringToIndex:(bankNumber.length -1)] copy];//前15或18位
NSMutableArray * forwardArr = [[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<forwardNum.length; i++) {
NSString * subStr = [forwardNum substringWithRange:NSMakeRange(i, 1)];
[forwardArr addObject:subStr];
}
NSMutableArray * forwardDescArr = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = (int)(forwardArr.count-1); i> -1; i--) {//前15位或者前18位倒序存进数组
[forwardDescArr addObject:forwardArr[i]];
}
NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];//奇数位*2的积 < 9
NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];//奇数位*2的积 > 9
NSMutableArray * arrEvenNum = [[NSMutableArray alloc] initWithCapacity:0];//偶数位数组
for (int i=0; i< forwardDescArr.count; i++) {
NSInteger num = [forwardDescArr[i] intValue];
if (i%2) {//偶数位
[arrEvenNum addObject:[NSNumber numberWithInteger:num]];
}else{//奇数位
if (num * 2 < 9) {
[arrOddNum addObject:[NSNumber numberWithInteger:num * 2]];
}else{
NSInteger decadeNum = (num * 2) / 10;
NSInteger unitNum = (num * 2) % 10;
[arrOddNum2 addObject:[NSNumber numberWithInteger:unitNum]];
[arrOddNum2 addObject:[NSNumber numberWithInteger:decadeNum]];
}
}
}
__block NSInteger sumOddNumTotal = 0;
[arrOddNum enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
sumOddNumTotal += [obj integerValue];
}];
__block NSInteger sumOddNum2Total = 0;
[arrOddNum2 enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
sumOddNum2Total += [obj integerValue];
}];
__block NSInteger sumEvenNumTotal = 0 ;
[arrEvenNum enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
sumEvenNumTotal += [obj integerValue];
}];
NSInteger lastNumber = [lastNum integerValue];
NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal;
return (luhmTotal%10 ==0)?YES:NO;
}
//验证手机号的合法性
+(BOOL)isValidateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
// NSLog(@"phoneTest is %@",phoneTest);
return [phoneTest evaluateWithObject:mobile];
}
/**
* 验证车牌号
*/
+ (BOOL) checkCarNumber:(NSString *) CarNumber
{
NSString *bankNum = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",bankNum];
BOOL isMatch = [pred evaluateWithObject:CarNumber];
return isMatch;
}
/**
* 验证17位车架号
*/
+ (BOOL) checkCheJiaNumber:(NSString *) CheJiaNumber
{
NSString *bankNum=@"^(\\d{17})$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",bankNum];
BOOL isMatch = [pred evaluateWithObject:CheJiaNumber];
return isMatch;
}
/**
* 验证只能输入数字和字母
*/
+ (BOOL) checkTeshuZifuNumber:(NSString *) CheJiaNumber
{
NSString *bankNum=@"^[A-Za-z0-9]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",bankNum];
BOOL isMatch = [pred evaluateWithObject:CheJiaNumber];
return isMatch;
}
/**
* 验证以C开头字符
*/
+ (BOOL) checkCtooNumber:(NSString *) nickNumber{
NSString *nickNum=@"^C{1}[0-9]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nickNum];
BOOL isMatch = [pred evaluateWithObject:nickNumber];
return isMatch;
}
/**
* 验证以C开头的18位字符
*/
+ (BOOL) checkCtooNumberTo18:(NSString *) nickNumber
{
NSString *nickNum=@"^C{1}[0-9]{18}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nickNum];
BOOL isMatch = [pred evaluateWithObject:nickNumber];
return isMatch;
}
/**
* 验证昵称
*/
+ (BOOL) checkNickname:(NSString *) nickname
{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
BOOL isMatch = [pred evaluateWithObject:nickname];
return isMatch;
}
/**
* 验证URL
*/
+ (BOOL)checkURL : (NSString *) url
{
NSString *pattern = @"^[0-9A-Za-z]{1,50}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:url];
return isMatch;
}
/**
* 验证员工号,12位的数字
*/
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
NSString *pattern = @"^[0-9]{12}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:number];
return isMatch;
}
/**
* 验证用户姓名,20位的中文或英文
*/
+ (BOOL)checkUserName : (NSString *) userName
{
// NSString *pattern = @"^[A-Za-z0-9]{6,20}+$";
NSString *pattern = @"^([\u4e00-\u9fa5]+|([a-zA-Z]+\s?)+)$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:userName];
return isMatch;
}
/**
* model转字典
*/
- (NSDictionary *)dictionaryFromModel
{
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:count];
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
id value = [self valueForKey:key];
//only add it to dictionary if it is not nil
if (key && value) {
if ([value isKindOfClass:[NSString class]]
|| [value isKindOfClass:[NSNumber class]]) {
// 普通类型的直接变成字典的值
[dict setObject:value forKey:key];
}
else if ([value isKindOfClass:[NSArray class]]
|| [value isKindOfClass:[NSDictionary class]]) {
// 数组类型或字典类型
[dict setObject:[self idFromObject:value] forKey:key];
}
else {
// 如果model里有其他自定义模型,则递归将其转换为字典
[dict setObject:[value dictionaryFromModel] forKey:key];
}
} else if (key && value == nil) {
// 如果当前对象该值为空,设为nil。在字典中直接加nil会抛异常,需要加NSNull对象
[dict setObject:[NSNull null] forKey:key];
}
}
free(properties);
return dict;
}
/**
* 字典 转 model
*/
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary
{
id obj = [[self alloc] init];
// 获取所有的成员变量
unsigned int count;
Ivar *ivars = class_copyIvarList(self, &count);
for (unsigned int i = 0; i < count; i++)
{
Ivar ivar = ivars[i];
// 取出的成员变量,去掉下划线
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
NSString *key = [ivarName substringFromIndex:1];
id value = dictionary[key];
// 当这个值为空时,判断一下是否执行了replacedKeyFromPropertyName协议,如果执行了替换原来的key查值
if (!value)
{
if ([self respondsToSelector:@selector(replacedKeyFromPropertyName)])
{
NSString *replaceKey = [self replacedKeyFromPropertyName][key];
value = dictionary[replaceKey];
}
}
// 字典嵌套字典
if ([value isKindOfClass:[NSDictionary class]])
{
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
NSRange range = [type rangeOfString:@"\""];
type = [type substringFromIndex:range.location + range.length];
range = [type rangeOfString:@"\""];
type = [type substringToIndex:range.location];
Class modelClass = NSClassFromString(type);
if (modelClass)
{
value = [modelClass objectWithDictionary:value];
}
}
// 字典嵌套数组
if ([value isKindOfClass:[NSArray class]])
{
if ([self respondsToSelector:@selector(objectClassInArray)])
{
NSMutableArray *models = [NSMutableArray array];
NSString *type = [self objectClassInArray][key];
Class classModel = NSClassFromString(type);
for (NSDictionary *dict in value)
{
id model = [classModel objectWithDictionary:dict];
[models addObject:model];
}
value = models;
}
}
if (value)
{
[obj setValue:value forKey:key];
}
}
// 释放ivars
free(ivars);
return obj;
}
网友评论