Skip to content

Commit

Permalink
封装工具优化
Browse files Browse the repository at this point in the history
  • Loading branch information
bingxue314159 committed Jun 27, 2016
1 parent 0a54c26 commit 7a2b18d
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 63 deletions.
6 changes: 6 additions & 0 deletions SuperDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
080069991C0BFE1B00FE9A2A /* TYGSignatureLineView.m in Sources */ = {isa = PBXBuildFile; fileRef = 080069981C0BFE1B00FE9A2A /* TYGSignatureLineView.m */; };
080ACAD81D20CB98000C2350 /* TYGFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 080ACAD71D20CB98000C2350 /* TYGFileManager.m */; };
080E5A071B2191F800F54F44 /* QBPopupMenuDemo.m in Sources */ = {isa = PBXBuildFile; fileRef = 080E5A061B2191F800F54F44 /* QBPopupMenuDemo.m */; };
080E5A091B21923C00F54F44 /* QBPopupMenuDemo.xib in Resources */ = {isa = PBXBuildFile; fileRef = 080E5A081B21923C00F54F44 /* QBPopupMenuDemo.xib */; };
0814C6791B9EDBCC0061BDA3 /* TYGValid.m in Sources */ = {isa = PBXBuildFile; fileRef = 0814C6781B9EDBCC0061BDA3 /* TYGValid.m */; };
Expand Down Expand Up @@ -555,6 +556,8 @@
/* Begin PBXFileReference section */
080069971C0BFE1B00FE9A2A /* TYGSignatureLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYGSignatureLineView.h; sourceTree = "<group>"; };
080069981C0BFE1B00FE9A2A /* TYGSignatureLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYGSignatureLineView.m; sourceTree = "<group>"; };
080ACAD61D20CB98000C2350 /* TYGFileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYGFileManager.h; sourceTree = "<group>"; };
080ACAD71D20CB98000C2350 /* TYGFileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYGFileManager.m; sourceTree = "<group>"; };
080E5A051B2191F800F54F44 /* QBPopupMenuDemo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QBPopupMenuDemo.h; sourceTree = "<group>"; };
080E5A061B2191F800F54F44 /* QBPopupMenuDemo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QBPopupMenuDemo.m; sourceTree = "<group>"; };
080E5A081B21923C00F54F44 /* QBPopupMenuDemo.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = QBPopupMenuDemo.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1403,6 +1406,8 @@
08878EA51D13CA32000AF43A /* TYGNetworkUtility.m */,
0843EED61AE4DB0C00220F15 /* W_R_Plist.h */,
0843EED71AE4DB0C00220F15 /* W_R_Plist.m */,
080ACAD61D20CB98000C2350 /* TYGFileManager.h */,
080ACAD71D20CB98000C2350 /* TYGFileManager.m */,
);
path = TYG_Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -3211,6 +3216,7 @@
0843EEE11AE4DB0C00220F15 /* Utility.m in Sources */,
08D7D98C1AE61DB400543F2A /* SphereViewController.m in Sources */,
08B2A1D61B85AA7700B89635 /* TYGProgressTableViewController.m in Sources */,
080ACAD81D20CB98000C2350 /* TYGFileManager.m in Sources */,
0843EFCC1AE4F62F00220F15 /* kxMenuDemo.m in Sources */,
08F27D4B1B550ABC0039DF49 /* ControlsViewController.m in Sources */,
0843EF811AE4E54700220F15 /* IQActionSheetDemo.m in Sources */,
Expand Down
Binary file not shown.
25 changes: 25 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGFileManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// TYGFileManager.h
// SuperDemo
//
// Created by 谈宇刚 on 16/6/27.
// Copyright © 2016年 TYG. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TYGFileManager : NSObject

/** 磁盘总空间 */
+(CGFloat)diskOfAllSizeMBytes;

/** 获取磁盘可用空间大小 */
+ (CGFloat)diskOfFreeSizeMBytes;

/** 获取指定路径下某个文件的大小 */
+ (long long)fileSizeAtPath:(NSString *)filePath;

/** 获取文件夹下所有文件的大小 */
+ (long long)folderSizeAtPath:(NSString *)folderPath;

@end
83 changes: 83 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGFileManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// TYGFileManager.m
// SuperDemo
//
// Created by 谈宇刚 on 16/6/27.
// Copyright © 2016年 TYG. All rights reserved.
//

#import "TYGFileManager.h"

@implementation TYGFileManager


/** 磁盘总空间 */
+(CGFloat)diskOfAllSizeMBytes{

CGFloat size = 0.0;

NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"ERROR:%@",error.localizedDescription);
#endif
}
else{
NSNumber *number = [dic objectForKey:NSFileSystemSize];
size = [number floatValue]/1024.0/1024.0;
}

return size;
}

