iOS 開發 一些常用的公共方法
1、 擷取磁盤空間大小
// 擷取磁盤空間大小
+ (CGFloat)diskOfAllSizeMBytes{
CGFloat size = ;
NSError * error;
NSDictionary * dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
NSLog(@"error : %@",error.localizedDescription);
}else{
NSNumber * number = [dict objectForKey:NSFileSize];
size = [number floatValue]//;
}
return size;
}
2、擷取磁盤可用空間大小
// 擷取磁盤可用空間大小
+ (CGFloat)diskOfFreeSizeMBytes{
CGFloat size = ;
NSError * error;
NSDictionary * dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
NSLog(@"error : %@",error.localizedDescription);
}else{
NSNumber * number = [dict objectForKey:NSFileSystemFreeSize];
size = [number floatValue]//;
}
return size;
}
3、擷取指定路徑下某個檔案的大小
// 擷取指定路徑下某個檔案的大小
+ (long long)fileSizeAtPath:(NSString *)filePath{
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) return ;
return [[fileManager attributesOfFileSystemForPath:filePath error:nil] fileSize];
}
4、擷取檔案夾下所有檔案大小
// 擷取檔案4、擷取檔案夾下所有檔案大小夾下所有檔案大小
+ (long long)folderSizeAtPath:(NSString *)folderPath{
NSFileManager * fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:folderPath]) return ;
NSEnumerator * fileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];
NSString * fileName;
long long folerSize = ;
while ((fileName = [fileEnumerator nextObject]) != nil) {
NSString * filepath = [folderPath stringByAppendingPathComponent:fileName];
folerSize += [self fileSizeAtPath:filepath];
}
return folerSize;
}
5、擷取字元串(或漢字)首字母
// 擷取字元串(或漢字)首字母
+ (NSString *)firstCharacterWithString:(NSString *)string{
NSMutableString * str = [NSMutableString stringWithString:string];
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
NSString * pingyin = [str capitalizedString];
return [pingyin substringFromIndex:];
}
6、将字元串數組按照元素首字母順序進行排序分組
// 将字元串數組按照元素首字母順序進行排序分組
+ (NSDictionary *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array{
if (array.count == ) {
return nil;
}
for (id obj in array) {
if (![obj isKindOfClass:[NSString class]]) {
return nil;
}
}
UILocalizedIndexedCollation * indexedCollation = [UILocalizedIndexedCollation currentCollation];
NSMutableArray * objects = [NSMutableArray arrayWithCapacity:indexedCollation.sectionTitles.count];
// 建立27個分組數組
for (int i = ; i < indexedCollation.sectionTitles.count; i++ ) {
NSMutableArray * obj = [NSMutableArray array];
[objects addObject:obj];
}
NSMutableArray * keys = [NSMutableArray arrayWithCapacity:objects.count];
// 按字母順序進行分組
NSInteger lastIndex = -;
for (int i = ; i < array.count; i++) {
NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
[[objects objectAtIndex:index] addObject:array[i]];
lastIndex = index;
}
// 去掉空數組
for (int i = ; i < objects.count; i++) {
NSMutableArray * obj = objects[i];
if (obj.count == ) {
[objects removeObject:obj];
}
}
// 擷取索引字母
for (NSMutableArray * obj in objects) {
NSString * str = obj[];
NSString * key = [self firstCharacterWithString:str];// 擷取字元串(或漢字)首字母,見5.
[keys addObject:key];
}
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setObject:objects forKey:keys];
return dict;
}
7、判斷手機号碼格式是否正确
// 判斷手機号碼格式是否正确
+ (BOOL)valiMoblle:(NSString *)mobile{
mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
if (mobile.length != ) {
return NO;
}else{
/* 移動号碼正規表達式 */
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 * pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate * pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate * pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
return YES;
}else
{
return NO;
}
}
}
8、 判斷郵箱格式是否正确
// 判斷郵箱格式是否正确
+ (BOOL)isAvailableEmail:(NSString *)email{
NSString * emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate * emaileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL reg = [emaileTest evaluateWithObject:email];
return reg;
}
9、将十六進制顔色轉換為 UIColor 對象
// 将十六進制顔色轉換為 UIColor 對象
+ (UIColor *)colorWithHexString:(NSString *)color{
NSString * cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// string should be 6 or 8 characters
if ([cString length] < ) {
return [UIColor clearColor];
}
// strip "0X" or "#" if it appears
if ([cString hasPrefix:@"0X"]) {
cString = [cString substringFromIndex:];
}
if ([cString hasPrefix:@"#"]) {
cString = [cString substringFromIndex:];
}
if ([cString length] != ) {
return [UIColor clearColor];
}
// Separate into r,g,b substrings
NSRange range;
range.location = ;
range.length = ;
// r
NSString * rString = [cString substringWithRange:range];
// g
range.location = ;
NSString * gString = [cString substringWithRange:range];
// b
range.location = ;
NSString * bString = [cString substringWithRange:range];
// Scan values
unsigned int r,g,b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / f) green:((float) g / f) blue:((float) b / f) alpha:f];
}
10、 全屏截圖
// 全屏截圖
+ (UIImage *)shotScreen{
UIWindow * windou = [UIApplication sharedApplication].keyWindow;
UIGraphicsBeginImageContext(windou.bounds.size);
[windou.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
github源代碼位址:https://github.com/wangjianchao1990/iOS—