美文网首页
iOS 的一点小记载(六)

iOS 的一点小记载(六)

作者: lonelyjimmy | 来源:发表于2017-05-26 11:48 被阅读8次

计算某个文件\文件夹的大小

@implementation NSString (CLExtension)
//- (unsigned long long)fileSize
//{
//    // 总大小
//    unsigned long long size = 0;
//
//    // 文件管理者
//    NSFileManager *mgr = [NSFileManager defaultManager];
//
//    // 文件属性
//    NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
//
//    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
//        // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
//        NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
//        for (NSString *subpath in enumerator) {
//            // 全路径
//            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
//            // 累加文件大小
//            size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
//        }
//    } else { // 文件
//        size = attrs.fileSize;
//    }
//
//    return size;
//}

- (unsigned long long)fileSize
{
    // 总大小
    unsigned long long size = 0;

    // 文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];

    // 是否为文件夹
    BOOL isDirectory = NO;

    // 路径是否存在
    BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
    if (!exists) return size;

    if (isDirectory) { // 文件夹
        // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
        NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
        for (NSString *subpath in enumerator) {
            // 全路径
            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
            // 累加文件大小
            size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
        }
    } else { // 文件
        size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
    }

    return size;
}
@end

CLLog(@"%zd", @"/Users/jimmyC/Desktop".fileSize);

计算文字的宽度

CGFloat titleW = [字符串 sizeWithFont:字体大小].width;
CGFloat titleW = [字符串 sizeWithAttributes:@{NSFontAttributeName : 字体大小}].width;

有透明度的颜色

[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2];
[UIColor colorWithWhite:1.0 alpha:0.2];
[[UIColor whiteColor] colorWithAlphaComponent:0.2];

viewWithTag:内部的大致实现思路

@implementation UIView
- (UIView *)viewWithTag:(NSInteger)tag
{
    if (self.tag == tag) return self;

    for (UIView *subview in self.subviews) {
        return [subview viewWithTag:tag];
    }
}
@end

相关文章

  • iOS 的一点小记载(六)

    计算某个文件\文件夹的大小 计算文字的宽度 有透明度的颜色 viewWithTag:内部的大致实现思路

  • iOS 的一点小记载

    从iOS9开始的常见报错 从iOS9开始, 在程序启动完毕那一刻显示出来的窗口必须要设置根控制器 应用程序的图标 ...

  • iOS 的一点小记载 (三)

    frame和bounds的重新认识 frame以父控件 内容的左上角为坐标原点, 计算出的控件自己 矩形框的位置和...

  • iOS 的一点小记载(二)

    控制台可能会输出以下警告信息 警告的原因: [UIImage imageNamed:nil] 警告的原因: [UI...

  • iOS 的一点小记载(四)

    修改UITextField的光标颜色 UITextField占位文字相关的设置 NSAttributedStrin...

  • iOS 的一点小记载(五)

    CocoaPods Podfile.lock文件最后一次更新Pods时, 所有第三方框架的版本号 常用指令的区别p...

  • iOS 的一点小记载(七)

    addObject:和addObjectsFromArray:的区别 服务器分页的做法 集成MJRefresh g...

  • iOS 的一点小记载(八)

    NSDateFormatter的作用 NSString * -> NSDate * NSDate * -> NSS...

  • 面试题

    ios相关问题,这里也要一个博客有些记载https://ios.nobady.cn/Performance-opt...

  • 小罕记载——絮说2

    一说起和小罕有关的故事简直一个接一个,我也不是没有时间写。尽管每天都写,但还是发现还有那么多没有来得及写。今天又写...

网友评论

      本文标题:iOS 的一点小记载(六)

      本文链接:https://www.haomeiwen.com/subject/jdotfxtx.html