/** 获取磁盘可用空间大小 */
+ (CGFloat)diskOfFreeSizeMBytes{

CGFloat size = 0.0;

NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"ERROR:%@",error.localizedDescription);
#endif
}
else{
NSNumber *number = [dic objectForKey:NSFileSystemFreeSize];
size = [number floatValue]/1024.0/1024.0;
}

return size;
}

/** 获取指定路径下某个文件的大小 */
+ (long long)fileSizeAtPath:(NSString *)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
return 0;
}

return [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
}

/** 获取文件夹下所有文件的大小 */
+ (long long)folderSizeAtPath:(NSString *)folderPath{

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:folderPath]) {
return 0;
}

NSString *fileName;
NSEnumerator *filesEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];
long long folderSize = 0;
while ((fileName = [filesEnumerator nextObject]) != nil) {
NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:filePath];
}

return folderSize;
}

@end
26 changes: 26 additions & 0 deletions SuperDemo/Utility/TYG_Utility/TYGNetworkUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import <arpa/inet.h>
#import <SystemConfiguration/SystemConfiguration.h>

#import <ifaddrs.h> //获取设备IP地址

@implementation TYGNetworkUtility

/*
Expand Down Expand Up @@ -108,6 +110,30 @@ + (NSString *) getIPAddressForHost: (NSString *) theHost
return addressString;
}

/** 获取设备IP地址 */
+ (NSString *)getIPAddress{

NSString *address = nil;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) {
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == AF_INET) {
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
temp_addr = temp_addr->ifa_next;
}
}
}
freeifaddrs(interfaces);

return address;
}

