iOS基础:每次都记不起来的代码(持续更新)

1. 本地路径获取

a 总路径

NSString *homeDir = NSHomeDirectory();

b Documents 目录:这个目录用于存储用户数据或其它应该定期备份的信息。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];

c Library 目录:这个目录下有两个子目录:Caches 和 Preferences

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];

c.1 Preferences 目录:包含应用程序的偏好设置文件。不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好。

[[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] stringArrayForKey:defaultName];

c.2 Caches 目录:用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];

d tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。

NSString *tmpDir = NSTemporaryDirectory();

2. 回到主线程的三种方法

a NSThread

[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];

- (void)updateUI {
    // UI
}

b NSOperationQueue

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // UI
}];

c GCD

dispatch_async(dispatch_get_main_queue(), ^{
   // UI
});

3. GCD操作相关的宏定义

//在Main线程上运行
#define DISPATCH_ON_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

//在Global Queue上运行
#define DISPATCH_ON_GLOBAL_QUEUE_HIGH(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_LOW(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), globalQueueBlock);
#define DISPATCH_ON_GLOBAL_QUEUE_BACKGROUND(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), globalQueueBlock);

4. 计算文本宽度

    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.text = string;
    label.font = [UIFont boldSystemFontOfSize:23];
    [self.view addSubview:label];
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX)
                                                  options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:nil].size;
    label.frame = CGRectMake(0, 0, size.width, size.height);

可以写一个NSString的分类:

- (CGSize)sizeForFont:(UIFont *)font width:(CGFloat)width
{
    return [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{NSFontAttributeName:font}
                                context:nil].size;
}

5. 修改系统UITableViewCell的imageView的大小

因为cell的imageView是readonly属性,不能修改。所以可以修改imageView.image的大小来改变imageView的大小。

    // 设置cell图片大小
    UIImage *cellImage = [UIImage imageNamed:@"cellImage"];
    CGSize imageSize = CGSizeMake(40, 40);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
    [cellImage drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

6.一行代码移除视图的所有子视图

[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

你可能感兴趣的:(iOS基础:每次都记不起来的代码(持续更新))