1.磁盤總空間大小
+?(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/1024;
}
returnsize;
}
2.磁盤可用空間大小
+?(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/1024;
}
returnsize;
}
3.將字符串?dāng)?shù)組按照元素首字母順序進行排序分組
+?(NSDictionary?*)dictionaryOrderByCharacterWithOriginalArray:(NSArray?*)array
{
if(array.count?==?0)?{
returnnil;
}
for(id?obj?in?array)?{
if(![obj?isKindOfClass:[NSStringclass]])?{
returnnil;
}
}
UILocalizedIndexedCollation?*indexedCollation?=?[UILocalizedIndexedCollation?currentCollation];
NSMutableArray?*objects?=?[NSMutableArray?arrayWithCapacity:indexedCollation.sectionTitles.count];
//創(chuàng)建27個分組數(shù)組
for(inti?=?0;?i?<?indexedCollation.sectionTitles.count;?i++)?{
NSMutableArray?*obj?=?[NSMutableArray?array];
[objects?addObject:obj];
}
NSMutableArray?*keys?=?[NSMutableArray?arrayWithCapacity:objects.count];
//按字母順序進行分組
NSInteger?lastIndex?=?-1;
for(inti?=?0;?i?<?array.count;?i++)?{
NSInteger?index?=?[indexedCollation?sectionForObject:array[i]?collationStringSelector:@selector(uppercaseString)];
[[objects?objectAtIndex:index]?addObject:array[i]];
lastIndex?=?index;
}
//去掉空數(shù)組
for(inti?=?0;?i?<?objects.count;?i++)?{
NSMutableArray?*obj?=?objects[i];
if(obj.count?==?0)?{
[objects?removeObject:obj];
}
}
//獲取索引字母
for(NSMutableArray?*obj?in?objects)?{
NSString?*str?=?obj[0];
NSString?*key?=?[self?firstCharacterWithString:str];
[keys?addObject:key];
}
NSMutableDictionary?*dic?=?[NSMutableDictionary?dictionary];
[dic?setObject:objects?forKey:keys];
returndic;
}
4.將字符串?dāng)?shù)組按照元素首字母順序進行排序分組
+?(NSDictionary?*)dictionaryOrderByCharacterWithOriginalArray:(NSArray?*)array
{
if(array.count?==?0)?{
returnnil;
}
for(id?obj?in?array)?{
if(![obj?isKindOfClass:[NSStringclass]])?{
returnnil;
}
}
UILocalizedIndexedCollation?*indexedCollation?=?[UILocalizedIndexedCollation?currentCollation];
NSMutableArray?*objects?=?[NSMutableArray?arrayWithCapacity:indexedCollation.sectionTitles.count];
//創(chuàng)建27個分組數(shù)組
for(inti?=?0;?i?<?indexedCollation.sectionTitles.count;?i++)?{
NSMutableArray?*obj?=?[NSMutableArray?array];
[objects?addObject:obj];
}
NSMutableArray?*keys?=?[NSMutableArray?arrayWithCapacity:objects.count];
//按字母順序進行分組
NSInteger?lastIndex?=?-1;
for(inti?=?0;?i?<?array.count;?i++)?{
NSInteger?index?=?[indexedCollation?sectionForObject:array[i]?collationStringSelector:@selector(uppercaseString)];
[[objects?objectAtIndex:index]?addObject:array[i]];
lastIndex?=?index;
}
//去掉空數(shù)組
for(inti?=?0;?i?<?objects.count;?i++)?{
NSMutableArray?*obj?=?objects[i];
if(obj.count?==?0)?{
[objects?removeObject:obj];
}
}
//獲取索引字母
for(NSMutableArray?*obj?in?objects)?{
NSString?*str?=?obj[0];
NSString?*key?=?[self?firstCharacterWithString:str];
[keys?addObject:key];
}
NSMutableDictionary?*dic?=?[NSMutableDictionary?dictionary];
[dic?setObject:objects?forKey:keys];
returndic;
}
5.對圖片進行濾鏡處理
//?懷舊?-->?CIPhotoEffectInstant?????????????????????????單色?-->?CIPhotoEffectMono
//?黑白?-->?CIPhotoEffectNoir????????????????????????????褪色?-->?CIPhotoEffectFade
//?色調(diào)?-->?CIPhotoEffectTonal???????????????????????????沖印?-->?CIPhotoEffectProcess
//?歲月?-->?CIPhotoEffectTransfer????????????????????????鉻黃?-->?CIPhotoEffectChrome
//?CILinearToSRGBToneCurve,?CISRGBToneCurveToLinear,?CIGaussianBlur,?CIBoxBlur,?CIDiscBlur,?CISepiaTone,?CIDepthOfField
+?(UIImage?*)filterWithOriginalImage:(UIImage?*)image?filterName:(NSString?*)name
{
CIContext?*context?=?[CIContext?contextWithOptions:nil];
CIImage?*inputImage?=?[[CIImage?alloc]?initWithImage:image];
CIFilter?*filter?=?[CIFilter?filterWithName:name];
[filter?setValue:inputImage?forKey:kCIInputImageKey];
CIImage?*result?=?[filter?valueForKey:kCIOutputImageKey];
CGImageRef?cgImage?=?[context?createCGImage:result?fromRect:[result?extent]];
UIImage?*resultImage?=?[UIImage?imageWithCGImage:cgImage];
CGImageRelease(cgImage);
returnresultImage;
}
6.對圖片進行模糊處理
//?CIGaussianBlur?--->?高斯模糊
//?CIBoxBlur??????--->?均值模糊(Available?in?iOS?9.0?and?later)
//?CIDiscBlur?????--->?環(huán)形卷積模糊(Available?in?iOS?9.0?and?later)
//?CIMedianFilter?--->?中值模糊,?用于消除圖像噪點,?無需設(shè)置radius(Available?in?iOS?9.0?and?later)
//?CIMotionBlur???--->?運動模糊,?用于模擬相機移動拍攝時的掃尾效果(Available?in?iOS?9.0?and?later)
+?(UIImage?*)blurWithOriginalImage:(UIImage?*)image
blurName:(NSString?*)name
radius:(NSInteger)radius
{
CIContext?*context?=?[CIContext?contextWithOptions:nil];
CIImage?*inputImage?=?[[CIImage?alloc]?initWithImage:image];
CIFilter?*filter;
if(name.length?!=?0)?{
filter?=?[CIFilter?filterWithName:name];
[filter?setValue:inputImage?forKey:kCIInputImageKey];
if(![name?isEqualToString:@"CIMedianFilter"])?{
[filter?setValue:@(radius)?forKey:@"inputRadius"];
}
CIImage?*result?=?[filter?valueForKey:kCIOutputImageKey];
CGImageRef?cgImage?=?[context?createCGImage:result?fromRect:[result?extent]];
UIImage?*resultImage?=?[UIImage?imageWithCGImage:cgImage];
CGImageRelease(cgImage);
returnresultImage;
}else{
returnnil;
}
}
7.跳轉(zhuǎn)到系統(tǒng)的相關(guān)界面:
/*
*??需要添加一個字段
*??藍色的項目工程文件?->?Info?->?URL?Types?->?添加一個?->?設(shè)置URL??Sch****?為?prefs的url
NSURL?*url?=?[NSURL?URLWithString:@"prefs:root=WIFI"];
[[UIApplication?sharedApplication]?openURL:url];
跳轉(zhuǎn)到其他的界面的字段(不全,詳細看鏈接)
About?—?prefs:root=General&path=About
Accessibility?—?prefs:root=General&path=ACCESSIBILITY
AirplaneModeOn—?prefs:root=AIRPLANE_MODE
Auto-Lock?—?prefs:root=General&path=AUTOLOCK
Brightness?—?prefs:root=Brightness
Bluetooth?—?prefs:root=General&path=Bluetooth
Date&?Time?—?prefs:root=General&path=DATE_AND_TIME
FaceTime?—?prefs:root=FACETIME
General—?prefs:root=General
原文鏈接:http://www.reibang.com/p/19602f48309b
*/
8.創(chuàng)建一張實時模糊效果?View?(毛玻璃效果)
//Avilable?in?iOS?8.0?and?later
+?(UIVisualEffectView?*)effectViewWithFrame:(CGRect)frame
{
UIBlurEffect?*effect?=?[UIBlurEffect?effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView?*effectView?=?[[UIVisualEffectView?alloc]?initWithEffect:effect];
effectView.frame?=?frame;
returneffectView;
}
9.設(shè)置Label的行間距
+?(void)setLineSpaceWithString:(UILabel?*)label
{
NSMutableAttributedString?*attributedString?=
[[NSMutableAttributedString?alloc]?initWithString:label.text];
NSMutableParagraphStyle?*paragraphStyle?=??[[NSMutableParagraphStyle?alloc]?init];
[paragraphStyle?setLineSpacing:3];
//調(diào)整行間距
[attributedString?addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0,?[label.text?length])];
label.attributedText?=?attributedString;
}
10.讓Plain風(fēng)格的TableView的區(qū)頭可以”不懸涂闹纾”(可以直接百度搜到):
-?(void)scrollViewDidScroll:(UIScrollView?*)scrollView
{
if(scrollView?==?self.myTab)?{
CGFloat?sectionHeaderHeight?=?40;
if(scrollView.contentOffset.y=0)?{
scrollView.contentInset?=?UIEdgeInsetsMake(-scrollView.contentOffset.y,?0,?0,?0);
}elseif(scrollView.contentOffset.y>=sectionHeaderHeight)?{
scrollView.contentInset?=?UIEdgeInsetsMake(-sectionHeaderHeight,?0,?0,?0);
}
}
}
11. 判斷手機號碼格式是否正確悲酷,利用正則表達式驗證
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
if (mobileNum.length != 11)
{
return NO;
}
/**
* 手機號碼:
* 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
* 移動號段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
* 聯(lián)通號段: 130,131,132,155,156,185,186,145,176,1709
* 電信號段: 133,153,180,181,189,177,1700
*/
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$";
/**
* 中國移動:China Mobile
* 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
*/
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)";
/**
* 中國聯(lián)通:China Unicom
* 130,131,132,155,156,185,186,145,176,1709
*/
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)";
/**
* 中國電信:China Telecom
* 133,153,180,181,189,177,1700
*/
NSString *CT = @"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
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] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
}
12. 判斷郵箱格式是否正確,利用正則表達式驗證
+ (BOOL)isAvailableEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
13. 判斷字符串中是否含有空格
+ (BOOL)isHaveSpaceInString:(NSString *)string{
NSRange _range = [string rangeOfString:@" "];
if (_range.location != NSNotFound) {
return YES;
}else {
return NO;
}
}
14. 判斷字符串中是否含有中文
+ (BOOL)isHaveChineseInString:(NSString *)string
{
for(NSInteger i = 0; i < [string length]; i++){
int a = [string characterAtIndex:i];
if (a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
15. 判斷字符串是否全部為數(shù)字
+ (BOOL)isAllNum:(NSString *)string{? ? unichar c;? ? for (int i=0; i<="" pre="">
判斷是否是純數(shù)字
+ (BOOL)isPureInteger:(NSString *)str {
NSScanner *scanner = [NSScanner scannerWithString:str];
NSInteger val;
return [scanner scanInteger:&val] && [scanner isAtEnd];
}
16. 過濾一些特殊字符 似乎只能去除頭尾的特殊字符(不準)
+ (NSString *)filterSpecialWithString:(NSString *)string
{
// 定義一個特殊字符的集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:痊远;: ;()?「」"、[]{}#%-*+=_|~<>$?^?'@#$%^&*()_+'"];
// 過濾字符串的特殊字符
NSString *newString = [string stringByTrimmingCharactersInSet:set];
return newString;
}
17. 讓iOS應(yīng)用直接退出
+ (void)backOutApp {
UIWindow *window = [[UIApplication sharedApplication].delegate window];
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
} completion:^(BOOL finished) {
exit(0);
}];
}
18. NSArray 快速求總和、最大值、最小值感论、平均值
+ (NSString *)caculateArray:(NSArray *)array
{
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%fn%fn%fn%f",sum,avg,max,min);
return [NSString stringWithFormat:@"%f",sum];
}
19. 驗證身份證(本人試過,還挺準的)
+ (BOOL)checkIdentityCardNo:(NSString*)value {
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSInteger length =0;
if (!value) {
return NO;
}else {
length = value.length;
if (length !=15 && length !=18) {
return NO;
}
}
// 省份代碼
NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
NSString *valueStart2 = [value substringToIndex:2];
BOOL areaFlag =NO;
for (NSString *areaCode in areasArray) {
if ([areaCode isEqualToString:valueStart2]) {
areaFlag =YES;
break;
}
}
if (!areaFlag) {
return false;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
NSInteger year =0;
switch (length) {
case 15:
year = [[value substringWithRange:NSMakeRange(6,2)] integerValue] +1900;
if (year %4 ==0 || (year 0 ==0 && year %4 ==0)) {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
options:NSRegularExpressionCaseInsensitive
error:nil];//測試出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
options:NSRegularExpressionCaseInsensitive
error:nil];//測試出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:NSMatchingReportProgress
range:NSMakeRange(0, value.length)];
if(numberofMatch >0) {
return YES;
}else {
return NO;
}
case 18:
year = [value substringWithRange:NSMakeRange(6,4)].intValue;
if (year %4 ==0 || (year 0 ==0 && year %4 ==0)) {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
options:NSRegularExpressionCaseInsensitive? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:nil];//測試出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:NSRegularExpressionCaseInsensitive? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:nil];//測試出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:NSMatchingReportProgress? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, value.length)];
if(numberofMatch >0) {
int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
int Y = S ;
NSString *M =@"F";
NSString *JYM =@"10X98765432";
M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判斷校驗位
if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
return YES;// 檢測ID的校驗位
}else {
return NO;
}
}else {
return NO;
}
default:
return false;
}
}
20.?設(shè)置Label里的字符有不同的顏色
//可根據(jù)自己的需求進行增刪改
-?(void)stringColorSet?{
NSString*string?=?@"如何使得Label里的字符有不同的顏色?";
NSRange?range?=?[string?rangeOfString:?@"Label"];
NSMutableAttributedString*attribute?=?[[NSMutableAttributedString?alloc]?initWithString:?string];
[attribute?addAttributes:?@{NSForegroundColorAttributeName:?[UIColor?redColor]}range:?range];
[attribute?addAttributes:?@{NSForegroundColorAttributeName:?[UIColor?greenColor]}range:?NSMakeRange(0,?range.location)];
[attribute?addAttributes:?@{NSForegroundColorAttributeName:?[UIColor?cyanColor]}range:?NSMakeRange(range.location+?range.length,?5)];
UILabel?*label?=?[[UILabel?alloc]?initWithFrame:?CGRectMake(0.0f,?100.0f,?320.0f,?100.0f)];
[label?setText:?string];
[label?setAttributedText:?attribute];
}
如果上面的文章對您在開發(fā)中能派上用場贮尖,請關(guān)注或點個贊吧笛粘,我會不定期的給您分享或推薦一些好的文章哦^_^