【IOS】IOS开发总结,持续更新...

2017-06-27  本文已影响35人  雨影
  1. 给一个view截图

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

2. collectionView的内容小于其宽高的时候是不能滚动的,设置可以滚动:

collectionView.alwaysBounceHorizontal = YES;
collectionView.alwaysBounceVertical = YES;

3. 设置navigationBar上的title颜色和大小

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor youColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}]

4.  颜色转图片

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//获取自定义缓存大小
//用枚举器遍历 一个文件夹的内容
//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;//自定义所有缓存大小
}
// 得到是字节  转化为M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

5. 获取APP缓存大小

(CGFloat)getCachSize {

NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
//获取自定义缓存大小
//用枚举器遍历 一个文件夹的内容
//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;//自定义所有缓存大小
}
// 得到是字节  转化为M
CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
return totalSize;

}

6. 清理APP缓存
- (void)handleClearView {
 //删除两部分
 //1.删除 sd 图片缓存
 //先清除内存中的图片缓存
 [[SDImageCache sharedImageCache] clearMemory];
 //清除磁盘的缓存
 [[SDImageCache sharedImageCache] clearDisk];
 //2.删除自己缓存
 NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
 [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
 }```
  1. 身份证号验证
    - (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];
     }
  1. UITextView实现placeHolder占位文字
    通过runtime,我们发现,UITextView内部有一个名为“_placeHolderLabel”的私有成员变量。大家知道,Objective-C没有绝对的私有变量,因为我们可以通过KVC来访问私有变量。
    特点:虽然Apple官方没有给我们开发者提供类似于placeholder的属性,但是通过运行时,我们遍历出了一个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];
//方法一
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]];
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];```
//  最好抽成一个分类
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分类方法

- UIImage和base64互转

// view分类方法

上一篇下一篇

猜你喜欢

热点阅读