- 給一個view截圖
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2. collectionView的內(nèi)容小于其寬高的時候是不能滾動的谣蠢,設(shè)置可以滾動:
collectionView.alwaysBounceHorizontal = YES;
collectionView.alwaysBounceVertical = YES;
3. 設(shè)置navigationBar上的title顏色和大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor youColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}]
4. 顏色轉(zhuǎn)圖片
(CGFloat)getCachSize {
NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義緩存大小
//用枚舉器遍歷 一個文件夾的內(nèi)容
//1.獲取 文件夾枚舉器
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
//2.遍歷
for (NSString *fileName in enumerator) {
NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
count += fileDict.fileSize;//自定義所有緩存大小
}
// 得到是字節(jié) 轉(zhuǎn)化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;
}
5. 獲取APP緩存大小
(CGFloat)getCachSize {
NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//獲取自定義緩存大小
//用枚舉器遍歷 一個文件夾的內(nèi)容
//1.獲取 文件夾枚舉器
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
__block NSUInteger count = 0;
//2.遍歷
for (NSString *fileName in enumerator) {
NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
count += fileDict.fileSize;//自定義所有緩存大小
}
// 得到是字節(jié) 轉(zhuǎn)化為M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;
}
6. 清理APP緩存
- (void)handleClearView {
//刪除兩部分
//1.刪除 sd 圖片緩存
//先清除內(nèi)存中的圖片緩存
[[SDImageCache sharedImageCache] clearMemory];
//清除磁盤的緩存
[[SDImageCache sharedImageCache] clearDisk];
//2.刪除自己緩存
NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
[[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
}```
- 身份證號驗(yàn)證
- (BOOL)validateIdentityCard {
BOOL flag;
if (self.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:self];
}
- UITextView實(shí)現(xiàn)placeHolder占位文字
通過runtime,我們發(fā)現(xiàn)查近,UITextView內(nèi)部有一個名為“_placeHolderLabel”的私有成員變量眉踱。大家知道,Objective-C沒有絕對的私有變量霜威,因?yàn)槲覀兛梢酝ㄟ^KVC來訪問私有變量谈喳。
特點(diǎn):雖然Apple官方?jīng)]有給我們開發(fā)者提供類似于placeholder的屬性,但是通過運(yùn)行時戈泼,我們遍歷出了一個placeHolderLabel的私有變量婿禽。這種方法簡單易懂赏僧,代碼量少,推薦大家使用這種方法扭倾。
UILabel *label =[JDUtils createLabelWithFrame:CGRectZero Font:18 Text:@"請輸入備注"];
[label sizeToFit];
label.textColor = [UIColor lightGrayColor];
[_textView addSubview:label];
[_textView setValue:label forKey:@"_placeholderLabel"];
[self.view addSubview:_textView];
- 刪除NSUserDefaults所有記錄
//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
//方法二
- (void)resetDefaults {
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
[defs removeObjectForKey:key];
}
[defs synchronize];
}
// 方法三
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
- 自定義cell選中背景顏色
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];```
- UIView背景顏色漸變
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[self.view addSubview:view];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];```
- 在非ViewController的地方彈出UIAlertController對話框
// 最好抽成一個分類
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//...
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
}
[rootViewController presentViewController:alertController animated:YES completion:nil];```
- 獲取一個view所屬的控制器
// view分類方法
- (UIViewController *)belongViewController {
for (UIView next = [self superview]; next; next = next.superview) {
UIResponder nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
- UIImage和base64互轉(zhuǎn)
// view分類方法
(NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}(UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}```-
讓推送只有聲音沒有橫幅的方法
把本地推送的alertBody設(shè)置為空字符串
-
去掉icon上的角標(biāo)數(shù)字
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];