/** 检测一个网址是否可以正常访问 */
+ (BOOL) hostAvailable: (NSString *) theHost
{
Expand Down
17 changes: 11 additions & 6 deletions SuperDemo/Utility/TYG_Utility/TYGValid.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
*/
+ (BOOL)isTelphoneNumber:(NSString *)telNum;

/**
* 固定电话号码格式
* 因为固定电话格式比较复杂,情况比较多,主要验证了以下类型
* 如:010-12345678、0912-1234567、(010)-12345678、(0912)1234567、(010)12345678、(0912)-1234567、01012345678、09121234567
*/
+(BOOL)isHomePhoneNumber:(NSString *)sting;

/*
* Email邮箱格式验证
* 如:[email protected][email protected][email protected]
Expand All @@ -46,16 +53,14 @@
+ (BOOL)isIDCard:(NSString *)idCardString;

/**
* 固定电话号码格式
* 因为固定电话格式比较复杂,情况比较多,主要验证了以下类型
* 如:010-12345678、0912-1234567、(010)-12345678、(0912)1234567、(010)12345678、(0912)-1234567、01012345678、09121234567
* 只能是中文汉字
*/
+(BOOL)isPhoneNumber:(NSString *)sting;
+(BOOL)isChinese:(NSString *)sting;

/**
* 只能是中文汉字
* 是否含有中文汉字
*/
+(BOOL)isChinese:(NSString *)sting;
+ (BOOL)isHaveChineseInString:(NSString *)string;

/**
* 验证银行卡号(13位 到 19位)
Expand Down
71 changes: 51 additions & 20 deletions SuperDemo/Utility/TYG_Utility/TYGValid.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,47 @@ +(BOOL)isValidateEmail:(NSString *)email {
+(BOOL)isTelphoneNumber:(NSString *)telNum{

telNum = [telNum stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([telNum length] == 11) {
NSString *telNumRegex = @"^1[3-8]+\\d{9}$";
NSPredicate *telNumTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telNumRegex];
return [telNumTest evaluateWithObject:telNum];
if ([telNum length] != 11) {
return NO;
}

// NSString *telNumRegex = @"^1[3-8]+\\d{9}$";
// NSPredicate *telNumTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telNumRegex];
// return [telNumTest evaluateWithObject:telNum];

//移动号段正则
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
//联通号段正则
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
//电信号段正则
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";

NSPredicate *pred_CM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM_NUM];
NSPredicate *pred_CU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU_NUM];
NSPredicate *pred_CT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT_NUM];
BOOL isMatch_CM = [pred_CM evaluateWithObject:telNum];
BOOL isMatch_CU = [pred_CU evaluateWithObject:telNum];
BOOL isMatch_CT = [pred_CT evaluateWithObject:telNum];
if (isMatch_CM || isMatch_CT || isMatch_CU) {
return YES;
}

return NO;
}

/**
* 固定电话号码格式
* 因为固定电话格式比较复杂,情况比较多,主要验证了以下类型
* 如:010-12345678、0912-1234567、(010)-12345678、(0912)1234567、(010)12345678、(0912)-1234567、01012345678、09121234567
*/
+(BOOL)isHomePhoneNumber:(NSString *)sting{
//^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|(^0\d2-?\d{8}$)|(^0\d3-?\d{7}$)$
sting = [sting stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *stringRegex = @"^(^0\\d{2}-?\\d{8}$)|(^0\\d{3}-?\\d{7,8}$)|(^0\\d2-?\\d{8}$)|(^0\\d3-?\\d{7,8}$)$";
NSPredicate *stringTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
return [stringTest evaluateWithObject:sting];
}

/**
* 利用正则表达式验证是否是数字与字母的组合
*/
Expand All @@ -71,7 +104,6 @@ +(BOOL)isNumber:(NSString *)sting{
return [stringTest evaluateWithObject:sting];
}


/**
* 验证是否是身份证号码
*
Expand Down Expand Up @@ -149,29 +181,28 @@ + (BOOL)isIDCard:(NSString *)idCardString{
return flag;
}


/**
* 固定电话号码格式
* 因为固定电话格式比较复杂,情况比较多,主要验证了以下类型
* 如:010-12345678、0912-1234567、(010)-12345678、(0912)1234567、(010)12345678、(0912)-1234567、01012345678、09121234567
* 只能是中文汉字
*/
+(BOOL)isPhoneNumber:(NSString *)sting{
//^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|(^0\d2-?\d{8}$)|(^0\d3-?\d{7}$)$
sting = [sting stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *stringRegex = @"^(^0\\d{2}-?\\d{8}$)|(^0\\d{3}-?\\d{7,8}$)|(^0\\d2-?\\d{8}$)|(^0\\d3-?\\d{7,8}$)$";
NSPredicate *stringTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
return [stringTest evaluateWithObject:sting];
+(BOOL)isChinese:(NSString *)string{

for (NSInteger i = 0; i < [string length]; i++) {
int a = [string characterAtIndex:i];
if (a < 0x4e00 || a > 0x9fa5) {
return NO;
}
}
return YES;
}

/**
* 只能是中文汉字
* 是否含有中文汉字
*/
+(BOOL)isChinese:(NSString *)sting{
//^[\u4e00-\u9fa5]+$
sting = [sting stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+ (BOOL)isHaveChineseInString:(NSString *)string{
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *stringRegex = @"^[\u4e00-\u9fa5]+$";
NSPredicate *stringTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
return [stringTest evaluateWithObject:sting];
return [stringTest evaluateWithObject:string];
}

/**
Expand Down
8 changes: 8 additions & 0 deletions SuperDemo/Utility/TYG_Utility/UIImage+TYGOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
*/
- (UIImage *)reSizeToSize:(CGSize)reSize;

/**
* 压缩图片到指定文件大小
* @param image 原始图片
* @param size 指定图片物理大小
* @return 图片二进制文件
*/
+ (NSData *)compressOrigianlImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)size;

/**
* 生成纯色图片
* @param color 颜色
Expand Down
26 changes: 26 additions & 0 deletions SuperDemo/Utility/TYG_Utility/UIImage+TYGOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ - (UIImage *)reSizeToSize:(CGSize)reSize{
return reSizeImage;
}

/**
* 压缩图片到指定文件大小
* @param image 原始图片
* @param size 指定图片物理大小
* @return 图片二进制文件
*/
+ (NSData *)compressOrigianlImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)size{

NSData *data = UIImageJPEGRepresentation(image, 1.0);
CGFloat dataKBytes = data.length/1000.0;
CGFloat maxQuality = 0.9f;
CGFloat lastData = dataKBytes;
while (dataKBytes > size && maxQuality > 0.01) {
maxQuality -= 0.01;
data = UIImageJPEGRepresentation(image, maxQuality);
dataKBytes = data.length/1000.0;
if (lastData == dataKBytes) {
break;
}
else{
lastData = dataKBytes;
}
}
return data;
}

/**
* 生成纯色图片
* @param color 颜色
Expand Down
9 changes: 1 addition & 8 deletions SuperDemo/Utility/TYG_Utility/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@
* @param uiDate 输入的日期字符串形如:@"1992-05-21 13:08:08"
* @return NSDate
*/
+ (NSDate *) dateConvertDateFromString:(NSString*)uiDate;
//+ (NSDate *) dateConvertDateFromString:(NSString*)uiDate;

/**
* 解析新浪微博中的日期,@"EEE MMM d HH:mm:ss Z yyyy"
*/
+ (NSString*)getTimeResolveSinaWeiboDate:(NSString*)date;

/**
* 转换时间到当天零点或者最后一秒
* @parma currentDate-传入的时间(e.g. 2014-05-09 18:06:59),timeType ? 0 : 1
* @return 转化好的时间 2014-05-09 00:00:00 | 2014-05-09 23:59:59
**/
+ (NSDate *)dateFromDate:(NSDate *)currentDate timeType:(NSInteger)timeType;

/**
* 计算时间间隔,如:1小时前,1天前,1个月前,1年前
* @parma paramStartDate,paramEndDate-传入的起止时间(e.g. 2014-05-09 18:06:59)
Expand Down
Loading

0 comments on commit 7a2b18d

Please sign in to comment.