├── .gitignore ├── README.md └── TZHFileManagerDemo ├── TZHFileManagerDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── TZHFileManagerDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ ├── 120-1.png │ ├── 120.png │ ├── 180.png │ ├── 40.png │ ├── 58.png │ ├── 60.png │ ├── 80.png │ ├── 87.png │ └── Contents.json ├── Contents.json ├── T.imageset │ ├── Contents.json │ └── fd976fdb283bc8ebf02322188d761459.jpg └── 线框.imageset │ ├── Contents.json │ └── 线框.png ├── Base.lproj └── LaunchScreen.storyboard ├── DemoModel.h ├── DemoModel.m ├── DemoTabBarController.h ├── DemoTabBarController.m ├── DemoViewController.h ├── DemoViewController.m ├── Info.plist ├── MJExtension ├── Info.plist ├── MJExtension.h ├── MJExtensionConst.h ├── MJExtensionConst.m ├── MJFoundation.h ├── MJFoundation.m ├── MJProperty.h ├── MJProperty.m ├── MJPropertyKey.h ├── MJPropertyKey.m ├── MJPropertyType.h ├── MJPropertyType.m ├── NSObject+MJClass.h ├── NSObject+MJClass.m ├── NSObject+MJCoding.h ├── NSObject+MJCoding.m ├── NSObject+MJKeyValue.h ├── NSObject+MJKeyValue.m ├── NSObject+MJProperty.h ├── NSObject+MJProperty.m ├── NSString+MJExtension.h └── NSString+MJExtension.m ├── Masonry ├── Info.plist ├── MASCompositeConstraint.h ├── MASCompositeConstraint.m ├── MASConstraint+Private.h ├── MASConstraint.h ├── MASConstraint.m ├── MASConstraintMaker.h ├── MASConstraintMaker.m ├── MASLayoutConstraint.h ├── MASLayoutConstraint.m ├── MASUtilities.h ├── MASViewAttribute.h ├── MASViewAttribute.m ├── MASViewConstraint.h ├── MASViewConstraint.m ├── Masonry.h ├── NSArray+MASAdditions.h ├── NSArray+MASAdditions.m ├── NSArray+MASShorthandAdditions.h ├── NSLayoutConstraint+MASDebugAdditions.h ├── NSLayoutConstraint+MASDebugAdditions.m ├── View+MASAdditions.h ├── View+MASAdditions.m ├── View+MASShorthandAdditions.h ├── ViewController+MASAdditions.h └── ViewController+MASAdditions.m ├── TZHFileManager ├── .DS_Store ├── FileDownloadCell.xib ├── NSString+Hash.h ├── NSString+Hash.m ├── TZHDownloadCell.h ├── TZHDownloadCell.m ├── TZHDownloadManager.h ├── TZHDownloadManager.m ├── TZHDownloadViewController.h ├── TZHDownloadViewController.m ├── TZHFileDownload.h ├── TZHFileManager.bundle │ ├── Root.plist │ ├── en.lproj │ │ └── Root.strings │ └── 返回@2x.png ├── TZHFilesDownloadModel.h ├── TZHFilesDownloadModel.m ├── TZHLoadDownloadFileVC.h ├── TZHLoadDownloadFileVC.m └── screenshots │ └── .DS_Store └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TZHFileManager 2 | 文件附件文档下载到本地与查看本地文件 3 | === 4 | 本框架支持下载.doc .pdf .docx Excel 等格式文件 图片也可以下载并本地查看,为项目里有附件下载和查看的开发者们提供了很大便利!本框架的最大优点就是集成和使用简单.只要三四行代码即可. 5 | 6 | 使用方法: 7 | ----- 8 | 1.下载TZHFileManager框架,并集成到当前工程目录下 9 | 10 | 2.导入#import "TZHFileDownload.h" 头文件 11 | 12 | 3.在字典转模型处,创建TZHDownloadManager 对象,并将你的文件模型中的文件名和文件地址赋值给TZHDownloadManager 对象的相应属性,并将TZHDownloadManager的对象存储在可变数组中 13 | 14 | 4.在要跳转到文件列表控制器的地方,创建TZHDownloadViewController控制器对象.并把请求网络数据所获得的文件模型数组赋值给TZHDownloadViewController对象的filesArray数组. 15 | 16 | P.S. 如我要做到点击查看附件然后跳转到附件列表,就在查看附件按钮的点击事件里写步骤4的代码即可 17 | 18 | 19 | 20 | 新增文件占内存数值显示 和删除已下载的文件 两处功能: 21 | --- 22 | 23 | 24 | 1.显示下载文件占内存数值: 25 | //显示文件占内存大小 26 | 27 | +(NSString *)getFileCacheSize; 28 | 29 | 调用方法: 30 | 31 | NSString *fileSize = [TZHDownloadManager getFileCacheSize]; 32 | 33 | 2.删除所下载的文件: 34 | //删除文件 35 | 36 | +(void)deleteFileFromCache; 37 | 38 | 39 | 调用方法: 40 | 41 | [TZHDownloadManager deleteFileFromCache]; 42 | 43 | 44 | 联系我: 45 | ------- 46 | 如果你有什么问题或者好的建议欢迎与我联系 我的QQ: 734754688 47 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | #import "AppDelegate.h" 9 | #import "DemoTabBarController.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | 20 | self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 21 | 22 | DemoTabBarController *tzhTabBarController = [[DemoTabBarController alloc]init]; 23 | self.window.rootViewController = tzhTabBarController; 24 | 25 | [self.window makeKeyAndVisible]; 26 | return YES; 27 | } 28 | 29 | 30 | - (void)applicationWillResignActive:(UIApplication *)application { 31 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 32 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 33 | } 34 | 35 | 36 | - (void)applicationDidEnterBackground:(UIApplication *)application { 37 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 38 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 39 | } 40 | 41 | 42 | - (void)applicationWillEnterForeground:(UIApplication *)application { 43 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 44 | } 45 | 46 | 47 | - (void)applicationDidBecomeActive:(UIApplication *)application { 48 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 49 | } 50 | 51 | 52 | - (void)applicationWillTerminate:(UIApplication *)application { 53 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 54 | } 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/120-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/120-1.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/120.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/180.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/40.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/58.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/60.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/80.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/87.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/87.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "40.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "60.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "58.png", 19 | "scale" : "2x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "87.png", 25 | "scale" : "3x" 26 | }, 27 | { 28 | "size" : "40x40", 29 | "idiom" : "iphone", 30 | "filename" : "80.png", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "120.png", 37 | "scale" : "3x" 38 | }, 39 | { 40 | "size" : "60x60", 41 | "idiom" : "iphone", 42 | "filename" : "120-1.png", 43 | "scale" : "2x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "180.png", 49 | "scale" : "3x" 50 | }, 51 | { 52 | "idiom" : "ipad", 53 | "size" : "20x20", 54 | "scale" : "1x" 55 | }, 56 | { 57 | "idiom" : "ipad", 58 | "size" : "20x20", 59 | "scale" : "2x" 60 | }, 61 | { 62 | "idiom" : "ipad", 63 | "size" : "29x29", 64 | "scale" : "1x" 65 | }, 66 | { 67 | "idiom" : "ipad", 68 | "size" : "29x29", 69 | "scale" : "2x" 70 | }, 71 | { 72 | "idiom" : "ipad", 73 | "size" : "40x40", 74 | "scale" : "1x" 75 | }, 76 | { 77 | "idiom" : "ipad", 78 | "size" : "40x40", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "idiom" : "ipad", 83 | "size" : "76x76", 84 | "scale" : "1x" 85 | }, 86 | { 87 | "idiom" : "ipad", 88 | "size" : "76x76", 89 | "scale" : "2x" 90 | }, 91 | { 92 | "idiom" : "ipad", 93 | "size" : "83.5x83.5", 94 | "scale" : "2x" 95 | } 96 | ], 97 | "info" : { 98 | "version" : 1, 99 | "author" : "xcode" 100 | } 101 | } -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/T.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "fd976fdb283bc8ebf02322188d761459.jpg", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/T.imageset/fd976fdb283bc8ebf02322188d761459.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/T.imageset/fd976fdb283bc8ebf02322188d761459.jpg -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/线框.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "resizing" : { 9 | "mode" : "9-part", 10 | "center" : { 11 | "mode" : "tile", 12 | "width" : 1, 13 | "height" : 1 14 | }, 15 | "cap-insets" : { 16 | "bottom" : 7, 17 | "top" : 6, 18 | "right" : 6, 19 | "left" : 7 20 | } 21 | }, 22 | "idiom" : "universal", 23 | "filename" : "线框.png", 24 | "scale" : "2x" 25 | }, 26 | { 27 | "idiom" : "universal", 28 | "scale" : "3x" 29 | } 30 | ], 31 | "info" : { 32 | "version" : 1, 33 | "author" : "xcode" 34 | } 35 | } -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/线框.imageset/线框.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/Assets.xcassets/线框.imageset/线框.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHFileManagerModel.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DemoModel : NSObject 12 | 13 | @property(nonatomic,strong)NSString *fileName; 14 | @property(nonatomic,strong)NSString *fileUrl; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHFileManagerModel.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | #import "DemoModel.h" 9 | 10 | @implementation DemoModel 11 | 12 | @end 13 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoTabBarController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DemoTabBarController.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DemoTabBarController : UITabBarController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoTabBarController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoTabBarController.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "DemoTabBarController.h" 10 | #import "DemoViewController.h" 11 | 12 | @interface DemoTabBarController () 13 | 14 | @end 15 | 16 | @implementation DemoTabBarController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | [self creatUI]; 21 | 22 | } 23 | 24 | -(void)creatUI{ 25 | 26 | DemoViewController *tzhCheckFileVC = [[DemoViewController alloc]init]; 27 | 28 | tzhCheckFileVC.title = @"查看"; 29 | UINavigationController *navigationVC = [[UINavigationController alloc]initWithRootViewController:tzhCheckFileVC]; 30 | 31 | self.viewControllers = @[navigationVC]; 32 | } 33 | 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DemoViewController.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DemoViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/DemoViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoViewController.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "DemoViewController.h" 10 | #import "TZHFileDownload.h" 11 | #import "MJExtension.h" 12 | #import "DemoModel.h" 13 | #import "Masonry.h" 14 | 15 | @interface DemoViewController () 16 | 17 | @property(nonatomic,strong)NSArray *fileArray; 18 | @property(nonatomic,weak)UILabel *memoryLabel; 19 | 20 | @end 21 | 22 | @implementation DemoViewController 23 | 24 | - (void)viewDidLoad { 25 | [super viewDidLoad]; 26 | 27 | [self loadData]; 28 | [self creatUI]; 29 | 30 | } 31 | 32 | -(void)viewWillAppear:(BOOL)animated{ 33 | [super viewWillAppear:animated]; 34 | 35 | _memoryLabel.text = [NSString stringWithFormat:@"文件占用 %@",[TZHDownloadManager getFileCacheSize]]; 36 | } 37 | -(void)loadData{ 38 | 39 | //文件数组,此处做假数据,在您的项目中应为接口数据返回 40 | #pragma mark --以下链接可能会失效,如果要看效果,建议将你的后台接口返回的下载链接填进去,运行即可 41 | NSArray *dataArray = @[@{@"fileName":@"doc格式文件.doc",@"fileUrl":@"http://down.51rc.com/dwndoc/WrittenExamination/WrittenExperiences/dwn00006795.doc"}, 42 | @{@"fileName":@"ppt文档.ppt",@"fileUrl":@"http://down.51rc.com/dwndoc/Work/Occupational%20stress/dwn00006466.ppt"}, 43 | 44 | @{@"fileName":@"图片.jpg",@"fileUrl":@"https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/21a4462309f79052b56a7dc60df3d7ca7acbd5d4.jpg"}, 45 | @{@"fileName":@"pdf文件.pdf",@"fileUrl":@"http://d2.dajudeng.com:8092/files/20160225/201450_基于DSP和FPGA的Roip网关的设计与实现.pdf"}, 46 | @{@"fileName":@"xls文件.xls",@"fileUrl":@"http://d2.dajudeng.com:8092/files/20160613/165531_120号文.xls"} 47 | ]; 48 | 49 | 50 | #pragma mark --第一步创建 TZHFilesDownloadModel对象,将你的文件模型中的文件下载地址和文件名分别赋值给 TZHFilesDownloadModel对象的相应属性 51 | NSMutableArray *filesMutableArray = [NSMutableArray array]; 52 | 53 | for(NSDictionary *dict in dataArray){ 54 | 55 | DemoModel *model = [DemoModel mj_objectWithKeyValues:dict]; 56 | 57 | 58 | TZHFilesDownloadModel *tzhModel = [[TZHFilesDownloadModel alloc]init]; 59 | 60 | tzhModel.FileURL = model.fileUrl; 61 | tzhModel.FileName = model.fileName; 62 | 63 | [filesMutableArray addObject:tzhModel]; 64 | } 65 | 66 | self.fileArray = filesMutableArray; 67 | 68 | } 69 | 70 | -(void)creatUI{ 71 | self.view.backgroundColor = [UIColor whiteColor]; 72 | 73 | UIButton *clickBtn = [[UIButton alloc]init]; 74 | 75 | [clickBtn setTitle:@"查看附件" forState:UIControlStateNormal]; 76 | 77 | [clickBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 78 | 79 | [self.view addSubview:clickBtn]; 80 | 81 | [clickBtn addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; 82 | 83 | [clickBtn mas_makeConstraints:^(MASConstraintMaker *make) { 84 | make.centerX.equalTo(self.view); 85 | make.centerY.equalTo(self.view).offset(-100); 86 | }]; 87 | 88 | //创建显示文件占内存空间显示框 89 | UIImageView *screenLineImageView = [[UIImageView alloc]init]; 90 | 91 | screenLineImageView.image = [UIImage imageNamed:@"线框"]; 92 | 93 | [self.view addSubview:screenLineImageView]; 94 | 95 | [screenLineImageView mas_makeConstraints:^(MASConstraintMaker *make) { 96 | make.centerX.equalTo(self.view); 97 | make.top.equalTo(clickBtn.mas_bottom).offset(100); 98 | make.leading.equalTo(self.view).offset(50); 99 | make.trailing.equalTo(self.view).offset(-50); 100 | make.height.equalTo(@40); 101 | }]; 102 | 103 | UILabel *nameLabel = [[UILabel alloc]init]; 104 | nameLabel.text = @"清除文件"; 105 | [self.view addSubview:nameLabel]; 106 | 107 | [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { 108 | make.centerY.equalTo(screenLineImageView); 109 | make.leading.equalTo(screenLineImageView).offset(15); 110 | }]; 111 | 112 | UILabel *memoryLabel = [[UILabel alloc]init]; 113 | _memoryLabel = memoryLabel; 114 | memoryLabel.textColor = [UIColor lightGrayColor]; 115 | memoryLabel.font = [UIFont systemFontOfSize:15]; 116 | 117 | [self.view addSubview:memoryLabel]; 118 | 119 | [memoryLabel mas_makeConstraints:^(MASConstraintMaker *make) { 120 | make.centerY.equalTo(screenLineImageView); 121 | make.trailing.equalTo(screenLineImageView).offset(-40); 122 | }]; 123 | 124 | UIButton *deleteBtn = [[UIButton alloc]init]; 125 | [deleteBtn setBackgroundColor:[UIColor blueColor]]; 126 | [deleteBtn setTitle:@"删除所有文件" forState:UIControlStateNormal]; 127 | [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 128 | 129 | [self.view addSubview:deleteBtn]; 130 | 131 | [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { 132 | make.centerX.equalTo(self.view); 133 | make.top.equalTo(screenLineImageView.mas_bottom).offset(100); 134 | }]; 135 | 136 | [deleteBtn addTarget:self action:@selector(clickDeleteBtn) forControlEvents:UIControlEventTouchUpInside]; 137 | #pragma mark -- 想要显示下载的文件在沙盒中的内存调用 [TZHDownloadManager getFileCacheSize] 方法 138 | memoryLabel.text = [NSString stringWithFormat:@"文件占用 %@",[TZHDownloadManager getFileCacheSize]]; 139 | 140 | 141 | } 142 | 143 | -(void)clickButton{ 144 | 145 | #pragma mark --第二步,在要查看附件文件的点击事件的实现方法中创建 TZHDownloadViewController对象可直接复制以下代码 146 | TZHDownloadViewController *tzhDownloadVC = [[TZHDownloadViewController alloc]init]; 147 | 148 | //此处为跳转影藏底部tabBar 149 | tzhDownloadVC.hidesBottomBarWhenPushed = YES; 150 | 151 | tzhDownloadVC.filesArray = self.fileArray; 152 | [self.navigationController pushViewController:tzhDownloadVC animated:YES]; 153 | 154 | //返回时显示子页面隐藏的tabBar 155 | self.hidesBottomBarWhenPushed = NO; 156 | 157 | } 158 | 159 | 160 | #pragma mark ---删除下载的所有文件调用以下方法 161 | -(void)clickDeleteBtn{ 162 | 163 | [TZHDownloadManager deleteFileFromCache]; 164 | 165 | _memoryLabel.text = [NSString stringWithFormat:@"文件占用 %@",[TZHDownloadManager getFileCacheSize]]; 166 | 167 | } 168 | @end 169 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSAppTransportSecurity 24 | 25 | NSAllowsArbitraryLoads 26 | 27 | 28 | UILaunchStoryboardName 29 | LaunchScreen 30 | UIRequiredDeviceCapabilities 31 | 32 | armv7 33 | 34 | UISupportedInterfaceOrientations 35 | 36 | UIInterfaceOrientationPortrait 37 | UIInterfaceOrientationLandscapeLeft 38 | UIInterfaceOrientationLandscapeRight 39 | 40 | UISupportedInterfaceOrientations~ipad 41 | 42 | UIInterfaceOrientationPortrait 43 | UIInterfaceOrientationPortraitUpsideDown 44 | UIInterfaceOrientationLandscapeLeft 45 | UIInterfaceOrientationLandscapeRight 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJExtension.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJExtension.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 代码地址:https://github.com/CoderMJLee/MJExtension 8 | // 代码地址:http://code4app.com/ios/%E5%AD%97%E5%85%B8-JSON-%E4%B8%8E%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%BD%AC%E6%8D%A2/5339992a933bf062608b4c57 9 | 10 | #import "NSObject+MJCoding.h" 11 | #import "NSObject+MJProperty.h" 12 | #import "NSObject+MJClass.h" 13 | #import "NSObject+MJKeyValue.h" 14 | #import "NSString+MJExtension.h" 15 | #import "MJExtensionConst.h" -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJExtensionConst.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MJExtensionConst__H__ 3 | #define __MJExtensionConst__H__ 4 | 5 | #import 6 | 7 | // 过期 8 | #define MJExtensionDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead) 9 | 10 | // 构建错误 11 | #define MJExtensionBuildError(clazz, msg) \ 12 | NSError *error = [NSError errorWithDomain:msg code:250 userInfo:nil]; \ 13 | [clazz setMj_error:error]; 14 | 15 | // 日志输出 16 | #ifdef DEBUG 17 | #define MJExtensionLog(...) NSLog(__VA_ARGS__) 18 | #else 19 | #define MJExtensionLog(...) 20 | #endif 21 | 22 | /** 23 | * 断言 24 | * @param condition 条件 25 | * @param returnValue 返回值 26 | */ 27 | #define MJExtensionAssertError(condition, returnValue, clazz, msg) \ 28 | [clazz setMj_error:nil]; \ 29 | if ((condition) == NO) { \ 30 | MJExtensionBuildError(clazz, msg); \ 31 | return returnValue;\ 32 | } 33 | 34 | #define MJExtensionAssert2(condition, returnValue) \ 35 | if ((condition) == NO) return returnValue; 36 | 37 | /** 38 | * 断言 39 | * @param condition 条件 40 | */ 41 | #define MJExtensionAssert(condition) MJExtensionAssert2(condition, ) 42 | 43 | /** 44 | * 断言 45 | * @param param 参数 46 | * @param returnValue 返回值 47 | */ 48 | #define MJExtensionAssertParamNotNil2(param, returnValue) \ 49 | MJExtensionAssert2((param) != nil, returnValue) 50 | 51 | /** 52 | * 断言 53 | * @param param 参数 54 | */ 55 | #define MJExtensionAssertParamNotNil(param) MJExtensionAssertParamNotNil2(param, ) 56 | 57 | /** 58 | * 打印所有的属性 59 | */ 60 | #define MJLogAllIvars \ 61 | -(NSString *)description \ 62 | { \ 63 | return [self mj_keyValues].description; \ 64 | } 65 | #define MJExtensionLogAllProperties MJLogAllIvars 66 | 67 | /** 68 | * 类型(属性类型) 69 | */ 70 | extern NSString *const MJPropertyTypeInt; 71 | extern NSString *const MJPropertyTypeShort; 72 | extern NSString *const MJPropertyTypeFloat; 73 | extern NSString *const MJPropertyTypeDouble; 74 | extern NSString *const MJPropertyTypeLong; 75 | extern NSString *const MJPropertyTypeLongLong; 76 | extern NSString *const MJPropertyTypeChar; 77 | extern NSString *const MJPropertyTypeBOOL1; 78 | extern NSString *const MJPropertyTypeBOOL2; 79 | extern NSString *const MJPropertyTypePointer; 80 | 81 | extern NSString *const MJPropertyTypeIvar; 82 | extern NSString *const MJPropertyTypeMethod; 83 | extern NSString *const MJPropertyTypeBlock; 84 | extern NSString *const MJPropertyTypeClass; 85 | extern NSString *const MJPropertyTypeSEL; 86 | extern NSString *const MJPropertyTypeId; 87 | 88 | #endif -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJExtensionConst.m: -------------------------------------------------------------------------------- 1 | #ifndef __MJExtensionConst__M__ 2 | #define __MJExtensionConst__M__ 3 | 4 | #import 5 | 6 | /** 7 | * 成员变量类型(属性类型) 8 | */ 9 | NSString *const MJPropertyTypeInt = @"i"; 10 | NSString *const MJPropertyTypeShort = @"s"; 11 | NSString *const MJPropertyTypeFloat = @"f"; 12 | NSString *const MJPropertyTypeDouble = @"d"; 13 | NSString *const MJPropertyTypeLong = @"l"; 14 | NSString *const MJPropertyTypeLongLong = @"q"; 15 | NSString *const MJPropertyTypeChar = @"c"; 16 | NSString *const MJPropertyTypeBOOL1 = @"c"; 17 | NSString *const MJPropertyTypeBOOL2 = @"b"; 18 | NSString *const MJPropertyTypePointer = @"*"; 19 | 20 | NSString *const MJPropertyTypeIvar = @"^{objc_ivar=}"; 21 | NSString *const MJPropertyTypeMethod = @"^{objc_method=}"; 22 | NSString *const MJPropertyTypeBlock = @"@?"; 23 | NSString *const MJPropertyTypeClass = @"#"; 24 | NSString *const MJPropertyTypeSEL = @":"; 25 | NSString *const MJPropertyTypeId = @"@"; 26 | 27 | #endif -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJFoundation.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJFoundation.h 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 14/7/16. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MJFoundation : NSObject 12 | + (BOOL)isClassFromFoundation:(Class)c; 13 | @end 14 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJFoundation.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJFoundation.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 14/7/16. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "MJFoundation.h" 10 | #import "MJExtensionConst.h" 11 | #import 12 | 13 | static NSSet *foundationClasses_; 14 | 15 | @implementation MJFoundation 16 | 17 | + (NSSet *)foundationClasses 18 | { 19 | if (foundationClasses_ == nil) { 20 | // 集合中没有NSObject,因为几乎所有的类都是继承自NSObject,具体是不是NSObject需要特殊判断 21 | foundationClasses_ = [NSSet setWithObjects: 22 | [NSURL class], 23 | [NSDate class], 24 | [NSValue class], 25 | [NSData class], 26 | [NSError class], 27 | [NSArray class], 28 | [NSDictionary class], 29 | [NSString class], 30 | [NSAttributedString class], nil]; 31 | } 32 | return foundationClasses_; 33 | } 34 | 35 | + (BOOL)isClassFromFoundation:(Class)c 36 | { 37 | if (c == [NSObject class] || c == [NSManagedObject class]) return YES; 38 | 39 | __block BOOL result = NO; 40 | [[self foundationClasses] enumerateObjectsUsingBlock:^(Class foundationClass, BOOL *stop) { 41 | if ([c isSubclassOfClass:foundationClass]) { 42 | result = YES; 43 | *stop = YES; 44 | } 45 | }]; 46 | return result; 47 | } 48 | @end 49 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJProperty.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJProperty.h 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/4/17. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 包装一个成员属性 8 | 9 | #import 10 | #import 11 | #import "MJPropertyType.h" 12 | #import "MJPropertyKey.h" 13 | 14 | /** 15 | * 包装一个成员 16 | */ 17 | @interface MJProperty : NSObject 18 | /** 成员属性 */ 19 | @property (nonatomic, assign) objc_property_t property; 20 | /** 成员属性的名字 */ 21 | @property (nonatomic, readonly) NSString *name; 22 | 23 | /** 成员属性的类型 */ 24 | @property (nonatomic, readonly) MJPropertyType *type; 25 | /** 成员属性来源于哪个类(可能是父类) */ 26 | @property (nonatomic, assign) Class srcClass; 27 | 28 | /**** 同一个成员属性 - 父类和子类的行为可能不一致(originKey、propertyKeys、objectClassInArray) ****/ 29 | /** 设置最原始的key */ 30 | - (void)setOriginKey:(id)originKey forClass:(Class)c; 31 | /** 对应着字典中的多级key(里面存放的数组,数组里面都是MJPropertyKey对象) */ 32 | - (NSArray *)propertyKeysForClass:(Class)c; 33 | 34 | /** 模型数组中的模型类型 */ 35 | - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c; 36 | - (Class)objectClassInArrayForClass:(Class)c; 37 | /**** 同一个成员变量 - 父类和子类的行为可能不一致(key、keys、objectClassInArray) ****/ 38 | 39 | /** 40 | * 设置object的成员变量值 41 | */ 42 | - (void)setValue:(id)value forObject:(id)object; 43 | /** 44 | * 得到object的成员属性值 45 | */ 46 | - (id)valueForObject:(id)object; 47 | 48 | /** 49 | * 初始化 50 | */ 51 | + (instancetype)cachedPropertyWithProperty:(objc_property_t)property; 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJProperty.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJProperty.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/4/17. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "MJProperty.h" 10 | #import "MJFoundation.h" 11 | #import "MJExtensionConst.h" 12 | #import 13 | 14 | @interface MJProperty() 15 | @property (strong, nonatomic) NSMutableDictionary *propertyKeysDict; 16 | @property (strong, nonatomic) NSMutableDictionary *objectClassInArrayDict; 17 | @end 18 | 19 | @implementation MJProperty 20 | 21 | #pragma mark - 初始化 22 | - (instancetype)init 23 | { 24 | if (self = [super init]) { 25 | _propertyKeysDict = [NSMutableDictionary dictionary]; 26 | _objectClassInArrayDict = [NSMutableDictionary dictionary]; 27 | } 28 | return self; 29 | } 30 | 31 | #pragma mark - 缓存 32 | + (instancetype)cachedPropertyWithProperty:(objc_property_t)property 33 | { 34 | MJProperty *propertyObj = objc_getAssociatedObject(self, property); 35 | if (propertyObj == nil) { 36 | propertyObj = [[self alloc] init]; 37 | propertyObj.property = property; 38 | objc_setAssociatedObject(self, property, propertyObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 39 | } 40 | return propertyObj; 41 | } 42 | 43 | #pragma mark - 公共方法 44 | - (void)setProperty:(objc_property_t)property 45 | { 46 | _property = property; 47 | 48 | MJExtensionAssertParamNotNil(property); 49 | 50 | // 1.属性名 51 | _name = @(property_getName(property)); 52 | 53 | // 2.成员类型 54 | NSString *attrs = @(property_getAttributes(property)); 55 | NSUInteger dotLoc = [attrs rangeOfString:@","].location; 56 | NSString *code = nil; 57 | NSUInteger loc = 1; 58 | if (dotLoc == NSNotFound) { // 没有, 59 | code = [attrs substringFromIndex:loc]; 60 | } else { 61 | code = [attrs substringWithRange:NSMakeRange(loc, dotLoc - loc)]; 62 | } 63 | _type = [MJPropertyType cachedTypeWithCode:code]; 64 | } 65 | 66 | /** 67 | * 获得成员变量的值 68 | */ 69 | - (id)valueForObject:(id)object 70 | { 71 | if (self.type.KVCDisabled) return [NSNull null]; 72 | return [object valueForKey:self.name]; 73 | } 74 | 75 | /** 76 | * 设置成员变量的值 77 | */ 78 | - (void)setValue:(id)value forObject:(id)object 79 | { 80 | if (self.type.KVCDisabled || value == nil) return; 81 | [object setValue:value forKey:self.name]; 82 | } 83 | 84 | /** 85 | * 通过字符串key创建对应的keys 86 | */ 87 | - (NSArray *)propertyKeysWithStringKey:(NSString *)stringKey 88 | { 89 | if (stringKey.length == 0) return nil; 90 | 91 | NSMutableArray *propertyKeys = [NSMutableArray array]; 92 | // 如果有多级映射 93 | NSArray *oldKeys = [stringKey componentsSeparatedByString:@"."]; 94 | 95 | for (NSString *oldKey in oldKeys) { 96 | NSUInteger start = [oldKey rangeOfString:@"["].location; 97 | if (start != NSNotFound) { // 有索引的key 98 | NSString *prefixKey = [oldKey substringToIndex:start]; 99 | NSString *indexKey = prefixKey; 100 | if (prefixKey.length) { 101 | MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init]; 102 | propertyKey.name = prefixKey; 103 | [propertyKeys addObject:propertyKey]; 104 | 105 | indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""]; 106 | } 107 | 108 | /** 解析索引 **/ 109 | // 元素 110 | NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"]; 111 | for (NSInteger i = 0; i 10 | 11 | typedef enum { 12 | MJPropertyKeyTypeDictionary = 0, // 字典的key 13 | MJPropertyKeyTypeArray // 数组的key 14 | } MJPropertyKeyType; 15 | 16 | /** 17 | * 属性的key 18 | */ 19 | @interface MJPropertyKey : NSObject 20 | /** key的名字 */ 21 | @property (copy, nonatomic) NSString *name; 22 | /** key的种类,可能是@"10",可能是@"age" */ 23 | @property (assign, nonatomic) MJPropertyKeyType type; 24 | 25 | /** 26 | * 根据当前的key,也就是name,从object(字典或者数组)中取值 27 | */ 28 | - (id)valueInObject:(id)object; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJPropertyKey.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJPropertyKey.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/8/11. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "MJPropertyKey.h" 10 | 11 | @implementation MJPropertyKey 12 | 13 | - (id)valueInObject:(id)object 14 | { 15 | if ([object isKindOfClass:[NSDictionary class]] && self.type == MJPropertyKeyTypeDictionary) { 16 | return object[self.name]; 17 | } else if ([object isKindOfClass:[NSArray class]] && self.type == MJPropertyKeyTypeArray) { 18 | NSArray *array = object; 19 | NSUInteger index = self.name.intValue; 20 | if (index < array.count) return array[index]; 21 | return nil; 22 | } 23 | return nil; 24 | } 25 | @end 26 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJPropertyType.h: -------------------------------------------------------------------------------- 1 | // 2 | // MJPropertyType.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 包装一种类型 8 | 9 | #import 10 | 11 | /** 12 | * 包装一种类型 13 | */ 14 | @interface MJPropertyType : NSObject 15 | /** 类型标识符 */ 16 | @property (nonatomic, copy) NSString *code; 17 | 18 | /** 是否为id类型 */ 19 | @property (nonatomic, readonly, getter=isIdType) BOOL idType; 20 | 21 | /** 是否为基本数字类型:int、float等 */ 22 | @property (nonatomic, readonly, getter=isNumberType) BOOL numberType; 23 | 24 | /** 是否为BOOL类型 */ 25 | @property (nonatomic, readonly, getter=isBoolType) BOOL boolType; 26 | 27 | /** 对象类型(如果是基本数据类型,此值为nil) */ 28 | @property (nonatomic, readonly) Class typeClass; 29 | 30 | /** 类型是否来自于Foundation框架,比如NSString、NSArray */ 31 | @property (nonatomic, readonly, getter = isFromFoundation) BOOL fromFoundation; 32 | /** 类型是否不支持KVC */ 33 | @property (nonatomic, readonly, getter = isKVCDisabled) BOOL KVCDisabled; 34 | 35 | /** 36 | * 获得缓存的类型对象 37 | */ 38 | + (instancetype)cachedTypeWithCode:(NSString *)code; 39 | @end -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/MJPropertyType.m: -------------------------------------------------------------------------------- 1 | // 2 | // MJPropertyType.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "MJPropertyType.h" 10 | #import "MJExtension.h" 11 | #import "MJFoundation.h" 12 | #import "MJExtensionConst.h" 13 | 14 | @implementation MJPropertyType 15 | 16 | static NSMutableDictionary *types_; 17 | + (void)initialize 18 | { 19 | types_ = [NSMutableDictionary dictionary]; 20 | } 21 | 22 | + (instancetype)cachedTypeWithCode:(NSString *)code 23 | { 24 | MJExtensionAssertParamNotNil2(code, nil); 25 | @synchronized (self) { 26 | MJPropertyType *type = types_[code]; 27 | if (type == nil) { 28 | type = [[self alloc] init]; 29 | type.code = code; 30 | types_[code] = type; 31 | } 32 | return type; 33 | } 34 | } 35 | 36 | #pragma mark - 公共方法 37 | - (void)setCode:(NSString *)code 38 | { 39 | _code = code; 40 | 41 | MJExtensionAssertParamNotNil(code); 42 | 43 | if ([code isEqualToString:MJPropertyTypeId]) { 44 | _idType = YES; 45 | } else if (code.length == 0) { 46 | _KVCDisabled = YES; 47 | } else if (code.length > 3 && [code hasPrefix:@"@\""]) { 48 | // 去掉@"和",截取中间的类型名称 49 | _code = [code substringWithRange:NSMakeRange(2, code.length - 3)]; 50 | _typeClass = NSClassFromString(_code); 51 | _fromFoundation = [MJFoundation isClassFromFoundation:_typeClass]; 52 | _numberType = [_typeClass isSubclassOfClass:[NSNumber class]]; 53 | 54 | } else if ([code isEqualToString:MJPropertyTypeSEL] || 55 | [code isEqualToString:MJPropertyTypeIvar] || 56 | [code isEqualToString:MJPropertyTypeMethod]) { 57 | _KVCDisabled = YES; 58 | } 59 | 60 | // 是否为数字类型 61 | NSString *lowerCode = _code.lowercaseString; 62 | NSArray *numberTypes = @[MJPropertyTypeInt, MJPropertyTypeShort, MJPropertyTypeBOOL1, MJPropertyTypeBOOL2, MJPropertyTypeFloat, MJPropertyTypeDouble, MJPropertyTypeLong, MJPropertyTypeLongLong, MJPropertyTypeChar]; 63 | if ([numberTypes containsObject:lowerCode]) { 64 | _numberType = YES; 65 | 66 | if ([lowerCode isEqualToString:MJPropertyTypeBOOL1] 67 | || [lowerCode isEqualToString:MJPropertyTypeBOOL2]) { 68 | _boolType = YES; 69 | } 70 | } 71 | } 72 | @end 73 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJClass.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJClass.h 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/8/11. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | * 遍历所有类的block(父类) 13 | */ 14 | typedef void (^MJClassesEnumeration)(Class c, BOOL *stop); 15 | 16 | /** 这个数组中的属性名才会进行字典和模型的转换 */ 17 | typedef NSArray * (^MJAllowedPropertyNames)(); 18 | /** 这个数组中的属性名才会进行归档 */ 19 | typedef NSArray * (^MJAllowedCodingPropertyNames)(); 20 | 21 | /** 这个数组中的属性名将会被忽略:不进行字典和模型的转换 */ 22 | typedef NSArray * (^MJIgnoredPropertyNames)(); 23 | /** 这个数组中的属性名将会被忽略:不进行归档 */ 24 | typedef NSArray * (^MJIgnoredCodingPropertyNames)(); 25 | 26 | /** 27 | * 类相关的扩展 28 | */ 29 | @interface NSObject (MJClass) 30 | /** 31 | * 遍历所有的类 32 | */ 33 | + (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration; 34 | + (void)mj_enumerateAllClasses:(MJClassesEnumeration)enumeration; 35 | 36 | #pragma mark - 属性白名单配置 37 | /** 38 | * 这个数组中的属性名才会进行字典和模型的转换 39 | * 40 | * @param allowedPropertyNames 这个数组中的属性名才会进行字典和模型的转换 41 | */ 42 | + (void)mj_setupAllowedPropertyNames:(MJAllowedPropertyNames)allowedPropertyNames; 43 | 44 | /** 45 | * 这个数组中的属性名才会进行字典和模型的转换 46 | */ 47 | + (NSMutableArray *)mj_totalAllowedPropertyNames; 48 | 49 | #pragma mark - 属性黑名单配置 50 | /** 51 | * 这个数组中的属性名将会被忽略:不进行字典和模型的转换 52 | * 53 | * @param ignoredPropertyNames 这个数组中的属性名将会被忽略:不进行字典和模型的转换 54 | */ 55 | + (void)mj_setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames; 56 | 57 | /** 58 | * 这个数组中的属性名将会被忽略:不进行字典和模型的转换 59 | */ 60 | + (NSMutableArray *)mj_totalIgnoredPropertyNames; 61 | 62 | #pragma mark - 归档属性白名单配置 63 | /** 64 | * 这个数组中的属性名才会进行归档 65 | * 66 | * @param allowedCodingPropertyNames 这个数组中的属性名才会进行归档 67 | */ 68 | + (void)mj_setupAllowedCodingPropertyNames:(MJAllowedCodingPropertyNames)allowedCodingPropertyNames; 69 | 70 | /** 71 | * 这个数组中的属性名才会进行字典和模型的转换 72 | */ 73 | + (NSMutableArray *)mj_totalAllowedCodingPropertyNames; 74 | 75 | #pragma mark - 归档属性黑名单配置 76 | /** 77 | * 这个数组中的属性名将会被忽略:不进行归档 78 | * 79 | * @param ignoredCodingPropertyNames 这个数组中的属性名将会被忽略:不进行归档 80 | */ 81 | + (void)mj_setupIgnoredCodingPropertyNames:(MJIgnoredCodingPropertyNames)ignoredCodingPropertyNames; 82 | 83 | /** 84 | * 这个数组中的属性名将会被忽略:不进行归档 85 | */ 86 | + (NSMutableArray *)mj_totalIgnoredCodingPropertyNames; 87 | 88 | #pragma mark - 内部使用 89 | + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key; 90 | @end 91 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJClass.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJClass.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/8/11. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJClass.h" 10 | #import "NSObject+MJCoding.h" 11 | #import "NSObject+MJKeyValue.h" 12 | #import "MJFoundation.h" 13 | #import 14 | 15 | static const char MJAllowedPropertyNamesKey = '\0'; 16 | static const char MJIgnoredPropertyNamesKey = '\0'; 17 | static const char MJAllowedCodingPropertyNamesKey = '\0'; 18 | static const char MJIgnoredCodingPropertyNamesKey = '\0'; 19 | 20 | static NSMutableDictionary *allowedPropertyNamesDict_; 21 | static NSMutableDictionary *ignoredPropertyNamesDict_; 22 | static NSMutableDictionary *allowedCodingPropertyNamesDict_; 23 | static NSMutableDictionary *ignoredCodingPropertyNamesDict_; 24 | 25 | @implementation NSObject (MJClass) 26 | 27 | + (void)load 28 | { 29 | allowedPropertyNamesDict_ = [NSMutableDictionary dictionary]; 30 | ignoredPropertyNamesDict_ = [NSMutableDictionary dictionary]; 31 | allowedCodingPropertyNamesDict_ = [NSMutableDictionary dictionary]; 32 | ignoredCodingPropertyNamesDict_ = [NSMutableDictionary dictionary]; 33 | } 34 | 35 | + (NSMutableDictionary *)dictForKey:(const void *)key 36 | { 37 | @synchronized (self) { 38 | if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_; 39 | if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_; 40 | if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_; 41 | if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_; 42 | return nil; 43 | } 44 | } 45 | 46 | + (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration 47 | { 48 | // 1.没有block就直接返回 49 | if (enumeration == nil) return; 50 | 51 | // 2.停止遍历的标记 52 | BOOL stop = NO; 53 | 54 | // 3.当前正在遍历的类 55 | Class c = self; 56 | 57 | // 4.开始遍历每一个类 58 | while (c && !stop) { 59 | // 4.1.执行操作 60 | enumeration(c, &stop); 61 | 62 | // 4.2.获得父类 63 | c = class_getSuperclass(c); 64 | 65 | if ([MJFoundation isClassFromFoundation:c]) break; 66 | } 67 | } 68 | 69 | + (void)mj_enumerateAllClasses:(MJClassesEnumeration)enumeration 70 | { 71 | // 1.没有block就直接返回 72 | if (enumeration == nil) return; 73 | 74 | // 2.停止遍历的标记 75 | BOOL stop = NO; 76 | 77 | // 3.当前正在遍历的类 78 | Class c = self; 79 | 80 | // 4.开始遍历每一个类 81 | while (c && !stop) { 82 | // 4.1.执行操作 83 | enumeration(c, &stop); 84 | 85 | // 4.2.获得父类 86 | c = class_getSuperclass(c); 87 | } 88 | } 89 | 90 | #pragma mark - 属性黑名单配置 91 | + (void)mj_setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames 92 | { 93 | [self mj_setupBlockReturnValue:ignoredPropertyNames key:&MJIgnoredPropertyNamesKey]; 94 | } 95 | 96 | + (NSMutableArray *)mj_totalIgnoredPropertyNames 97 | { 98 | return [self mj_totalObjectsWithSelector:@selector(mj_ignoredPropertyNames) key:&MJIgnoredPropertyNamesKey]; 99 | } 100 | 101 | #pragma mark - 归档属性黑名单配置 102 | + (void)mj_setupIgnoredCodingPropertyNames:(MJIgnoredCodingPropertyNames)ignoredCodingPropertyNames 103 | { 104 | [self mj_setupBlockReturnValue:ignoredCodingPropertyNames key:&MJIgnoredCodingPropertyNamesKey]; 105 | } 106 | 107 | + (NSMutableArray *)mj_totalIgnoredCodingPropertyNames 108 | { 109 | return [self mj_totalObjectsWithSelector:@selector(mj_ignoredCodingPropertyNames) key:&MJIgnoredCodingPropertyNamesKey]; 110 | } 111 | 112 | #pragma mark - 属性白名单配置 113 | + (void)mj_setupAllowedPropertyNames:(MJAllowedPropertyNames)allowedPropertyNames; 114 | { 115 | [self mj_setupBlockReturnValue:allowedPropertyNames key:&MJAllowedPropertyNamesKey]; 116 | } 117 | 118 | + (NSMutableArray *)mj_totalAllowedPropertyNames 119 | { 120 | return [self mj_totalObjectsWithSelector:@selector(mj_allowedPropertyNames) key:&MJAllowedPropertyNamesKey]; 121 | } 122 | 123 | #pragma mark - 归档属性白名单配置 124 | + (void)mj_setupAllowedCodingPropertyNames:(MJAllowedCodingPropertyNames)allowedCodingPropertyNames 125 | { 126 | [self mj_setupBlockReturnValue:allowedCodingPropertyNames key:&MJAllowedCodingPropertyNamesKey]; 127 | } 128 | 129 | + (NSMutableArray *)mj_totalAllowedCodingPropertyNames 130 | { 131 | return [self mj_totalObjectsWithSelector:@selector(mj_allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey]; 132 | } 133 | #pragma mark - block和方法处理:存储block的返回值 134 | + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key 135 | { 136 | if (block) { 137 | objc_setAssociatedObject(self, key, block(), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 138 | } else { 139 | objc_setAssociatedObject(self, key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 140 | } 141 | 142 | // 清空数据 143 | [[self dictForKey:key] removeAllObjects]; 144 | } 145 | 146 | + (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key 147 | { 148 | NSMutableArray *array = [self dictForKey:key][NSStringFromClass(self)]; 149 | if (array) return array; 150 | 151 | // 创建、存储 152 | [self dictForKey:key][NSStringFromClass(self)] = array = [NSMutableArray array]; 153 | 154 | if ([self respondsToSelector:selector]) { 155 | #pragma clang diagnostic push 156 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" 157 | NSArray *subArray = [self performSelector:selector]; 158 | #pragma clang diagnostic pop 159 | if (subArray) { 160 | [array addObjectsFromArray:subArray]; 161 | } 162 | } 163 | 164 | [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) { 165 | NSArray *subArray = objc_getAssociatedObject(c, key); 166 | [array addObjectsFromArray:subArray]; 167 | }]; 168 | return array; 169 | } 170 | @end 171 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJCoding.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJCoding.h 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MJExtensionConst.h" 11 | 12 | /** 13 | * Codeing协议 14 | */ 15 | @protocol MJCoding 16 | @optional 17 | /** 18 | * 这个数组中的属性名才会进行归档 19 | */ 20 | + (NSArray *)mj_allowedCodingPropertyNames; 21 | /** 22 | * 这个数组中的属性名将会被忽略:不进行归档 23 | */ 24 | + (NSArray *)mj_ignoredCodingPropertyNames; 25 | @end 26 | 27 | @interface NSObject (MJCoding) 28 | /** 29 | * 解码(从文件中解析对象) 30 | */ 31 | - (void)mj_decode:(NSCoder *)decoder; 32 | /** 33 | * 编码(将对象写入文件中) 34 | */ 35 | - (void)mj_encode:(NSCoder *)encoder; 36 | @end 37 | 38 | /** 39 | 归档的实现 40 | */ 41 | #define MJCodingImplementation \ 42 | - (id)initWithCoder:(NSCoder *)decoder \ 43 | { \ 44 | if (self = [super init]) { \ 45 | [self mj_decode:decoder]; \ 46 | } \ 47 | return self; \ 48 | } \ 49 | \ 50 | - (void)encodeWithCoder:(NSCoder *)encoder \ 51 | { \ 52 | [self mj_encode:encoder]; \ 53 | } 54 | 55 | #define MJExtensionCodingImplementation MJCodingImplementation -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJCoding.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJCoding.m 3 | // MJExtension 4 | // 5 | // Created by mj on 14-1-15. 6 | // Copyright (c) 2014年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJCoding.h" 10 | #import "NSObject+MJClass.h" 11 | #import "NSObject+MJProperty.h" 12 | #import "MJProperty.h" 13 | 14 | @implementation NSObject (MJCoding) 15 | 16 | - (void)mj_encode:(NSCoder *)encoder 17 | { 18 | Class clazz = [self class]; 19 | 20 | NSArray *allowedCodingPropertyNames = [clazz mj_totalAllowedCodingPropertyNames]; 21 | NSArray *ignoredCodingPropertyNames = [clazz mj_totalIgnoredCodingPropertyNames]; 22 | 23 | [clazz mj_enumerateProperties:^(MJProperty *property, BOOL *stop) { 24 | // 检测是否被忽略 25 | if (allowedCodingPropertyNames.count && ![allowedCodingPropertyNames containsObject:property.name]) return; 26 | if ([ignoredCodingPropertyNames containsObject:property.name]) return; 27 | 28 | id value = [property valueForObject:self]; 29 | if (value == nil) return; 30 | [encoder encodeObject:value forKey:property.name]; 31 | }]; 32 | } 33 | 34 | - (void)mj_decode:(NSCoder *)decoder 35 | { 36 | Class clazz = [self class]; 37 | 38 | NSArray *allowedCodingPropertyNames = [clazz mj_totalAllowedCodingPropertyNames]; 39 | NSArray *ignoredCodingPropertyNames = [clazz mj_totalIgnoredCodingPropertyNames]; 40 | 41 | [clazz mj_enumerateProperties:^(MJProperty *property, BOOL *stop) { 42 | // 检测是否被忽略 43 | if (allowedCodingPropertyNames.count && ![allowedCodingPropertyNames containsObject:property.name]) return; 44 | if ([ignoredCodingPropertyNames containsObject:property.name]) return; 45 | 46 | id value = [decoder decodeObjectForKey:property.name]; 47 | if (value == nil) { // 兼容以前的MJExtension版本 48 | value = [decoder decodeObjectForKey:[@"_" stringByAppendingString:property.name]]; 49 | } 50 | if (value == nil) return; 51 | [property setValue:value forObject:self]; 52 | }]; 53 | } 54 | @end 55 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJKeyValue.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJKeyValue.h 3 | // MJExtension 4 | // 5 | // Created by mj on 13-8-24. 6 | // Copyright (c) 2013年 小码哥. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MJExtensionConst.h" 11 | #import 12 | #import "MJProperty.h" 13 | 14 | /** 15 | * KeyValue协议 16 | */ 17 | @protocol MJKeyValue 18 | @optional 19 | /** 20 | * 只有这个数组中的属性名才允许进行字典和模型的转换 21 | */ 22 | + (NSArray *)mj_allowedPropertyNames; 23 | 24 | /** 25 | * 这个数组中的属性名将会被忽略:不进行字典和模型的转换 26 | */ 27 | + (NSArray *)mj_ignoredPropertyNames; 28 | 29 | /** 30 | * 将属性名换为其他key去字典中取值 31 | * 32 | * @return 字典中的key是属性名,value是从字典中取值用的key 33 | */ 34 | + (NSDictionary *)mj_replacedKeyFromPropertyName; 35 | 36 | /** 37 | * 将属性名换为其他key去字典中取值 38 | * 39 | * @return 从字典中取值用的key 40 | */ 41 | + (id)mj_replacedKeyFromPropertyName121:(NSString *)propertyName; 42 | 43 | /** 44 | * 数组中需要转换的模型类 45 | * 46 | * @return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型) 47 | */ 48 | + (NSDictionary *)mj_objectClassInArray; 49 | 50 | /** 51 | * 旧值换新值,用于过滤字典中的值 52 | * 53 | * @param oldValue 旧值 54 | * 55 | * @return 新值 56 | */ 57 | - (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property; 58 | 59 | /** 60 | * 当字典转模型完毕时调用 61 | */ 62 | - (void)mj_keyValuesDidFinishConvertingToObject; 63 | 64 | /** 65 | * 当模型转字典完毕时调用 66 | */ 67 | - (void)mj_objectDidFinishConvertingToKeyValues; 68 | @end 69 | 70 | @interface NSObject (MJKeyValue) 71 | #pragma mark - 类方法 72 | /** 73 | * 字典转模型过程中遇到的错误 74 | */ 75 | + (NSError *)mj_error; 76 | 77 | /** 78 | * 模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法(父类设置了,子类也会继承下来) 79 | */ 80 | + (void)mj_referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference; 81 | 82 | #pragma mark - 对象方法 83 | /** 84 | * 将字典的键值对转成模型属性 85 | * @param keyValues 字典(可以是NSDictionary、NSData、NSString) 86 | */ 87 | - (instancetype)mj_setKeyValues:(id)keyValues; 88 | 89 | /** 90 | * 将字典的键值对转成模型属性 91 | * @param keyValues 字典(可以是NSDictionary、NSData、NSString) 92 | * @param context CoreData上下文 93 | */ 94 | - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context; 95 | 96 | /** 97 | * 将模型转成字典 98 | * @return 字典 99 | */ 100 | - (NSMutableDictionary *)mj_keyValues; 101 | - (NSMutableDictionary *)mj_keyValuesWithKeys:(NSArray *)keys; 102 | - (NSMutableDictionary *)mj_keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys; 103 | 104 | /** 105 | * 通过模型数组来创建一个字典数组 106 | * @param objectArray 模型数组 107 | * @return 字典数组 108 | */ 109 | + (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray; 110 | + (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray keys:(NSArray *)keys; 111 | + (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray ignoredKeys:(NSArray *)ignoredKeys; 112 | 113 | #pragma mark - 字典转模型 114 | /** 115 | * 通过字典来创建一个模型 116 | * @param keyValues 字典(可以是NSDictionary、NSData、NSString) 117 | * @return 新建的对象 118 | */ 119 | + (instancetype)mj_objectWithKeyValues:(id)keyValues; 120 | 121 | /** 122 | * 通过字典来创建一个CoreData模型 123 | * @param keyValues 字典(可以是NSDictionary、NSData、NSString) 124 | * @param context CoreData上下文 125 | * @return 新建的对象 126 | */ 127 | + (instancetype)mj_objectWithKeyValues:(id)keyValues context:(NSManagedObjectContext *)context; 128 | 129 | /** 130 | * 通过plist来创建一个模型 131 | * @param filename 文件名(仅限于mainBundle中的文件) 132 | * @return 新建的对象 133 | */ 134 | + (instancetype)mj_objectWithFilename:(NSString *)filename; 135 | 136 | /** 137 | * 通过plist来创建一个模型 138 | * @param file 文件全路径 139 | * @return 新建的对象 140 | */ 141 | + (instancetype)mj_objectWithFile:(NSString *)file; 142 | 143 | #pragma mark - 字典数组转模型数组 144 | /** 145 | * 通过字典数组来创建一个模型数组 146 | * @param keyValuesArray 字典数组(可以是NSDictionary、NSData、NSString) 147 | * @return 模型数组 148 | */ 149 | + (NSMutableArray *)mj_objectArrayWithKeyValuesArray:(id)keyValuesArray; 150 | 151 | /** 152 | * 通过字典数组来创建一个模型数组 153 | * @param keyValuesArray 字典数组(可以是NSDictionary、NSData、NSString) 154 | * @param context CoreData上下文 155 | * @return 模型数组 156 | */ 157 | + (NSMutableArray *)mj_objectArrayWithKeyValuesArray:(id)keyValuesArray context:(NSManagedObjectContext *)context; 158 | 159 | /** 160 | * 通过plist来创建一个模型数组 161 | * @param filename 文件名(仅限于mainBundle中的文件) 162 | * @return 模型数组 163 | */ 164 | + (NSMutableArray *)mj_objectArrayWithFilename:(NSString *)filename; 165 | 166 | /** 167 | * 通过plist来创建一个模型数组 168 | * @param file 文件全路径 169 | * @return 模型数组 170 | */ 171 | + (NSMutableArray *)mj_objectArrayWithFile:(NSString *)file; 172 | 173 | #pragma mark - 转换为JSON 174 | /** 175 | * 转换为JSON Data 176 | */ 177 | - (NSData *)mj_JSONData; 178 | /** 179 | * 转换为字典或者数组 180 | */ 181 | - (id)mj_JSONObject; 182 | /** 183 | * 转换为JSON 字符串 184 | */ 185 | - (NSString *)mj_JSONString; 186 | @end 187 | 188 | @interface NSObject (MJKeyValueDeprecated_v_2_5_16) 189 | - (instancetype)setKeyValues:(id)keyValue MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 190 | - (instancetype)setKeyValues:(id)keyValues error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 191 | - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 192 | - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 193 | + (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 194 | - (NSMutableDictionary *)keyValues MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 195 | - (NSMutableDictionary *)keyValuesWithError:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 196 | - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 197 | - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 198 | - (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 199 | - (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 200 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 201 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 202 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray keys:(NSArray *)keys MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 203 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray keys:(NSArray *)keys error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 204 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray ignoredKeys:(NSArray *)ignoredKeys MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 205 | + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray ignoredKeys:(NSArray *)ignoredKeys error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 206 | + (instancetype)objectWithKeyValues:(id)keyValues MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 207 | + (instancetype)objectWithKeyValues:(id)keyValues error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 208 | + (instancetype)objectWithKeyValues:(id)keyValues context:(NSManagedObjectContext *)context MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 209 | + (instancetype)objectWithKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 210 | + (instancetype)objectWithFilename:(NSString *)filename MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 211 | + (instancetype)objectWithFilename:(NSString *)filename error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 212 | + (instancetype)objectWithFile:(NSString *)file MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 213 | + (instancetype)objectWithFile:(NSString *)file error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 214 | + (NSMutableArray *)objectArrayWithKeyValuesArray:(id)keyValuesArray MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 215 | + (NSMutableArray *)objectArrayWithKeyValuesArray:(id)keyValuesArray error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 216 | + (NSMutableArray *)objectArrayWithKeyValuesArray:(id)keyValuesArray context:(NSManagedObjectContext *)context MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 217 | + (NSMutableArray *)objectArrayWithKeyValuesArray:(id)keyValuesArray context:(NSManagedObjectContext *)context error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 218 | + (NSMutableArray *)objectArrayWithFilename:(NSString *)filename MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 219 | + (NSMutableArray *)objectArrayWithFilename:(NSString *)filename error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 220 | + (NSMutableArray *)objectArrayWithFile:(NSString *)file MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 221 | + (NSMutableArray *)objectArrayWithFile:(NSString *)file error:(NSError **)error MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 222 | - (NSData *)JSONData MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 223 | - (id)JSONObject MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 224 | - (NSString *)JSONString MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 225 | @end 226 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJProperty.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJProperty.h 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/4/17. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "MJExtensionConst.h" 11 | 12 | @class MJProperty; 13 | 14 | /** 15 | * 遍历成员变量用的block 16 | * 17 | * @param property 成员的包装对象 18 | * @param stop YES代表停止遍历,NO代表继续遍历 19 | */ 20 | typedef void (^MJPropertiesEnumeration)(MJProperty *property, BOOL *stop); 21 | 22 | /** 将属性名换为其他key去字典中取值 */ 23 | typedef NSDictionary * (^MJReplacedKeyFromPropertyName)(); 24 | typedef id (^MJReplacedKeyFromPropertyName121)(NSString *propertyName); 25 | /** 数组中需要转换的模型类 */ 26 | typedef NSDictionary * (^MJObjectClassInArray)(); 27 | /** 用于过滤字典中的值 */ 28 | typedef id (^MJNewValueFromOldValue)(id object, id oldValue, MJProperty *property); 29 | 30 | /** 31 | * 成员属性相关的扩展 32 | */ 33 | @interface NSObject (MJProperty) 34 | #pragma mark - 遍历 35 | /** 36 | * 遍历所有的成员 37 | */ 38 | + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration; 39 | 40 | #pragma mark - 新值配置 41 | /** 42 | * 用于过滤字典中的值 43 | * 44 | * @param newValueFormOldValue 用于过滤字典中的值 45 | */ 46 | + (void)mj_setupNewValueFromOldValue:(MJNewValueFromOldValue)newValueFormOldValue; 47 | + (id)mj_getNewValueFromObject:(__unsafe_unretained id)object oldValue:(__unsafe_unretained id)oldValue property:(__unsafe_unretained MJProperty *)property; 48 | 49 | #pragma mark - key配置 50 | /** 51 | * 将属性名换为其他key去字典中取值 52 | * 53 | * @param replacedKeyFromPropertyName 将属性名换为其他key去字典中取值 54 | */ 55 | + (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName; 56 | /** 57 | * 将属性名换为其他key去字典中取值 58 | * 59 | * @param replacedKeyFromPropertyName121 将属性名换为其他key去字典中取值 60 | */ 61 | + (void)mj_setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121; 62 | 63 | #pragma mark - array model class配置 64 | /** 65 | * 数组中需要转换的模型类 66 | * 67 | * @param objectClassInArray 数组中需要转换的模型类 68 | */ 69 | + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray; 70 | @end 71 | 72 | @interface NSObject (MJPropertyDeprecated_v_2_5_16) 73 | + (void)enumerateProperties:(MJPropertiesEnumeration)enumeration MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 74 | + (void)setupNewValueFromOldValue:(MJNewValueFromOldValue)newValueFormOldValue MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 75 | + (id)getNewValueFromObject:(__unsafe_unretained id)object oldValue:(__unsafe_unretained id)oldValue property:(__unsafe_unretained MJProperty *)property MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 76 | + (void)setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 77 | + (void)setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121 MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 78 | + (void)setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 79 | @end -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSObject+MJProperty.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MJProperty.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/4/17. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MJProperty.h" 10 | #import "NSObject+MJKeyValue.h" 11 | #import "NSObject+MJCoding.h" 12 | #import "NSObject+MJClass.h" 13 | #import "MJProperty.h" 14 | #import "MJFoundation.h" 15 | #import 16 | 17 | #pragma clang diagnostic push 18 | #pragma clang diagnostic ignored "-Wundeclared-selector" 19 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" 20 | 21 | static const char MJReplacedKeyFromPropertyNameKey = '\0'; 22 | static const char MJReplacedKeyFromPropertyName121Key = '\0'; 23 | static const char MJNewValueFromOldValueKey = '\0'; 24 | static const char MJObjectClassInArrayKey = '\0'; 25 | 26 | static const char MJCachedPropertiesKey = '\0'; 27 | 28 | @implementation NSObject (Property) 29 | 30 | static NSMutableDictionary *replacedKeyFromPropertyNameDict_; 31 | static NSMutableDictionary *replacedKeyFromPropertyName121Dict_; 32 | static NSMutableDictionary *newValueFromOldValueDict_; 33 | static NSMutableDictionary *objectClassInArrayDict_; 34 | static NSMutableDictionary *cachedPropertiesDict_; 35 | 36 | + (void)load 37 | { 38 | replacedKeyFromPropertyNameDict_ = [NSMutableDictionary dictionary]; 39 | replacedKeyFromPropertyName121Dict_ = [NSMutableDictionary dictionary]; 40 | newValueFromOldValueDict_ = [NSMutableDictionary dictionary]; 41 | objectClassInArrayDict_ = [NSMutableDictionary dictionary]; 42 | cachedPropertiesDict_ = [NSMutableDictionary dictionary]; 43 | } 44 | 45 | + (NSMutableDictionary *)dictForKey:(const void *)key 46 | { 47 | @synchronized (self) { 48 | if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_; 49 | if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_; 50 | if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_; 51 | if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_; 52 | if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_; 53 | return nil; 54 | } 55 | } 56 | 57 | #pragma mark - --私有方法-- 58 | + (id)propertyKey:(NSString *)propertyName 59 | { 60 | MJExtensionAssertParamNotNil2(propertyName, nil); 61 | 62 | __block id key = nil; 63 | // 查看有没有需要替换的key 64 | if ([self respondsToSelector:@selector(mj_replacedKeyFromPropertyName121:)]) { 65 | key = [self mj_replacedKeyFromPropertyName121:propertyName]; 66 | } 67 | // 兼容旧版本 68 | if ([self respondsToSelector:@selector(replacedKeyFromPropertyName121:)]) { 69 | key = [self performSelector:@selector(replacedKeyFromPropertyName121) withObject:propertyName]; 70 | } 71 | 72 | // 调用block 73 | if (!key) { 74 | [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) { 75 | MJReplacedKeyFromPropertyName121 block = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyName121Key); 76 | if (block) { 77 | key = block(propertyName); 78 | } 79 | if (key) *stop = YES; 80 | }]; 81 | } 82 | 83 | // 查看有没有需要替换的key 84 | if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(mj_replacedKeyFromPropertyName)]) { 85 | key = [self mj_replacedKeyFromPropertyName][propertyName]; 86 | } 87 | // 兼容旧版本 88 | if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(replacedKeyFromPropertyName)]) { 89 | key = [self performSelector:@selector(replacedKeyFromPropertyName)][propertyName]; 90 | } 91 | 92 | if (!key || [key isEqual:propertyName]) { 93 | [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) { 94 | NSDictionary *dict = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyNameKey); 95 | if (dict) { 96 | key = dict[propertyName]; 97 | } 98 | if (key && ![key isEqual:propertyName]) *stop = YES; 99 | }]; 100 | } 101 | 102 | // 2.用属性名作为key 103 | if (!key) key = propertyName; 104 | 105 | return key; 106 | } 107 | 108 | + (Class)propertyObjectClassInArray:(NSString *)propertyName 109 | { 110 | __block id clazz = nil; 111 | if ([self respondsToSelector:@selector(mj_objectClassInArray)]) { 112 | clazz = [self mj_objectClassInArray][propertyName]; 113 | } 114 | // 兼容旧版本 115 | if ([self respondsToSelector:@selector(objectClassInArray)]) { 116 | clazz = [self performSelector:@selector(objectClassInArray)][propertyName]; 117 | } 118 | 119 | if (!clazz) { 120 | [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) { 121 | NSDictionary *dict = objc_getAssociatedObject(c, &MJObjectClassInArrayKey); 122 | if (dict) { 123 | clazz = dict[propertyName]; 124 | } 125 | if (clazz) *stop = YES; 126 | }]; 127 | } 128 | 129 | // 如果是NSString类型 130 | if ([clazz isKindOfClass:[NSString class]]) { 131 | clazz = NSClassFromString(clazz); 132 | } 133 | return clazz; 134 | } 135 | 136 | #pragma mark - --公共方法-- 137 | + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration 138 | { 139 | // 获得成员变量 140 | NSArray *cachedProperties = [self properties]; 141 | 142 | // 遍历成员变量 143 | BOOL stop = NO; 144 | for (MJProperty *property in cachedProperties) { 145 | enumeration(property, &stop); 146 | if (stop) break; 147 | } 148 | } 149 | 150 | #pragma mark - 公共方法 151 | + (NSMutableArray *)properties 152 | { 153 | NSMutableArray *cachedProperties = [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)]; 154 | 155 | if (cachedProperties == nil) { 156 | cachedProperties = [NSMutableArray array]; 157 | 158 | [self mj_enumerateClasses:^(__unsafe_unretained Class c, BOOL *stop) { 159 | // 1.获得所有的成员变量 160 | unsigned int outCount = 0; 161 | objc_property_t *properties = class_copyPropertyList(c, &outCount); 162 | 163 | // 2.遍历每一个成员变量 164 | for (unsigned int i = 0; i 10 | #import "MJExtensionConst.h" 11 | 12 | @interface NSString (MJExtension) 13 | /** 14 | * 驼峰转下划线(loveYou -> love_you) 15 | */ 16 | - (NSString *)mj_underlineFromCamel; 17 | /** 18 | * 下划线转驼峰(love_you -> loveYou) 19 | */ 20 | - (NSString *)mj_camelFromUnderline; 21 | /** 22 | * 首字母变大写 23 | */ 24 | - (NSString *)mj_firstCharUpper; 25 | /** 26 | * 首字母变小写 27 | */ 28 | - (NSString *)mj_firstCharLower; 29 | 30 | - (BOOL)mj_isPureInt; 31 | 32 | - (NSURL *)mj_url; 33 | @end 34 | 35 | @interface NSString (MJExtensionDeprecated_v_2_5_16) 36 | - (NSString *)underlineFromCamel MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 37 | - (NSString *)camelFromUnderline MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 38 | - (NSString *)firstCharUpper MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 39 | - (NSString *)firstCharLower MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 40 | - (BOOL)isPureInt MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 41 | - (NSURL *)url MJExtensionDeprecated("请在方法名前面加上mj_前缀,使用mj_***"); 42 | @end 43 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/MJExtension/NSString+MJExtension.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+MJExtension.m 3 | // MJExtensionExample 4 | // 5 | // Created by MJ Lee on 15/6/7. 6 | // Copyright (c) 2015年 小码哥. All rights reserved. 7 | // 8 | 9 | #import "NSString+MJExtension.h" 10 | 11 | @implementation NSString (MJExtension) 12 | - (NSString *)mj_underlineFromCamel 13 | { 14 | if (self.length == 0) return self; 15 | NSMutableString *string = [NSMutableString string]; 16 | for (NSUInteger i = 0; i= 2) [string appendString:[cmp substringFromIndex:1]]; 40 | } else { 41 | [string appendString:cmp]; 42 | } 43 | } 44 | return string; 45 | } 46 | 47 | - (NSString *)mj_firstCharLower 48 | { 49 | if (self.length == 0) return self; 50 | NSMutableString *string = [NSMutableString string]; 51 | [string appendString:[NSString stringWithFormat:@"%c", [self characterAtIndex:0]].lowercaseString]; 52 | if (self.length >= 2) [string appendString:[self substringFromIndex:1]]; 53 | return string; 54 | } 55 | 56 | - (NSString *)mj_firstCharUpper 57 | { 58 | if (self.length == 0) return self; 59 | NSMutableString *string = [NSMutableString string]; 60 | [string appendString:[NSString stringWithFormat:@"%c", [self characterAtIndex:0]].uppercaseString]; 61 | if (self.length >= 2) [string appendString:[self substringFromIndex:1]]; 62 | return string; 63 | } 64 | 65 | - (BOOL)mj_isPureInt 66 | { 67 | NSScanner *scan = [NSScanner scannerWithString:self]; 68 | int val; 69 | return [scan scanInt:&val] && [scan isAtEnd]; 70 | } 71 | 72 | - (NSURL *)mj_url 73 | { 74 | // [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"!$&'()*+,-./:;=?@_~%#[]"]]; 75 | #pragma clang diagnostic push 76 | #pragma clang diagnostic ignored"-Wdeprecated-declarations" 77 | return [NSURL URLWithString:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL,kCFStringEncodingUTF8))]; 78 | #pragma clang diagnostic pop 79 | } 80 | @end 81 | 82 | @implementation NSString (MJExtensionDeprecated_v_2_5_16) 83 | - (NSString *)underlineFromCamel 84 | { 85 | return self.mj_underlineFromCamel; 86 | } 87 | 88 | - (NSString *)camelFromUnderline 89 | { 90 | return self.mj_camelFromUnderline; 91 | } 92 | 93 | - (NSString *)firstCharLower 94 | { 95 | return self.mj_firstCharLower; 96 | } 97 | 98 | - (NSString *)firstCharUpper 99 | { 100 | return self.mj_firstCharUpper; 101 | } 102 | 103 | - (BOOL)isPureInt 104 | { 105 | return self.mj_isPureInt; 106 | } 107 | 108 | - (NSURL *)url 109 | { 110 | return self.mj_url; 111 | } 112 | @end 113 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASCompositeConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | /** 13 | * A group of MASConstraint objects 14 | */ 15 | @interface MASCompositeConstraint : MASConstraint 16 | 17 | /** 18 | * Creates a composite with a predefined array of children 19 | * 20 | * @param children child MASConstraints 21 | * 22 | * @return a composite constraint 23 | */ 24 | - (id)initWithChildren:(NSArray *)children; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASCompositeConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASCompositeConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | 12 | @interface MASCompositeConstraint () 13 | 14 | @property (nonatomic, strong) id mas_key; 15 | @property (nonatomic, strong) NSMutableArray *childConstraints; 16 | 17 | @end 18 | 19 | @implementation MASCompositeConstraint 20 | 21 | - (id)initWithChildren:(NSArray *)children { 22 | self = [super init]; 23 | if (!self) return nil; 24 | 25 | _childConstraints = [children mutableCopy]; 26 | for (MASConstraint *constraint in _childConstraints) { 27 | constraint.delegate = self; 28 | } 29 | 30 | return self; 31 | } 32 | 33 | #pragma mark - MASConstraintDelegate 34 | 35 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 36 | NSUInteger index = [self.childConstraints indexOfObject:constraint]; 37 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 38 | [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint]; 39 | } 40 | 41 | - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 42 | id strongDelegate = self.delegate; 43 | MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 44 | newConstraint.delegate = self; 45 | [self.childConstraints addObject:newConstraint]; 46 | return newConstraint; 47 | } 48 | 49 | #pragma mark - NSLayoutConstraint multiplier proxies 50 | 51 | - (MASConstraint * (^)(CGFloat))multipliedBy { 52 | return ^id(CGFloat multiplier) { 53 | for (MASConstraint *constraint in self.childConstraints) { 54 | constraint.multipliedBy(multiplier); 55 | } 56 | return self; 57 | }; 58 | } 59 | 60 | - (MASConstraint * (^)(CGFloat))dividedBy { 61 | return ^id(CGFloat divider) { 62 | for (MASConstraint *constraint in self.childConstraints) { 63 | constraint.dividedBy(divider); 64 | } 65 | return self; 66 | }; 67 | } 68 | 69 | #pragma mark - MASLayoutPriority proxy 70 | 71 | - (MASConstraint * (^)(MASLayoutPriority))priority { 72 | return ^id(MASLayoutPriority priority) { 73 | for (MASConstraint *constraint in self.childConstraints) { 74 | constraint.priority(priority); 75 | } 76 | return self; 77 | }; 78 | } 79 | 80 | #pragma mark - NSLayoutRelation proxy 81 | 82 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 83 | return ^id(id attr, NSLayoutRelation relation) { 84 | for (MASConstraint *constraint in self.childConstraints.copy) { 85 | constraint.equalToWithRelation(attr, relation); 86 | } 87 | return self; 88 | }; 89 | } 90 | 91 | #pragma mark - attribute chaining 92 | 93 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 94 | [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 95 | return self; 96 | } 97 | 98 | #pragma mark - Animator proxy 99 | 100 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 101 | 102 | - (MASConstraint *)animator { 103 | for (MASConstraint *constraint in self.childConstraints) { 104 | [constraint animator]; 105 | } 106 | return self; 107 | } 108 | 109 | #endif 110 | 111 | #pragma mark - debug helpers 112 | 113 | - (MASConstraint * (^)(id))key { 114 | return ^id(id key) { 115 | self.mas_key = key; 116 | int i = 0; 117 | for (MASConstraint *constraint in self.childConstraints) { 118 | constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]); 119 | } 120 | return self; 121 | }; 122 | } 123 | 124 | #pragma mark - NSLayoutConstraint constant setters 125 | 126 | - (void)setInsets:(MASEdgeInsets)insets { 127 | for (MASConstraint *constraint in self.childConstraints) { 128 | constraint.insets = insets; 129 | } 130 | } 131 | 132 | - (void)setInset:(CGFloat)inset { 133 | for (MASConstraint *constraint in self.childConstraints) { 134 | constraint.inset = inset; 135 | } 136 | } 137 | 138 | - (void)setOffset:(CGFloat)offset { 139 | for (MASConstraint *constraint in self.childConstraints) { 140 | constraint.offset = offset; 141 | } 142 | } 143 | 144 | - (void)setSizeOffset:(CGSize)sizeOffset { 145 | for (MASConstraint *constraint in self.childConstraints) { 146 | constraint.sizeOffset = sizeOffset; 147 | } 148 | } 149 | 150 | - (void)setCenterOffset:(CGPoint)centerOffset { 151 | for (MASConstraint *constraint in self.childConstraints) { 152 | constraint.centerOffset = centerOffset; 153 | } 154 | } 155 | 156 | #pragma mark - MASConstraint 157 | 158 | - (void)activate { 159 | for (MASConstraint *constraint in self.childConstraints) { 160 | [constraint activate]; 161 | } 162 | } 163 | 164 | - (void)deactivate { 165 | for (MASConstraint *constraint in self.childConstraints) { 166 | [constraint deactivate]; 167 | } 168 | } 169 | 170 | - (void)install { 171 | for (MASConstraint *constraint in self.childConstraints) { 172 | constraint.updateExisting = self.updateExisting; 173 | [constraint install]; 174 | } 175 | } 176 | 177 | - (void)uninstall { 178 | for (MASConstraint *constraint in self.childConstraints) { 179 | [constraint uninstall]; 180 | } 181 | } 182 | 183 | @end 184 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASConstraint+Private.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint+Private.h 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 29/04/14. 6 | // Copyright (c) 2014 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | 11 | @protocol MASConstraintDelegate; 12 | 13 | 14 | @interface MASConstraint () 15 | 16 | /** 17 | * Whether or not to check for an existing constraint instead of adding constraint 18 | */ 19 | @property (nonatomic, assign) BOOL updateExisting; 20 | 21 | /** 22 | * Usually MASConstraintMaker but could be a parent MASConstraint 23 | */ 24 | @property (nonatomic, weak) id delegate; 25 | 26 | /** 27 | * Based on a provided value type, is equal to calling: 28 | * NSNumber - setOffset: 29 | * NSValue with CGPoint - setPointOffset: 30 | * NSValue with CGSize - setSizeOffset: 31 | * NSValue with MASEdgeInsets - setInsets: 32 | */ 33 | - (void)setLayoutConstantWithValue:(NSValue *)value; 34 | 35 | @end 36 | 37 | 38 | @interface MASConstraint (Abstract) 39 | 40 | /** 41 | * Sets the constraint relation to given NSLayoutRelation 42 | * returns a block which accepts one of the following: 43 | * MASViewAttribute, UIView, NSValue, NSArray 44 | * see readme for more details. 45 | */ 46 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation; 47 | 48 | /** 49 | * Override to set a custom chaining behaviour 50 | */ 51 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 52 | 53 | @end 54 | 55 | 56 | @protocol MASConstraintDelegate 57 | 58 | /** 59 | * Notifies the delegate when the constraint needs to be replaced with another constraint. For example 60 | * A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks 61 | */ 62 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint; 63 | 64 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * Enables Constraints to be created with chainable syntax 13 | * Constraint can represent single NSLayoutConstraint (MASViewConstraint) 14 | * or a group of NSLayoutConstraints (MASComposisteConstraint) 15 | */ 16 | @interface MASConstraint : NSObject 17 | 18 | // Chaining Support 19 | 20 | /** 21 | * Modifies the NSLayoutConstraint constant, 22 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 23 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 24 | */ 25 | - (MASConstraint * (^)(MASEdgeInsets insets))insets; 26 | 27 | /** 28 | * Modifies the NSLayoutConstraint constant, 29 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 30 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 31 | */ 32 | - (MASConstraint * (^)(CGFloat inset))inset; 33 | 34 | /** 35 | * Modifies the NSLayoutConstraint constant, 36 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 37 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 38 | */ 39 | - (MASConstraint * (^)(CGSize offset))sizeOffset; 40 | 41 | /** 42 | * Modifies the NSLayoutConstraint constant, 43 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 44 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 45 | */ 46 | - (MASConstraint * (^)(CGPoint offset))centerOffset; 47 | 48 | /** 49 | * Modifies the NSLayoutConstraint constant 50 | */ 51 | - (MASConstraint * (^)(CGFloat offset))offset; 52 | 53 | /** 54 | * Modifies the NSLayoutConstraint constant based on a value type 55 | */ 56 | - (MASConstraint * (^)(NSValue *value))valueOffset; 57 | 58 | /** 59 | * Sets the NSLayoutConstraint multiplier property 60 | */ 61 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy; 62 | 63 | /** 64 | * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy 65 | */ 66 | - (MASConstraint * (^)(CGFloat divider))dividedBy; 67 | 68 | /** 69 | * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority 70 | */ 71 | - (MASConstraint * (^)(MASLayoutPriority priority))priority; 72 | 73 | /** 74 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow 75 | */ 76 | - (MASConstraint * (^)())priorityLow; 77 | 78 | /** 79 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium 80 | */ 81 | - (MASConstraint * (^)())priorityMedium; 82 | 83 | /** 84 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh 85 | */ 86 | - (MASConstraint * (^)())priorityHigh; 87 | 88 | /** 89 | * Sets the constraint relation to NSLayoutRelationEqual 90 | * returns a block which accepts one of the following: 91 | * MASViewAttribute, UIView, NSValue, NSArray 92 | * see readme for more details. 93 | */ 94 | - (MASConstraint * (^)(id attr))equalTo; 95 | 96 | /** 97 | * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual 98 | * returns a block which accepts one of the following: 99 | * MASViewAttribute, UIView, NSValue, NSArray 100 | * see readme for more details. 101 | */ 102 | - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; 103 | 104 | /** 105 | * Sets the constraint relation to NSLayoutRelationLessThanOrEqual 106 | * returns a block which accepts one of the following: 107 | * MASViewAttribute, UIView, NSValue, NSArray 108 | * see readme for more details. 109 | */ 110 | - (MASConstraint * (^)(id attr))lessThanOrEqualTo; 111 | 112 | /** 113 | * Optional semantic property which has no effect but improves the readability of constraint 114 | */ 115 | - (MASConstraint *)with; 116 | 117 | /** 118 | * Optional semantic property which has no effect but improves the readability of constraint 119 | */ 120 | - (MASConstraint *)and; 121 | 122 | /** 123 | * Creates a new MASCompositeConstraint with the called attribute and reciever 124 | */ 125 | - (MASConstraint *)left; 126 | - (MASConstraint *)top; 127 | - (MASConstraint *)right; 128 | - (MASConstraint *)bottom; 129 | - (MASConstraint *)leading; 130 | - (MASConstraint *)trailing; 131 | - (MASConstraint *)width; 132 | - (MASConstraint *)height; 133 | - (MASConstraint *)centerX; 134 | - (MASConstraint *)centerY; 135 | - (MASConstraint *)baseline; 136 | 137 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 138 | 139 | - (MASConstraint *)firstBaseline; 140 | - (MASConstraint *)lastBaseline; 141 | 142 | #endif 143 | 144 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 145 | 146 | - (MASConstraint *)leftMargin; 147 | - (MASConstraint *)rightMargin; 148 | - (MASConstraint *)topMargin; 149 | - (MASConstraint *)bottomMargin; 150 | - (MASConstraint *)leadingMargin; 151 | - (MASConstraint *)trailingMargin; 152 | - (MASConstraint *)centerXWithinMargins; 153 | - (MASConstraint *)centerYWithinMargins; 154 | 155 | #endif 156 | 157 | 158 | /** 159 | * Sets the constraint debug name 160 | */ 161 | - (MASConstraint * (^)(id key))key; 162 | 163 | // NSLayoutConstraint constant Setters 164 | // for use outside of mas_updateConstraints/mas_makeConstraints blocks 165 | 166 | /** 167 | * Modifies the NSLayoutConstraint constant, 168 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 169 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 170 | */ 171 | - (void)setInsets:(MASEdgeInsets)insets; 172 | 173 | /** 174 | * Modifies the NSLayoutConstraint constant, 175 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 176 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 177 | */ 178 | - (void)setInset:(CGFloat)inset; 179 | 180 | /** 181 | * Modifies the NSLayoutConstraint constant, 182 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 183 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 184 | */ 185 | - (void)setSizeOffset:(CGSize)sizeOffset; 186 | 187 | /** 188 | * Modifies the NSLayoutConstraint constant, 189 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 190 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 191 | */ 192 | - (void)setCenterOffset:(CGPoint)centerOffset; 193 | 194 | /** 195 | * Modifies the NSLayoutConstraint constant 196 | */ 197 | - (void)setOffset:(CGFloat)offset; 198 | 199 | 200 | // NSLayoutConstraint Installation support 201 | 202 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 203 | /** 204 | * Whether or not to go through the animator proxy when modifying the constraint 205 | */ 206 | @property (nonatomic, copy, readonly) MASConstraint *animator; 207 | #endif 208 | 209 | /** 210 | * Activates an NSLayoutConstraint if it's supported by an OS. 211 | * Invokes install otherwise. 212 | */ 213 | - (void)activate; 214 | 215 | /** 216 | * Deactivates previously installed/activated NSLayoutConstraint. 217 | */ 218 | - (void)deactivate; 219 | 220 | /** 221 | * Creates a NSLayoutConstraint and adds it to the appropriate view. 222 | */ 223 | - (void)install; 224 | 225 | /** 226 | * Removes previously installed NSLayoutConstraint 227 | */ 228 | - (void)uninstall; 229 | 230 | @end 231 | 232 | 233 | /** 234 | * Convenience auto-boxing macros for MASConstraint methods. 235 | * 236 | * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax. 237 | * A potential drawback of this is that the unprefixed macros will appear in global scope. 238 | */ 239 | #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__))) 240 | #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 241 | #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 242 | 243 | #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__))) 244 | 245 | 246 | #ifdef MAS_SHORTHAND_GLOBALS 247 | 248 | #define equalTo(...) mas_equalTo(__VA_ARGS__) 249 | #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__) 250 | #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__) 251 | 252 | #define offset(...) mas_offset(__VA_ARGS__) 253 | 254 | #endif 255 | 256 | 257 | @interface MASConstraint (AutoboxingSupport) 258 | 259 | /** 260 | * Aliases to corresponding relation methods (for shorthand macros) 261 | * Also needed to aid autocompletion 262 | */ 263 | - (MASConstraint * (^)(id attr))mas_equalTo; 264 | - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo; 265 | - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo; 266 | 267 | /** 268 | * A dummy method to aid autocompletion 269 | */ 270 | - (MASConstraint * (^)(id offset))mas_offset; 271 | 272 | @end 273 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.m 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 1/20/14. 6 | // 7 | 8 | #import "MASConstraint.h" 9 | #import "MASConstraint+Private.h" 10 | 11 | #define MASMethodNotImplemented() \ 12 | @throw [NSException exceptionWithName:NSInternalInconsistencyException \ 13 | reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \ 14 | userInfo:nil] 15 | 16 | @implementation MASConstraint 17 | 18 | #pragma mark - Init 19 | 20 | - (id)init { 21 | NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly."); 22 | return [super init]; 23 | } 24 | 25 | #pragma mark - NSLayoutRelation proxies 26 | 27 | - (MASConstraint * (^)(id))equalTo { 28 | return ^id(id attribute) { 29 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 30 | }; 31 | } 32 | 33 | - (MASConstraint * (^)(id))mas_equalTo { 34 | return ^id(id attribute) { 35 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 36 | }; 37 | } 38 | 39 | - (MASConstraint * (^)(id))greaterThanOrEqualTo { 40 | return ^id(id attribute) { 41 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 42 | }; 43 | } 44 | 45 | - (MASConstraint * (^)(id))mas_greaterThanOrEqualTo { 46 | return ^id(id attribute) { 47 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 48 | }; 49 | } 50 | 51 | - (MASConstraint * (^)(id))lessThanOrEqualTo { 52 | return ^id(id attribute) { 53 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 54 | }; 55 | } 56 | 57 | - (MASConstraint * (^)(id))mas_lessThanOrEqualTo { 58 | return ^id(id attribute) { 59 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 60 | }; 61 | } 62 | 63 | #pragma mark - MASLayoutPriority proxies 64 | 65 | - (MASConstraint * (^)())priorityLow { 66 | return ^id{ 67 | self.priority(MASLayoutPriorityDefaultLow); 68 | return self; 69 | }; 70 | } 71 | 72 | - (MASConstraint * (^)())priorityMedium { 73 | return ^id{ 74 | self.priority(MASLayoutPriorityDefaultMedium); 75 | return self; 76 | }; 77 | } 78 | 79 | - (MASConstraint * (^)())priorityHigh { 80 | return ^id{ 81 | self.priority(MASLayoutPriorityDefaultHigh); 82 | return self; 83 | }; 84 | } 85 | 86 | #pragma mark - NSLayoutConstraint constant proxies 87 | 88 | - (MASConstraint * (^)(MASEdgeInsets))insets { 89 | return ^id(MASEdgeInsets insets){ 90 | self.insets = insets; 91 | return self; 92 | }; 93 | } 94 | 95 | - (MASConstraint * (^)(CGFloat))inset { 96 | return ^id(CGFloat inset){ 97 | self.inset = inset; 98 | return self; 99 | }; 100 | } 101 | 102 | - (MASConstraint * (^)(CGSize))sizeOffset { 103 | return ^id(CGSize offset) { 104 | self.sizeOffset = offset; 105 | return self; 106 | }; 107 | } 108 | 109 | - (MASConstraint * (^)(CGPoint))centerOffset { 110 | return ^id(CGPoint offset) { 111 | self.centerOffset = offset; 112 | return self; 113 | }; 114 | } 115 | 116 | - (MASConstraint * (^)(CGFloat))offset { 117 | return ^id(CGFloat offset){ 118 | self.offset = offset; 119 | return self; 120 | }; 121 | } 122 | 123 | - (MASConstraint * (^)(NSValue *value))valueOffset { 124 | return ^id(NSValue *offset) { 125 | NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset); 126 | [self setLayoutConstantWithValue:offset]; 127 | return self; 128 | }; 129 | } 130 | 131 | - (MASConstraint * (^)(id offset))mas_offset { 132 | // Will never be called due to macro 133 | return nil; 134 | } 135 | 136 | #pragma mark - NSLayoutConstraint constant setter 137 | 138 | - (void)setLayoutConstantWithValue:(NSValue *)value { 139 | if ([value isKindOfClass:NSNumber.class]) { 140 | self.offset = [(NSNumber *)value doubleValue]; 141 | } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { 142 | CGPoint point; 143 | [value getValue:&point]; 144 | self.centerOffset = point; 145 | } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { 146 | CGSize size; 147 | [value getValue:&size]; 148 | self.sizeOffset = size; 149 | } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { 150 | MASEdgeInsets insets; 151 | [value getValue:&insets]; 152 | self.insets = insets; 153 | } else { 154 | NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); 155 | } 156 | } 157 | 158 | #pragma mark - Semantic properties 159 | 160 | - (MASConstraint *)with { 161 | return self; 162 | } 163 | 164 | - (MASConstraint *)and { 165 | return self; 166 | } 167 | 168 | #pragma mark - Chaining 169 | 170 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute { 171 | MASMethodNotImplemented(); 172 | } 173 | 174 | - (MASConstraint *)left { 175 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 176 | } 177 | 178 | - (MASConstraint *)top { 179 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 180 | } 181 | 182 | - (MASConstraint *)right { 183 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 184 | } 185 | 186 | - (MASConstraint *)bottom { 187 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 188 | } 189 | 190 | - (MASConstraint *)leading { 191 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 192 | } 193 | 194 | - (MASConstraint *)trailing { 195 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 196 | } 197 | 198 | - (MASConstraint *)width { 199 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 200 | } 201 | 202 | - (MASConstraint *)height { 203 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 204 | } 205 | 206 | - (MASConstraint *)centerX { 207 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 208 | } 209 | 210 | - (MASConstraint *)centerY { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 212 | } 213 | 214 | - (MASConstraint *)baseline { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 216 | } 217 | 218 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 219 | 220 | - (MASConstraint *)firstBaseline { 221 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 222 | } 223 | - (MASConstraint *)lastBaseline { 224 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 225 | } 226 | 227 | #endif 228 | 229 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 230 | 231 | - (MASConstraint *)leftMargin { 232 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 233 | } 234 | 235 | - (MASConstraint *)rightMargin { 236 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 237 | } 238 | 239 | - (MASConstraint *)topMargin { 240 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 241 | } 242 | 243 | - (MASConstraint *)bottomMargin { 244 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 245 | } 246 | 247 | - (MASConstraint *)leadingMargin { 248 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 249 | } 250 | 251 | - (MASConstraint *)trailingMargin { 252 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 253 | } 254 | 255 | - (MASConstraint *)centerXWithinMargins { 256 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 257 | } 258 | 259 | - (MASConstraint *)centerYWithinMargins { 260 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 261 | } 262 | 263 | #endif 264 | 265 | #pragma mark - Abstract 266 | 267 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } 268 | 269 | - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); } 270 | 271 | - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); } 272 | 273 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); } 274 | 275 | - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); } 276 | 277 | - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); } 278 | 279 | - (void)setInset:(CGFloat __unused)inset { MASMethodNotImplemented(); } 280 | 281 | - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); } 282 | 283 | - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); } 284 | 285 | - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); } 286 | 287 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 288 | 289 | - (MASConstraint *)animator { MASMethodNotImplemented(); } 290 | 291 | #endif 292 | 293 | - (void)activate { MASMethodNotImplemented(); } 294 | 295 | - (void)deactivate { MASMethodNotImplemented(); } 296 | 297 | - (void)install { MASMethodNotImplemented(); } 298 | 299 | - (void)uninstall { MASMethodNotImplemented(); } 300 | 301 | @end 302 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASConstraintMaker.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintBuilder.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | typedef NS_OPTIONS(NSInteger, MASAttribute) { 13 | MASAttributeLeft = 1 << NSLayoutAttributeLeft, 14 | MASAttributeRight = 1 << NSLayoutAttributeRight, 15 | MASAttributeTop = 1 << NSLayoutAttributeTop, 16 | MASAttributeBottom = 1 << NSLayoutAttributeBottom, 17 | MASAttributeLeading = 1 << NSLayoutAttributeLeading, 18 | MASAttributeTrailing = 1 << NSLayoutAttributeTrailing, 19 | MASAttributeWidth = 1 << NSLayoutAttributeWidth, 20 | MASAttributeHeight = 1 << NSLayoutAttributeHeight, 21 | MASAttributeCenterX = 1 << NSLayoutAttributeCenterX, 22 | MASAttributeCenterY = 1 << NSLayoutAttributeCenterY, 23 | MASAttributeBaseline = 1 << NSLayoutAttributeBaseline, 24 | 25 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 26 | 27 | MASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline, 28 | MASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline, 29 | 30 | #endif 31 | 32 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 33 | 34 | MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, 35 | MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, 36 | MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, 37 | MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, 38 | MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, 39 | MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, 40 | MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, 41 | MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, 42 | 43 | #endif 44 | 45 | }; 46 | 47 | /** 48 | * Provides factory methods for creating MASConstraints. 49 | * Constraints are collected until they are ready to be installed 50 | * 51 | */ 52 | @interface MASConstraintMaker : NSObject 53 | 54 | /** 55 | * The following properties return a new MASViewConstraint 56 | * with the first item set to the makers associated view and the appropriate MASViewAttribute 57 | */ 58 | @property (nonatomic, strong, readonly) MASConstraint *left; 59 | @property (nonatomic, strong, readonly) MASConstraint *top; 60 | @property (nonatomic, strong, readonly) MASConstraint *right; 61 | @property (nonatomic, strong, readonly) MASConstraint *bottom; 62 | @property (nonatomic, strong, readonly) MASConstraint *leading; 63 | @property (nonatomic, strong, readonly) MASConstraint *trailing; 64 | @property (nonatomic, strong, readonly) MASConstraint *width; 65 | @property (nonatomic, strong, readonly) MASConstraint *height; 66 | @property (nonatomic, strong, readonly) MASConstraint *centerX; 67 | @property (nonatomic, strong, readonly) MASConstraint *centerY; 68 | @property (nonatomic, strong, readonly) MASConstraint *baseline; 69 | 70 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 71 | 72 | @property (nonatomic, strong, readonly) MASConstraint *firstBaseline; 73 | @property (nonatomic, strong, readonly) MASConstraint *lastBaseline; 74 | 75 | #endif 76 | 77 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 78 | 79 | @property (nonatomic, strong, readonly) MASConstraint *leftMargin; 80 | @property (nonatomic, strong, readonly) MASConstraint *rightMargin; 81 | @property (nonatomic, strong, readonly) MASConstraint *topMargin; 82 | @property (nonatomic, strong, readonly) MASConstraint *bottomMargin; 83 | @property (nonatomic, strong, readonly) MASConstraint *leadingMargin; 84 | @property (nonatomic, strong, readonly) MASConstraint *trailingMargin; 85 | @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; 86 | @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; 87 | 88 | #endif 89 | 90 | /** 91 | * Returns a block which creates a new MASCompositeConstraint with the first item set 92 | * to the makers associated view and children corresponding to the set bits in the 93 | * MASAttribute parameter. Combine multiple attributes via binary-or. 94 | */ 95 | @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs); 96 | 97 | /** 98 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges 99 | * which generates the appropriate MASViewConstraint children (top, left, bottom, right) 100 | * with the first item set to the makers associated view 101 | */ 102 | @property (nonatomic, strong, readonly) MASConstraint *edges; 103 | 104 | /** 105 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize 106 | * which generates the appropriate MASViewConstraint children (width, height) 107 | * with the first item set to the makers associated view 108 | */ 109 | @property (nonatomic, strong, readonly) MASConstraint *size; 110 | 111 | /** 112 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter 113 | * which generates the appropriate MASViewConstraint children (centerX, centerY) 114 | * with the first item set to the makers associated view 115 | */ 116 | @property (nonatomic, strong, readonly) MASConstraint *center; 117 | 118 | /** 119 | * Whether or not to check for an existing constraint instead of adding constraint 120 | */ 121 | @property (nonatomic, assign) BOOL updateExisting; 122 | 123 | /** 124 | * Whether or not to remove existing constraints prior to installing 125 | */ 126 | @property (nonatomic, assign) BOOL removeExisting; 127 | 128 | /** 129 | * initialises the maker with a default view 130 | * 131 | * @param view any MASConstrait are created with this view as the first item 132 | * 133 | * @return a new MASConstraintMaker 134 | */ 135 | - (id)initWithView:(MAS_VIEW *)view; 136 | 137 | /** 138 | * Calls install method on any MASConstraints which have been created by this maker 139 | * 140 | * @return an array of all the installed MASConstraints 141 | */ 142 | - (NSArray *)install; 143 | 144 | - (MASConstraint * (^)(dispatch_block_t))group; 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASConstraintMaker.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintBuilder.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraintMaker.h" 10 | #import "MASViewConstraint.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASConstraint+Private.h" 13 | #import "MASViewAttribute.h" 14 | #import "View+MASAdditions.h" 15 | 16 | @interface MASConstraintMaker () 17 | 18 | @property (nonatomic, weak) MAS_VIEW *view; 19 | @property (nonatomic, strong) NSMutableArray *constraints; 20 | 21 | @end 22 | 23 | @implementation MASConstraintMaker 24 | 25 | - (id)initWithView:(MAS_VIEW *)view { 26 | self = [super init]; 27 | if (!self) return nil; 28 | 29 | self.view = view; 30 | self.constraints = NSMutableArray.new; 31 | 32 | return self; 33 | } 34 | 35 | - (NSArray *)install { 36 | if (self.removeExisting) { 37 | NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view]; 38 | for (MASConstraint *constraint in installedConstraints) { 39 | [constraint uninstall]; 40 | } 41 | } 42 | NSArray *constraints = self.constraints.copy; 43 | for (MASConstraint *constraint in constraints) { 44 | constraint.updateExisting = self.updateExisting; 45 | [constraint install]; 46 | } 47 | [self.constraints removeAllObjects]; 48 | return constraints; 49 | } 50 | 51 | #pragma mark - MASConstraintDelegate 52 | 53 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 54 | NSUInteger index = [self.constraints indexOfObject:constraint]; 55 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 56 | [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint]; 57 | } 58 | 59 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 60 | MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute]; 61 | MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute]; 62 | if ([constraint isKindOfClass:MASViewConstraint.class]) { 63 | //replace with composite constraint 64 | NSArray *children = @[constraint, newConstraint]; 65 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 66 | compositeConstraint.delegate = self; 67 | [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint]; 68 | return compositeConstraint; 69 | } 70 | if (!constraint) { 71 | newConstraint.delegate = self; 72 | [self.constraints addObject:newConstraint]; 73 | } 74 | return newConstraint; 75 | } 76 | 77 | - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { 78 | __unused MASAttribute anyAttribute = (MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading 79 | | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX 80 | | MASAttributeCenterY | MASAttributeBaseline 81 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 82 | | MASAttributeFirstBaseline | MASAttributeLastBaseline 83 | #endif 84 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 85 | | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin 86 | | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins 87 | | MASAttributeCenterYWithinMargins 88 | #endif 89 | ); 90 | 91 | NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); 92 | 93 | NSMutableArray *attributes = [NSMutableArray array]; 94 | 95 | if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left]; 96 | if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right]; 97 | if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top]; 98 | if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom]; 99 | if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading]; 100 | if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing]; 101 | if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width]; 102 | if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height]; 103 | if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX]; 104 | if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; 105 | if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; 106 | 107 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 108 | 109 | if (attrs & MASAttributeFirstBaseline) [attributes addObject:self.view.mas_firstBaseline]; 110 | if (attrs & MASAttributeLastBaseline) [attributes addObject:self.view.mas_lastBaseline]; 111 | 112 | #endif 113 | 114 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 115 | 116 | if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; 117 | if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; 118 | if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; 119 | if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; 120 | if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; 121 | if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; 122 | if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; 123 | if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; 124 | 125 | #endif 126 | 127 | NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; 128 | 129 | for (MASViewAttribute *a in attributes) { 130 | [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]]; 131 | } 132 | 133 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 134 | constraint.delegate = self; 135 | [self.constraints addObject:constraint]; 136 | return constraint; 137 | } 138 | 139 | #pragma mark - standard Attributes 140 | 141 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 142 | return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute]; 143 | } 144 | 145 | - (MASConstraint *)left { 146 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 147 | } 148 | 149 | - (MASConstraint *)top { 150 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 151 | } 152 | 153 | - (MASConstraint *)right { 154 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 155 | } 156 | 157 | - (MASConstraint *)bottom { 158 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 159 | } 160 | 161 | - (MASConstraint *)leading { 162 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 163 | } 164 | 165 | - (MASConstraint *)trailing { 166 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 167 | } 168 | 169 | - (MASConstraint *)width { 170 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 171 | } 172 | 173 | - (MASConstraint *)height { 174 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 175 | } 176 | 177 | - (MASConstraint *)centerX { 178 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 179 | } 180 | 181 | - (MASConstraint *)centerY { 182 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 183 | } 184 | 185 | - (MASConstraint *)baseline { 186 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 187 | } 188 | 189 | - (MASConstraint *(^)(MASAttribute))attributes { 190 | return ^(MASAttribute attrs){ 191 | return [self addConstraintWithAttributes:attrs]; 192 | }; 193 | } 194 | 195 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 196 | 197 | - (MASConstraint *)firstBaseline { 198 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 199 | } 200 | 201 | - (MASConstraint *)lastBaseline { 202 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 203 | } 204 | 205 | #endif 206 | 207 | 208 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 209 | 210 | - (MASConstraint *)leftMargin { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 212 | } 213 | 214 | - (MASConstraint *)rightMargin { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 216 | } 217 | 218 | - (MASConstraint *)topMargin { 219 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 220 | } 221 | 222 | - (MASConstraint *)bottomMargin { 223 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 224 | } 225 | 226 | - (MASConstraint *)leadingMargin { 227 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 228 | } 229 | 230 | - (MASConstraint *)trailingMargin { 231 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 232 | } 233 | 234 | - (MASConstraint *)centerXWithinMargins { 235 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 236 | } 237 | 238 | - (MASConstraint *)centerYWithinMargins { 239 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 240 | } 241 | 242 | #endif 243 | 244 | 245 | #pragma mark - composite Attributes 246 | 247 | - (MASConstraint *)edges { 248 | return [self addConstraintWithAttributes:MASAttributeTop | MASAttributeLeft | MASAttributeRight | MASAttributeBottom]; 249 | } 250 | 251 | - (MASConstraint *)size { 252 | return [self addConstraintWithAttributes:MASAttributeWidth | MASAttributeHeight]; 253 | } 254 | 255 | - (MASConstraint *)center { 256 | return [self addConstraintWithAttributes:MASAttributeCenterX | MASAttributeCenterY]; 257 | } 258 | 259 | #pragma mark - grouping 260 | 261 | - (MASConstraint *(^)(dispatch_block_t group))group { 262 | return ^id(dispatch_block_t group) { 263 | NSInteger previousCount = self.constraints.count; 264 | group(); 265 | 266 | NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)]; 267 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 268 | constraint.delegate = self; 269 | return constraint; 270 | }; 271 | } 272 | 273 | @end 274 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASLayoutConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * When you are debugging or printing the constraints attached to a view this subclass 13 | * makes it easier to identify which constraints have been created via Masonry 14 | */ 15 | @interface MASLayoutConstraint : NSLayoutConstraint 16 | 17 | /** 18 | * a key to associate with this constraint 19 | */ 20 | @property (nonatomic, strong) id mas_key; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASLayoutConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASLayoutConstraint.h" 10 | 11 | @implementation MASLayoutConstraint 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASUtilities.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASUtilities.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 19/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | 13 | #if TARGET_OS_IPHONE || TARGET_OS_TV 14 | 15 | #import 16 | #define MAS_VIEW UIView 17 | #define MAS_VIEW_CONTROLLER UIViewController 18 | #define MASEdgeInsets UIEdgeInsets 19 | 20 | typedef UILayoutPriority MASLayoutPriority; 21 | static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired; 22 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh; 23 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500; 24 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow; 25 | static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel; 26 | 27 | #elif TARGET_OS_MAC 28 | 29 | #import 30 | #define MAS_VIEW NSView 31 | #define MASEdgeInsets NSEdgeInsets 32 | 33 | typedef NSLayoutPriority MASLayoutPriority; 34 | static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired; 35 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh; 36 | static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow; 37 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501; 38 | static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut; 39 | static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow; 40 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow; 41 | static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression; 42 | 43 | #endif 44 | 45 | /** 46 | * Allows you to attach keys to objects matching the variable names passed. 47 | * 48 | * view1.mas_key = @"view1", view2.mas_key = @"view2"; 49 | * 50 | * is equivalent to: 51 | * 52 | * MASAttachKeys(view1, view2); 53 | */ 54 | #define MASAttachKeys(...) \ 55 | { \ 56 | NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \ 57 | for (id key in keyPairs.allKeys) { \ 58 | id obj = keyPairs[key]; \ 59 | NSAssert([obj respondsToSelector:@selector(setMas_key:)], \ 60 | @"Cannot attach mas_key to %@", obj); \ 61 | [obj setMas_key:key]; \ 62 | } \ 63 | } 64 | 65 | /** 66 | * Used to create object hashes 67 | * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html 68 | */ 69 | #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger)) 70 | #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch))) 71 | 72 | /** 73 | * Given a scalar or struct value, wraps it in NSValue 74 | * Based on EXPObjectify: https://github.com/specta/expecta 75 | */ 76 | static inline id _MASBoxValue(const char *type, ...) { 77 | va_list v; 78 | va_start(v, type); 79 | id obj = nil; 80 | if (strcmp(type, @encode(id)) == 0) { 81 | id actual = va_arg(v, id); 82 | obj = actual; 83 | } else if (strcmp(type, @encode(CGPoint)) == 0) { 84 | CGPoint actual = (CGPoint)va_arg(v, CGPoint); 85 | obj = [NSValue value:&actual withObjCType:type]; 86 | } else if (strcmp(type, @encode(CGSize)) == 0) { 87 | CGSize actual = (CGSize)va_arg(v, CGSize); 88 | obj = [NSValue value:&actual withObjCType:type]; 89 | } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) { 90 | MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets); 91 | obj = [NSValue value:&actual withObjCType:type]; 92 | } else if (strcmp(type, @encode(double)) == 0) { 93 | double actual = (double)va_arg(v, double); 94 | obj = [NSNumber numberWithDouble:actual]; 95 | } else if (strcmp(type, @encode(float)) == 0) { 96 | float actual = (float)va_arg(v, double); 97 | obj = [NSNumber numberWithFloat:actual]; 98 | } else if (strcmp(type, @encode(int)) == 0) { 99 | int actual = (int)va_arg(v, int); 100 | obj = [NSNumber numberWithInt:actual]; 101 | } else if (strcmp(type, @encode(long)) == 0) { 102 | long actual = (long)va_arg(v, long); 103 | obj = [NSNumber numberWithLong:actual]; 104 | } else if (strcmp(type, @encode(long long)) == 0) { 105 | long long actual = (long long)va_arg(v, long long); 106 | obj = [NSNumber numberWithLongLong:actual]; 107 | } else if (strcmp(type, @encode(short)) == 0) { 108 | short actual = (short)va_arg(v, int); 109 | obj = [NSNumber numberWithShort:actual]; 110 | } else if (strcmp(type, @encode(char)) == 0) { 111 | char actual = (char)va_arg(v, int); 112 | obj = [NSNumber numberWithChar:actual]; 113 | } else if (strcmp(type, @encode(bool)) == 0) { 114 | bool actual = (bool)va_arg(v, int); 115 | obj = [NSNumber numberWithBool:actual]; 116 | } else if (strcmp(type, @encode(unsigned char)) == 0) { 117 | unsigned char actual = (unsigned char)va_arg(v, unsigned int); 118 | obj = [NSNumber numberWithUnsignedChar:actual]; 119 | } else if (strcmp(type, @encode(unsigned int)) == 0) { 120 | unsigned int actual = (unsigned int)va_arg(v, unsigned int); 121 | obj = [NSNumber numberWithUnsignedInt:actual]; 122 | } else if (strcmp(type, @encode(unsigned long)) == 0) { 123 | unsigned long actual = (unsigned long)va_arg(v, unsigned long); 124 | obj = [NSNumber numberWithUnsignedLong:actual]; 125 | } else if (strcmp(type, @encode(unsigned long long)) == 0) { 126 | unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long); 127 | obj = [NSNumber numberWithUnsignedLongLong:actual]; 128 | } else if (strcmp(type, @encode(unsigned short)) == 0) { 129 | unsigned short actual = (unsigned short)va_arg(v, unsigned int); 130 | obj = [NSNumber numberWithUnsignedShort:actual]; 131 | } 132 | va_end(v); 133 | return obj; 134 | } 135 | 136 | #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) 137 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASViewAttribute.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASAttribute.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * An immutable tuple which stores the view and the related NSLayoutAttribute. 13 | * Describes part of either the left or right hand side of a constraint equation 14 | */ 15 | @interface MASViewAttribute : NSObject 16 | 17 | /** 18 | * The view which the reciever relates to. Can be nil if item is not a view. 19 | */ 20 | @property (nonatomic, weak, readonly) MAS_VIEW *view; 21 | 22 | /** 23 | * The item which the reciever relates to. 24 | */ 25 | @property (nonatomic, weak, readonly) id item; 26 | 27 | /** 28 | * The attribute which the reciever relates to 29 | */ 30 | @property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute; 31 | 32 | /** 33 | * Convenience initializer. 34 | */ 35 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute; 36 | 37 | /** 38 | * The designated initializer. 39 | */ 40 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute; 41 | 42 | /** 43 | * Determine whether the layoutAttribute is a size attribute 44 | * 45 | * @return YES if layoutAttribute is equal to NSLayoutAttributeWidth or NSLayoutAttributeHeight 46 | */ 47 | - (BOOL)isSizeAttribute; 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASViewAttribute.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASAttribute.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | 11 | @implementation MASViewAttribute 12 | 13 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute { 14 | self = [self initWithView:view item:view layoutAttribute:layoutAttribute]; 15 | return self; 16 | } 17 | 18 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute { 19 | self = [super init]; 20 | if (!self) return nil; 21 | 22 | _view = view; 23 | _item = item; 24 | _layoutAttribute = layoutAttribute; 25 | 26 | return self; 27 | } 28 | 29 | - (BOOL)isSizeAttribute { 30 | return self.layoutAttribute == NSLayoutAttributeWidth 31 | || self.layoutAttribute == NSLayoutAttributeHeight; 32 | } 33 | 34 | - (BOOL)isEqual:(MASViewAttribute *)viewAttribute { 35 | if ([viewAttribute isKindOfClass:self.class]) { 36 | return self.view == viewAttribute.view 37 | && self.layoutAttribute == viewAttribute.layoutAttribute; 38 | } 39 | return [super isEqual:viewAttribute]; 40 | } 41 | 42 | - (NSUInteger)hash { 43 | return MAS_NSUINTROTATE([self.view hash], MAS_NSUINT_BIT / 2) ^ self.layoutAttribute; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/MASViewConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | #import "MASUtilities.h" 13 | 14 | /** 15 | * A single constraint. 16 | * Contains the attributes neccessary for creating a NSLayoutConstraint and adding it to the appropriate view 17 | */ 18 | @interface MASViewConstraint : MASConstraint 19 | 20 | /** 21 | * First item/view and first attribute of the NSLayoutConstraint 22 | */ 23 | @property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute; 24 | 25 | /** 26 | * Second item/view and second attribute of the NSLayoutConstraint 27 | */ 28 | @property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute; 29 | 30 | /** 31 | * initialises the MASViewConstraint with the first part of the equation 32 | * 33 | * @param firstViewAttribute view.mas_left, view.mas_width etc. 34 | * 35 | * @return a new view constraint 36 | */ 37 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute; 38 | 39 | /** 40 | * Returns all MASViewConstraints installed with this view as a first item. 41 | * 42 | * @param view A view to retrieve constraints for. 43 | * 44 | * @return An array of MASViewConstraints. 45 | */ 46 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/Masonry.h: -------------------------------------------------------------------------------- 1 | // 2 | // Masonry.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Masonry. 12 | FOUNDATION_EXPORT double MasonryVersionNumber; 13 | 14 | //! Project version string for Masonry. 15 | FOUNDATION_EXPORT const unsigned char MasonryVersionString[]; 16 | 17 | #import "MASUtilities.h" 18 | #import "View+MASAdditions.h" 19 | #import "View+MASShorthandAdditions.h" 20 | #import "ViewController+MASAdditions.h" 21 | #import "NSArray+MASAdditions.h" 22 | #import "NSArray+MASShorthandAdditions.h" 23 | #import "MASConstraint.h" 24 | #import "MASCompositeConstraint.h" 25 | #import "MASViewAttribute.h" 26 | #import "MASViewConstraint.h" 27 | #import "MASConstraintMaker.h" 28 | #import "MASLayoutConstraint.h" 29 | #import "NSLayoutConstraint+MASDebugAdditions.h" 30 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/NSArray+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.h 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | typedef NS_ENUM(NSUInteger, MASAxisType) { 14 | MASAxisTypeHorizontal, 15 | MASAxisTypeVertical 16 | }; 17 | 18 | @interface NSArray (MASAdditions) 19 | 20 | /** 21 | * Creates a MASConstraintMaker with each view in the callee. 22 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view 23 | * 24 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 25 | * 26 | * @return Array of created MASConstraints 27 | */ 28 | - (NSArray *)mas_makeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 29 | 30 | /** 31 | * Creates a MASConstraintMaker with each view in the callee. 32 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 33 | * If an existing constraint exists then it will be updated instead. 34 | * 35 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 36 | * 37 | * @return Array of created/updated MASConstraints 38 | */ 39 | - (NSArray *)mas_updateConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 40 | 41 | /** 42 | * Creates a MASConstraintMaker with each view in the callee. 43 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 44 | * All constraints previously installed for the views will be removed. 45 | * 46 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 47 | * 48 | * @return Array of created/updated MASConstraints 49 | */ 50 | - (NSArray *)mas_remakeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 51 | 52 | /** 53 | * distribute with fixed spacing 54 | * 55 | * @param axisType which axis to distribute items along 56 | * @param fixedSpacing the spacing between each item 57 | * @param leadSpacing the spacing before the first item and the container 58 | * @param tailSpacing the spacing after the last item and the container 59 | */ 60 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 61 | 62 | /** 63 | * distribute with fixed item size 64 | * 65 | * @param axisType which axis to distribute items along 66 | * @param fixedItemLength the fixed length of each item 67 | * @param leadSpacing the spacing before the first item and the container 68 | * @param tailSpacing the spacing after the last item and the container 69 | */ 70 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/NSArray+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.m 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | #import "View+MASAdditions.h" 11 | 12 | @implementation NSArray (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block { 15 | NSMutableArray *constraints = [NSMutableArray array]; 16 | for (MAS_VIEW *view in self) { 17 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 18 | [constraints addObjectsFromArray:[view mas_makeConstraints:block]]; 19 | } 20 | return constraints; 21 | } 22 | 23 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block { 24 | NSMutableArray *constraints = [NSMutableArray array]; 25 | for (MAS_VIEW *view in self) { 26 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 27 | [constraints addObjectsFromArray:[view mas_updateConstraints:block]]; 28 | } 29 | return constraints; 30 | } 31 | 32 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 33 | NSMutableArray *constraints = [NSMutableArray array]; 34 | for (MAS_VIEW *view in self) { 35 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 36 | [constraints addObjectsFromArray:[view mas_remakeConstraints:block]]; 37 | } 38 | return constraints; 39 | } 40 | 41 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 42 | if (self.count < 2) { 43 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 44 | return; 45 | } 46 | 47 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 48 | if (axisType == MASAxisTypeHorizontal) { 49 | MAS_VIEW *prev; 50 | for (int i = 0; i < self.count; i++) { 51 | MAS_VIEW *v = self[i]; 52 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 53 | if (prev) { 54 | make.width.equalTo(prev); 55 | make.left.equalTo(prev.mas_right).offset(fixedSpacing); 56 | if (i == self.count - 1) {//last one 57 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 58 | } 59 | } 60 | else {//first one 61 | make.left.equalTo(tempSuperView).offset(leadSpacing); 62 | } 63 | 64 | }]; 65 | prev = v; 66 | } 67 | } 68 | else { 69 | MAS_VIEW *prev; 70 | for (int i = 0; i < self.count; i++) { 71 | MAS_VIEW *v = self[i]; 72 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 73 | if (prev) { 74 | make.height.equalTo(prev); 75 | make.top.equalTo(prev.mas_bottom).offset(fixedSpacing); 76 | if (i == self.count - 1) {//last one 77 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 78 | } 79 | } 80 | else {//first one 81 | make.top.equalTo(tempSuperView).offset(leadSpacing); 82 | } 83 | 84 | }]; 85 | prev = v; 86 | } 87 | } 88 | } 89 | 90 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 91 | if (self.count < 2) { 92 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 93 | return; 94 | } 95 | 96 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 97 | if (axisType == MASAxisTypeHorizontal) { 98 | MAS_VIEW *prev; 99 | for (int i = 0; i < self.count; i++) { 100 | MAS_VIEW *v = self[i]; 101 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 102 | make.width.equalTo(@(fixedItemLength)); 103 | if (prev) { 104 | if (i == self.count - 1) {//last one 105 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 106 | } 107 | else { 108 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 109 | make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 110 | } 111 | } 112 | else {//first one 113 | make.left.equalTo(tempSuperView).offset(leadSpacing); 114 | } 115 | }]; 116 | prev = v; 117 | } 118 | } 119 | else { 120 | MAS_VIEW *prev; 121 | for (int i = 0; i < self.count; i++) { 122 | MAS_VIEW *v = self[i]; 123 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 124 | make.height.equalTo(@(fixedItemLength)); 125 | if (prev) { 126 | if (i == self.count - 1) {//last one 127 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 128 | } 129 | else { 130 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 131 | make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 132 | } 133 | } 134 | else {//first one 135 | make.top.equalTo(tempSuperView).offset(leadSpacing); 136 | } 137 | }]; 138 | prev = v; 139 | } 140 | } 141 | } 142 | 143 | - (MAS_VIEW *)mas_commonSuperviewOfViews 144 | { 145 | MAS_VIEW *commonSuperview = nil; 146 | MAS_VIEW *previousView = nil; 147 | for (id object in self) { 148 | if ([object isKindOfClass:[MAS_VIEW class]]) { 149 | MAS_VIEW *view = (MAS_VIEW *)object; 150 | if (previousView) { 151 | commonSuperview = [view mas_closestCommonSuperview:commonSuperview]; 152 | } else { 153 | commonSuperview = view; 154 | } 155 | previousView = view; 156 | } 157 | } 158 | NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy."); 159 | return commonSuperview; 160 | } 161 | 162 | @end 163 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/NSArray+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand array additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface NSArray (MASShorthandAdditions) 18 | 19 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 20 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 21 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 22 | 23 | @end 24 | 25 | @implementation NSArray (MASShorthandAdditions) 26 | 27 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 28 | return [self mas_makeConstraints:block]; 29 | } 30 | 31 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 32 | return [self mas_updateConstraints:block]; 33 | } 34 | 35 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 36 | return [self mas_remakeConstraints:block]; 37 | } 38 | 39 | @end 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/NSLayoutConstraint+MASDebugAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * makes debug and log output of NSLayoutConstraints more readable 13 | */ 14 | @interface NSLayoutConstraint (MASDebugAdditions) 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/NSLayoutConstraint+MASDebugAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSLayoutConstraint+MASDebugAdditions.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | 13 | @implementation NSLayoutConstraint (MASDebugAdditions) 14 | 15 | #pragma mark - description maps 16 | 17 | + (NSDictionary *)layoutRelationDescriptionsByValue { 18 | static dispatch_once_t once; 19 | static NSDictionary *descriptionMap; 20 | dispatch_once(&once, ^{ 21 | descriptionMap = @{ 22 | @(NSLayoutRelationEqual) : @"==", 23 | @(NSLayoutRelationGreaterThanOrEqual) : @">=", 24 | @(NSLayoutRelationLessThanOrEqual) : @"<=", 25 | }; 26 | }); 27 | return descriptionMap; 28 | } 29 | 30 | + (NSDictionary *)layoutAttributeDescriptionsByValue { 31 | static dispatch_once_t once; 32 | static NSDictionary *descriptionMap; 33 | dispatch_once(&once, ^{ 34 | descriptionMap = @{ 35 | @(NSLayoutAttributeTop) : @"top", 36 | @(NSLayoutAttributeLeft) : @"left", 37 | @(NSLayoutAttributeBottom) : @"bottom", 38 | @(NSLayoutAttributeRight) : @"right", 39 | @(NSLayoutAttributeLeading) : @"leading", 40 | @(NSLayoutAttributeTrailing) : @"trailing", 41 | @(NSLayoutAttributeWidth) : @"width", 42 | @(NSLayoutAttributeHeight) : @"height", 43 | @(NSLayoutAttributeCenterX) : @"centerX", 44 | @(NSLayoutAttributeCenterY) : @"centerY", 45 | @(NSLayoutAttributeBaseline) : @"baseline", 46 | 47 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 48 | @(NSLayoutAttributeFirstBaseline) : @"firstBaseline", 49 | @(NSLayoutAttributeLastBaseline) : @"lastBaseline", 50 | #endif 51 | 52 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 53 | @(NSLayoutAttributeLeftMargin) : @"leftMargin", 54 | @(NSLayoutAttributeRightMargin) : @"rightMargin", 55 | @(NSLayoutAttributeTopMargin) : @"topMargin", 56 | @(NSLayoutAttributeBottomMargin) : @"bottomMargin", 57 | @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", 58 | @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", 59 | @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", 60 | @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", 61 | #endif 62 | 63 | }; 64 | 65 | }); 66 | return descriptionMap; 67 | } 68 | 69 | 70 | + (NSDictionary *)layoutPriorityDescriptionsByValue { 71 | static dispatch_once_t once; 72 | static NSDictionary *descriptionMap; 73 | dispatch_once(&once, ^{ 74 | #if TARGET_OS_IPHONE || TARGET_OS_TV 75 | descriptionMap = @{ 76 | @(MASLayoutPriorityDefaultHigh) : @"high", 77 | @(MASLayoutPriorityDefaultLow) : @"low", 78 | @(MASLayoutPriorityDefaultMedium) : @"medium", 79 | @(MASLayoutPriorityRequired) : @"required", 80 | @(MASLayoutPriorityFittingSizeLevel) : @"fitting size", 81 | }; 82 | #elif TARGET_OS_MAC 83 | descriptionMap = @{ 84 | @(MASLayoutPriorityDefaultHigh) : @"high", 85 | @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window", 86 | @(MASLayoutPriorityDefaultMedium) : @"medium", 87 | @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put", 88 | @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window", 89 | @(MASLayoutPriorityDefaultLow) : @"low", 90 | @(MASLayoutPriorityFittingSizeCompression) : @"fitting size", 91 | @(MASLayoutPriorityRequired) : @"required", 92 | }; 93 | #endif 94 | }); 95 | return descriptionMap; 96 | } 97 | 98 | #pragma mark - description override 99 | 100 | + (NSString *)descriptionForObject:(id)obj { 101 | if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) { 102 | return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]]; 103 | } 104 | return [NSString stringWithFormat:@"%@:%p", [obj class], obj]; 105 | } 106 | 107 | - (NSString *)description { 108 | NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"]; 109 | 110 | [description appendString:[self.class descriptionForObject:self]]; 111 | 112 | [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]]; 113 | if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) { 114 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]]; 115 | } 116 | 117 | [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]]; 118 | 119 | if (self.secondItem) { 120 | [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]]; 121 | } 122 | if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) { 123 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]]; 124 | } 125 | 126 | if (self.multiplier != 1) { 127 | [description appendFormat:@" * %g", self.multiplier]; 128 | } 129 | 130 | if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) { 131 | [description appendFormat:@" %g", self.constant]; 132 | } else { 133 | if (self.constant) { 134 | [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)]; 135 | } 136 | } 137 | 138 | if (self.priority != MASLayoutPriorityRequired) { 139 | [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]]; 140 | } 141 | 142 | [description appendString:@">"]; 143 | return description; 144 | } 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/View+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | /** 14 | * Provides constraint maker block 15 | * and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs 16 | */ 17 | @interface MAS_VIEW (MASAdditions) 18 | 19 | /** 20 | * following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute 21 | */ 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_left; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_top; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_right; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leading; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_width; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_height; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX; 31 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY; 32 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline; 33 | @property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr); 34 | 35 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 36 | 37 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline; 39 | 40 | #endif 41 | 42 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 43 | 44 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; 49 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; 50 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; 51 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; 52 | 53 | #endif 54 | 55 | /** 56 | * a key to associate with this view 57 | */ 58 | @property (nonatomic, strong) id mas_key; 59 | 60 | /** 61 | * Finds the closest common superview between this view and another view 62 | * 63 | * @param view other view 64 | * 65 | * @return returns nil if common superview could not be found 66 | */ 67 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view; 68 | 69 | /** 70 | * Creates a MASConstraintMaker with the callee view. 71 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing 72 | * 73 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 74 | * 75 | * @return Array of created MASConstraints 76 | */ 77 | - (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 78 | 79 | /** 80 | * Creates a MASConstraintMaker with the callee view. 81 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 82 | * If an existing constraint exists then it will be updated instead. 83 | * 84 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 85 | * 86 | * @return Array of created/updated MASConstraints 87 | */ 88 | - (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 89 | 90 | /** 91 | * Creates a MASConstraintMaker with the callee view. 92 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 93 | * All constraints previously installed for the view will be removed. 94 | * 95 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 96 | * 97 | * @return Array of created/updated MASConstraints 98 | */ 99 | - (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 100 | 101 | @end 102 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/View+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | #import 11 | 12 | @implementation MAS_VIEW (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { 15 | self.translatesAutoresizingMaskIntoConstraints = NO; 16 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 17 | block(constraintMaker); 18 | return [constraintMaker install]; 19 | } 20 | 21 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block { 22 | self.translatesAutoresizingMaskIntoConstraints = NO; 23 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 24 | constraintMaker.updateExisting = YES; 25 | block(constraintMaker); 26 | return [constraintMaker install]; 27 | } 28 | 29 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 30 | self.translatesAutoresizingMaskIntoConstraints = NO; 31 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 32 | constraintMaker.removeExisting = YES; 33 | block(constraintMaker); 34 | return [constraintMaker install]; 35 | } 36 | 37 | #pragma mark - NSLayoutAttribute properties 38 | 39 | - (MASViewAttribute *)mas_left { 40 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft]; 41 | } 42 | 43 | - (MASViewAttribute *)mas_top { 44 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTop]; 45 | } 46 | 47 | - (MASViewAttribute *)mas_right { 48 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRight]; 49 | } 50 | 51 | - (MASViewAttribute *)mas_bottom { 52 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottom]; 53 | } 54 | 55 | - (MASViewAttribute *)mas_leading { 56 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeading]; 57 | } 58 | 59 | - (MASViewAttribute *)mas_trailing { 60 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailing]; 61 | } 62 | 63 | - (MASViewAttribute *)mas_width { 64 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeWidth]; 65 | } 66 | 67 | - (MASViewAttribute *)mas_height { 68 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeHeight]; 69 | } 70 | 71 | - (MASViewAttribute *)mas_centerX { 72 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterX]; 73 | } 74 | 75 | - (MASViewAttribute *)mas_centerY { 76 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterY]; 77 | } 78 | 79 | - (MASViewAttribute *)mas_baseline { 80 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBaseline]; 81 | } 82 | 83 | - (MASViewAttribute *(^)(NSLayoutAttribute))mas_attribute 84 | { 85 | return ^(NSLayoutAttribute attr) { 86 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:attr]; 87 | }; 88 | } 89 | 90 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 91 | 92 | - (MASViewAttribute *)mas_firstBaseline { 93 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeFirstBaseline]; 94 | } 95 | - (MASViewAttribute *)mas_lastBaseline { 96 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLastBaseline]; 97 | } 98 | 99 | #endif 100 | 101 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 102 | 103 | - (MASViewAttribute *)mas_leftMargin { 104 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; 105 | } 106 | 107 | - (MASViewAttribute *)mas_rightMargin { 108 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; 109 | } 110 | 111 | - (MASViewAttribute *)mas_topMargin { 112 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; 113 | } 114 | 115 | - (MASViewAttribute *)mas_bottomMargin { 116 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; 117 | } 118 | 119 | - (MASViewAttribute *)mas_leadingMargin { 120 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; 121 | } 122 | 123 | - (MASViewAttribute *)mas_trailingMargin { 124 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; 125 | } 126 | 127 | - (MASViewAttribute *)mas_centerXWithinMargins { 128 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 129 | } 130 | 131 | - (MASViewAttribute *)mas_centerYWithinMargins { 132 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 133 | } 134 | 135 | #endif 136 | 137 | #pragma mark - associated properties 138 | 139 | - (id)mas_key { 140 | return objc_getAssociatedObject(self, @selector(mas_key)); 141 | } 142 | 143 | - (void)setMas_key:(id)key { 144 | objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 145 | } 146 | 147 | #pragma mark - heirachy 148 | 149 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { 150 | MAS_VIEW *closestCommonSuperview = nil; 151 | 152 | MAS_VIEW *secondViewSuperview = view; 153 | while (!closestCommonSuperview && secondViewSuperview) { 154 | MAS_VIEW *firstViewSuperview = self; 155 | while (!closestCommonSuperview && firstViewSuperview) { 156 | if (secondViewSuperview == firstViewSuperview) { 157 | closestCommonSuperview = secondViewSuperview; 158 | } 159 | firstViewSuperview = firstViewSuperview.superview; 160 | } 161 | secondViewSuperview = secondViewSuperview.superview; 162 | } 163 | return closestCommonSuperview; 164 | } 165 | 166 | @end 167 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/View+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand view additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface MAS_VIEW (MASShorthandAdditions) 18 | 19 | @property (nonatomic, strong, readonly) MASViewAttribute *left; 20 | @property (nonatomic, strong, readonly) MASViewAttribute *top; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *right; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *bottom; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *leading; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *trailing; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *width; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *height; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *centerX; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *centerY; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *baseline; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); 31 | 32 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 33 | 34 | @property (nonatomic, strong, readonly) MASViewAttribute *firstBaseline; 35 | @property (nonatomic, strong, readonly) MASViewAttribute *lastBaseline; 36 | 37 | #endif 38 | 39 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 40 | 41 | @property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *topMargin; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; 49 | 50 | #endif 51 | 52 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 53 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 54 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 55 | 56 | @end 57 | 58 | #define MAS_ATTR_FORWARD(attr) \ 59 | - (MASViewAttribute *)attr { \ 60 | return [self mas_##attr]; \ 61 | } 62 | 63 | @implementation MAS_VIEW (MASShorthandAdditions) 64 | 65 | MAS_ATTR_FORWARD(top); 66 | MAS_ATTR_FORWARD(left); 67 | MAS_ATTR_FORWARD(bottom); 68 | MAS_ATTR_FORWARD(right); 69 | MAS_ATTR_FORWARD(leading); 70 | MAS_ATTR_FORWARD(trailing); 71 | MAS_ATTR_FORWARD(width); 72 | MAS_ATTR_FORWARD(height); 73 | MAS_ATTR_FORWARD(centerX); 74 | MAS_ATTR_FORWARD(centerY); 75 | MAS_ATTR_FORWARD(baseline); 76 | 77 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 78 | 79 | MAS_ATTR_FORWARD(firstBaseline); 80 | MAS_ATTR_FORWARD(lastBaseline); 81 | 82 | #endif 83 | 84 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 85 | 86 | MAS_ATTR_FORWARD(leftMargin); 87 | MAS_ATTR_FORWARD(rightMargin); 88 | MAS_ATTR_FORWARD(topMargin); 89 | MAS_ATTR_FORWARD(bottomMargin); 90 | MAS_ATTR_FORWARD(leadingMargin); 91 | MAS_ATTR_FORWARD(trailingMargin); 92 | MAS_ATTR_FORWARD(centerXWithinMargins); 93 | MAS_ATTR_FORWARD(centerYWithinMargins); 94 | 95 | #endif 96 | 97 | - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { 98 | return [self mas_attribute]; 99 | } 100 | 101 | - (NSArray *)makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 102 | return [self mas_makeConstraints:block]; 103 | } 104 | 105 | - (NSArray *)updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 106 | return [self mas_updateConstraints:block]; 107 | } 108 | 109 | - (NSArray *)remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 110 | return [self mas_remakeConstraints:block]; 111 | } 112 | 113 | @end 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/ViewController+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | #ifdef MAS_VIEW_CONTROLLER 14 | 15 | @interface MAS_VIEW_CONTROLLER (MASAdditions) 16 | 17 | /** 18 | * following properties return a new MASViewAttribute with appropriate UILayoutGuide and NSLayoutAttribute 19 | */ 20 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideTop; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideBottom; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom; 26 | 27 | 28 | @end 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/Masonry/ViewController+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "ViewController+MASAdditions.h" 10 | 11 | #ifdef MAS_VIEW_CONTROLLER 12 | 13 | @implementation MAS_VIEW_CONTROLLER (MASAdditions) 14 | 15 | - (MASViewAttribute *)mas_topLayoutGuide { 16 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 17 | } 18 | - (MASViewAttribute *)mas_topLayoutGuideTop { 19 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 20 | } 21 | - (MASViewAttribute *)mas_topLayoutGuideBottom { 22 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 23 | } 24 | 25 | - (MASViewAttribute *)mas_bottomLayoutGuide { 26 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 27 | } 28 | - (MASViewAttribute *)mas_bottomLayoutGuideTop { 29 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 30 | } 31 | - (MASViewAttribute *)mas_bottomLayoutGuideBottom { 32 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 33 | } 34 | 35 | 36 | 37 | @end 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/.DS_Store -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/FileDownloadCell.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 28 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/NSString+Hash.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #import 4 | 5 | @interface NSString (Hash) 6 | 7 | #pragma mark - 散列函数 8 | /** 9 | * 计算MD5散列结果 10 | * 11 | * 终端测试命令: 12 | * @code 13 | * md5 -s "string" 14 | * @endcode 15 | * 16 | *

提示:随着 MD5 碰撞生成器的出现,MD5 算法不应被用于任何软件完整性检查或代码签名的用途。

17 | * 18 | * @return 32个字符的MD5散列字符串 19 | */ 20 | - (NSString *)md5String; 21 | 22 | /** 23 | * 计算SHA1散列结果 24 | * 25 | * 终端测试命令: 26 | * @code 27 | * echo -n "string" | openssl sha -sha1 28 | * @endcode 29 | * 30 | * @return 40个字符的SHA1散列字符串 31 | */ 32 | - (NSString *)sha1String; 33 | 34 | /** 35 | * 计算SHA256散列结果 36 | * 37 | * 终端测试命令: 38 | * @code 39 | * echo -n "string" | openssl sha -sha256 40 | * @endcode 41 | * 42 | * @return 64个字符的SHA256散列字符串 43 | */ 44 | - (NSString *)sha256String; 45 | 46 | /** 47 | * 计算SHA 512散列结果 48 | * 49 | * 终端测试命令: 50 | * @code 51 | * echo -n "string" | openssl sha -sha512 52 | * @endcode 53 | * 54 | * @return 128个字符的SHA 512散列字符串 55 | */ 56 | - (NSString *)sha512String; 57 | 58 | #pragma mark - HMAC 散列函数 59 | /** 60 | * 计算HMAC MD5散列结果 61 | * 62 | * 终端测试命令: 63 | * @code 64 | * echo -n "string" | openssl dgst -md5 -hmac "key" 65 | * @endcode 66 | * 67 | * @return 32个字符的HMAC MD5散列字符串 68 | */ 69 | - (NSString *)hmacMD5StringWithKey:(NSString *)key; 70 | 71 | /** 72 | * 计算HMAC SHA1散列结果 73 | * 74 | * 终端测试命令: 75 | * @code 76 | * echo -n "string" | openssl sha -sha1 -hmac "key" 77 | * @endcode 78 | * 79 | * @return 40个字符的HMAC SHA1散列字符串 80 | */ 81 | - (NSString *)hmacSHA1StringWithKey:(NSString *)key; 82 | 83 | /** 84 | * 计算HMAC SHA256散列结果 85 | * 86 | * 终端测试命令: 87 | * @code 88 | * echo -n "string" | openssl sha -sha256 -hmac "key" 89 | * @endcode 90 | * 91 | * @return 64个字符的HMAC SHA256散列字符串 92 | */ 93 | - (NSString *)hmacSHA256StringWithKey:(NSString *)key; 94 | 95 | /** 96 | * 计算HMAC SHA512散列结果 97 | * 98 | * 终端测试命令: 99 | * @code 100 | * echo -n "string" | openssl sha -sha512 -hmac "key" 101 | * @endcode 102 | * 103 | * @return 128个字符的HMAC SHA512散列字符串 104 | */ 105 | - (NSString *)hmacSHA512StringWithKey:(NSString *)key; 106 | 107 | #pragma mark - 文件散列函数 108 | 109 | /** 110 | * 计算文件的MD5散列结果 111 | * 112 | * 终端测试命令: 113 | * @code 114 | * md5 file.dat 115 | * @endcode 116 | * 117 | * @return 32个字符的MD5散列字符串 118 | */ 119 | - (NSString *)fileMD5Hash; 120 | 121 | /** 122 | * 计算文件的SHA1散列结果 123 | * 124 | * 终端测试命令: 125 | * @code 126 | * openssl sha -sha1 file.dat 127 | * @endcode 128 | * 129 | * @return 40个字符的SHA1散列字符串 130 | */ 131 | - (NSString *)fileSHA1Hash; 132 | 133 | /** 134 | * 计算文件的SHA256散列结果 135 | * 136 | * 终端测试命令: 137 | * @code 138 | * openssl sha -sha256 file.dat 139 | * @endcode 140 | * 141 | * @return 64个字符的SHA256散列字符串 142 | */ 143 | - (NSString *)fileSHA256Hash; 144 | 145 | /** 146 | * 计算文件的SHA512散列结果 147 | * 148 | * 终端测试命令: 149 | * @code 150 | * openssl sha -sha512 file.dat 151 | * @endcode 152 | * 153 | * @return 128个字符的SHA512散列字符串 154 | */ 155 | - (NSString *)fileSHA512Hash; 156 | 157 | @end 158 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/NSString+Hash.m: -------------------------------------------------------------------------------- 1 | 2 | #import "NSString+Hash.h" 3 | #import 4 | 5 | @implementation NSString (Hash) 6 | 7 | #pragma mark - 散列函数 8 | - (NSString *)md5String { 9 | const char *str = self.UTF8String; 10 | uint8_t buffer[CC_MD5_DIGEST_LENGTH]; 11 | 12 | CC_MD5(str, (CC_LONG)strlen(str), buffer); 13 | 14 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH]; 15 | } 16 | 17 | - (NSString *)sha1String { 18 | const char *str = self.UTF8String; 19 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH]; 20 | 21 | CC_SHA1(str, (CC_LONG)strlen(str), buffer); 22 | 23 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH]; 24 | } 25 | 26 | - (NSString *)sha256String { 27 | const char *str = self.UTF8String; 28 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH]; 29 | 30 | CC_SHA256(str, (CC_LONG)strlen(str), buffer); 31 | 32 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH]; 33 | } 34 | 35 | - (NSString *)sha512String { 36 | const char *str = self.UTF8String; 37 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH]; 38 | 39 | CC_SHA512(str, (CC_LONG)strlen(str), buffer); 40 | 41 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH]; 42 | } 43 | 44 | #pragma mark - HMAC 散列函数 45 | - (NSString *)hmacMD5StringWithKey:(NSString *)key { 46 | const char *keyData = key.UTF8String; 47 | const char *strData = self.UTF8String; 48 | uint8_t buffer[CC_MD5_DIGEST_LENGTH]; 49 | 50 | CCHmac(kCCHmacAlgMD5, keyData, strlen(keyData), strData, strlen(strData), buffer); 51 | 52 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH]; 53 | } 54 | 55 | - (NSString *)hmacSHA1StringWithKey:(NSString *)key { 56 | const char *keyData = key.UTF8String; 57 | const char *strData = self.UTF8String; 58 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH]; 59 | 60 | CCHmac(kCCHmacAlgSHA1, keyData, strlen(keyData), strData, strlen(strData), buffer); 61 | 62 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH]; 63 | } 64 | 65 | - (NSString *)hmacSHA256StringWithKey:(NSString *)key { 66 | const char *keyData = key.UTF8String; 67 | const char *strData = self.UTF8String; 68 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH]; 69 | 70 | CCHmac(kCCHmacAlgSHA256, keyData, strlen(keyData), strData, strlen(strData), buffer); 71 | 72 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH]; 73 | } 74 | 75 | - (NSString *)hmacSHA512StringWithKey:(NSString *)key { 76 | const char *keyData = key.UTF8String; 77 | const char *strData = self.UTF8String; 78 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH]; 79 | 80 | CCHmac(kCCHmacAlgSHA512, keyData, strlen(keyData), strData, strlen(strData), buffer); 81 | 82 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH]; 83 | } 84 | 85 | #pragma mark - 文件散列函数 86 | 87 | #define FileHashDefaultChunkSizeForReadingData 4096 88 | 89 | - (NSString *)fileMD5Hash { 90 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self]; 91 | if (fp == nil) { 92 | return nil; 93 | } 94 | 95 | CC_MD5_CTX hashCtx; 96 | CC_MD5_Init(&hashCtx); 97 | 98 | while (YES) { 99 | @autoreleasepool { 100 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData]; 101 | 102 | CC_MD5_Update(&hashCtx, data.bytes, (CC_LONG)data.length); 103 | 104 | if (data.length == 0) { 105 | break; 106 | } 107 | } 108 | } 109 | [fp closeFile]; 110 | 111 | uint8_t buffer[CC_MD5_DIGEST_LENGTH]; 112 | CC_MD5_Final(buffer, &hashCtx); 113 | 114 | return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH]; 115 | } 116 | 117 | - (NSString *)fileSHA1Hash { 118 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self]; 119 | if (fp == nil) { 120 | return nil; 121 | } 122 | 123 | CC_SHA1_CTX hashCtx; 124 | CC_SHA1_Init(&hashCtx); 125 | 126 | while (YES) { 127 | @autoreleasepool { 128 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData]; 129 | 130 | CC_SHA1_Update(&hashCtx, data.bytes, (CC_LONG)data.length); 131 | 132 | if (data.length == 0) { 133 | break; 134 | } 135 | } 136 | } 137 | [fp closeFile]; 138 | 139 | uint8_t buffer[CC_SHA1_DIGEST_LENGTH]; 140 | CC_SHA1_Final(buffer, &hashCtx); 141 | 142 | return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH]; 143 | } 144 | 145 | - (NSString *)fileSHA256Hash { 146 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self]; 147 | if (fp == nil) { 148 | return nil; 149 | } 150 | 151 | CC_SHA256_CTX hashCtx; 152 | CC_SHA256_Init(&hashCtx); 153 | 154 | while (YES) { 155 | @autoreleasepool { 156 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData]; 157 | 158 | CC_SHA256_Update(&hashCtx, data.bytes, (CC_LONG)data.length); 159 | 160 | if (data.length == 0) { 161 | break; 162 | } 163 | } 164 | } 165 | [fp closeFile]; 166 | 167 | uint8_t buffer[CC_SHA256_DIGEST_LENGTH]; 168 | CC_SHA256_Final(buffer, &hashCtx); 169 | 170 | return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH]; 171 | } 172 | 173 | - (NSString *)fileSHA512Hash { 174 | NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self]; 175 | if (fp == nil) { 176 | return nil; 177 | } 178 | 179 | CC_SHA512_CTX hashCtx; 180 | CC_SHA512_Init(&hashCtx); 181 | 182 | while (YES) { 183 | @autoreleasepool { 184 | NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData]; 185 | 186 | CC_SHA512_Update(&hashCtx, data.bytes, (CC_LONG)data.length); 187 | 188 | if (data.length == 0) { 189 | break; 190 | } 191 | } 192 | } 193 | [fp closeFile]; 194 | 195 | uint8_t buffer[CC_SHA512_DIGEST_LENGTH]; 196 | CC_SHA512_Final(buffer, &hashCtx); 197 | 198 | return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH]; 199 | } 200 | 201 | #pragma mark - 助手方法 202 | /** 203 | * 返回二进制 Bytes 流的字符串表示形式 204 | * 205 | * @param bytes 二进制 Bytes 数组 206 | * @param length 数组长度 207 | * 208 | * @return 字符串表示形式 209 | */ 210 | - (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length { 211 | NSMutableString *strM = [NSMutableString string]; 212 | 213 | for (int i = 0; i < length; i++) { 214 | [strM appendFormat:@"%02x", bytes[i]]; 215 | } 216 | 217 | return [strM copy]; 218 | } 219 | 220 | @end 221 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadCell.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | #import 9 | #import "TZHFilesDownloadModel.h" 10 | 11 | @interface TZHDownloadCell : UITableViewCell 12 | 13 | @property(nonatomic,strong)TZHFilesDownloadModel *model; 14 | @property (weak, nonatomic) IBOutlet UIProgressView *DownloadProgress; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadCell.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "TZHDownloadCell.h" 10 | @interface TZHDownloadCell() 11 | 12 | @property (weak, nonatomic) IBOutlet UILabel *FileNameLabel; 13 | 14 | @end 15 | 16 | @implementation TZHDownloadCell 17 | 18 | - (void)awakeFromNib { 19 | [super awakeFromNib]; 20 | // Initialization code 21 | } 22 | 23 | -(void)setModel:(TZHFilesDownloadModel *)model{ 24 | 25 | _model = model; 26 | self.FileNameLabel.text = model.FileName; 27 | self.DownloadProgress.progress = model.progress; 28 | } 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadManager.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TZHDownloadManager : NSObject 12 | 13 | @property(nonatomic,strong)NSString *fileName; 14 | 15 | +(instancetype)shared; 16 | 17 | //异步下载的方法 进度的block 18 | -(void)downloadAudioWithURL:(NSURL *)url andFormat:(NSString *)format progress:(void(^)(float progress))progressBlock complete:(void(^)(NSString *fileSavePath,NSError *error))completeBlock; 19 | 20 | //判断是否正在下载 21 | -(BOOL)isDownloadingAudioWithURL:(NSURL *)url; 22 | 23 | 24 | //取消下载 25 | - (void)cancelDownloadingAudioWithURL:(NSURL *)url andFormat:(NSString *)format complete:(void (^)())completeBlock; 26 | 27 | 28 | //显示文件占内存大小 29 | + (NSString *)getFileCacheSize; 30 | //删除文件 31 | 32 | +(void)deleteFileFromCache; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadManager.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "TZHDownloadManager.h" 10 | #import "NSString+Hash.h" 11 | 12 | @interface TZHDownloadManager () 13 | 14 | @property (nonatomic, strong) NSURLSession *session; 15 | @property(nonatomic,strong)NSString *fileForm; 16 | 17 | @end 18 | @implementation TZHDownloadManager{ 19 | //保存下载任务对应的进度block 和 完成的block 20 | NSMutableDictionary *_progressBlocks; 21 | NSMutableDictionary *_completeBlocks; 22 | NSMutableDictionary *_downloadTasks; 23 | } 24 | 25 | static id _instance; 26 | 27 | +(instancetype)shared{ 28 | 29 | static dispatch_once_t onceToken; 30 | 31 | dispatch_once(&onceToken, ^{ 32 | _instance = [[self alloc] init]; 33 | }); 34 | return _instance; 35 | } 36 | 37 | 38 | - (instancetype)init { 39 | self = [super init]; 40 | 41 | if (self) { 42 | _progressBlocks = [NSMutableDictionary dictionary]; 43 | _completeBlocks = [NSMutableDictionary dictionary]; 44 | _downloadTasks = [NSMutableDictionary dictionary]; 45 | } 46 | return self; 47 | } 48 | 49 | - (NSURLSession *)session { 50 | if (!_session) { 51 | NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 52 | 53 | _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 54 | } 55 | return _session; 56 | } 57 | 58 | - (BOOL)isDownloadingAudioWithURL:(NSURL *)url { 59 | 60 | if (_completeBlocks[url]) { 61 | return YES; 62 | } 63 | return NO; 64 | } 65 | 66 | - (void)cancelDownloadingAudioWithURL:(NSURL *)url andFormat:(NSString *)format complete:(void (^)())completeBlock { 67 | 68 | NSURLSessionDownloadTask *currentTask = _downloadTasks[url]; 69 | 70 | // 2.cancel 71 | if (currentTask) { 72 | 73 | [currentTask cancelByProducingResumeData:^(NSData *_Nullable resumeData) { 74 | 75 | [resumeData writeToFile:[self getResumeDataPathWithURL:url andFormat:format] atomically:YES]; 76 | 77 | //把取消成功的结果返回 78 | if (completeBlock) { 79 | completeBlock(); 80 | } 81 | 82 | _progressBlocks[url] = nil; 83 | _completeBlocks[url] = nil; 84 | _downloadTasks[url] = nil; 85 | 86 | }]; 87 | } 88 | } 89 | 90 | //入口 91 | - (void)downloadAudioWithURL:(NSURL *)url andFormat:(NSString *)format progress:(void (^)(float progress))progressBlock complete:(void (^)(NSString *fileSavePath, NSError *error))completeBlock { 92 | 93 | NSFileManager *fileMan = [NSFileManager defaultManager]; 94 | 95 | NSLog(@"下载工具中打印格式%@",format); 96 | _fileForm = format; 97 | NSString *fileSavePath = [self getFileSavePathWithURL:url andFormat:format]; 98 | if ([fileMan fileExistsAtPath:fileSavePath]) { 99 | NSLog(@"文件已经存在"); 100 | if (completeBlock) { 101 | completeBlock(fileSavePath, nil); 102 | } 103 | return; 104 | } 105 | 106 | 107 | if ([self isDownloadingAudioWithURL:url]) { 108 | NSLog(@"正在下载"); 109 | return; 110 | } 111 | 112 | 113 | 114 | [_progressBlocks setObject:progressBlock forKey:url]; 115 | [_completeBlocks setObject:completeBlock forKey:url]; 116 | 117 | 118 | NSString *resumeDataPath = [self getResumeDataPathWithURL:url andFormat:format]; 119 | 120 | NSURLSessionDownloadTask *downloadTask; 121 | if ([fileMan fileExistsAtPath:resumeDataPath]) { 122 | 123 | NSData *resumeData = [NSData dataWithContentsOfFile:resumeDataPath]; 124 | downloadTask = [self.session downloadTaskWithResumeData:resumeData]; 125 | } else { 126 | downloadTask = [self.session downloadTaskWithURL:url]; 127 | } 128 | 129 | [_downloadTasks setObject:downloadTask forKey:url]; 130 | //开启 131 | [downloadTask resume]; 132 | } 133 | 134 | 135 | - (NSString *)getResumeDataPathWithURL:(NSURL *)url andFormat:(NSString *)format{ 136 | NSString *tmpPath = NSTemporaryDirectory(); 137 | 138 | NSString *fileName = [NSString stringWithFormat:@"%@%@",[url.absoluteString md5String],format]; 139 | return [tmpPath stringByAppendingPathComponent:fileName]; 140 | } 141 | 142 | 143 | - (NSString *)getFileSavePathWithURL:(NSURL *)url andFormat:(NSString *)format{ 144 | 145 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 146 | 147 | NSFileManager *fileManager = [NSFileManager defaultManager]; 148 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"]; 149 | [fileManager createDirectoryAtPath:TZHCachePath withIntermediateDirectories:YES attributes:nil error:nil]; 150 | 151 | NSString *fileName = [NSString stringWithFormat:@"%@%@",[url.absoluteString md5String],format]; 152 | _fileName = fileName; 153 | return [TZHCachePath stringByAppendingPathComponent:fileName]; 154 | } 155 | 156 | #pragma mark sessionDelegate 157 | 158 | - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { 159 | 160 | NSFileManager *fileMan = [NSFileManager defaultManager]; 161 | 162 | NSURL *currentURL = downloadTask.currentRequest.URL; 163 | [fileMan copyItemAtPath:location.path toPath:[self getFileSavePathWithURL:currentURL andFormat:_fileForm] error:NULL]; 164 | 165 | 166 | if (_completeBlocks[currentURL]) { 167 | void (^tmpCompBlock)(NSString *filePath, NSError *error) = _completeBlocks[currentURL]; 168 | tmpCompBlock([self getFileSavePathWithURL:currentURL andFormat:_fileForm], nil); 169 | } 170 | 171 | 172 | _progressBlocks[currentURL] = nil; 173 | _completeBlocks[currentURL] = nil; 174 | _downloadTasks[currentURL] = nil; 175 | } 176 | 177 | - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { 178 | 179 | float progress = totalBytesWritten * 1.0 / totalBytesExpectedToWrite; 180 | 181 | 182 | NSURL *url = downloadTask.currentRequest.URL; 183 | if (_progressBlocks[url]) { 184 | 185 | void (^tmpProBlock)(float) = _progressBlocks[url]; 186 | tmpProBlock(progress); 187 | } 188 | } 189 | 190 | + (NSString *)getFileCacheSize{ 191 | 192 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 193 | 194 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"]; 195 | 196 | NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:TZHCachePath]; 197 | 198 | NSString *filePath = nil; 199 | NSInteger totleSize = 0; 200 | 201 | for (NSString *subPath in subPathArr){ 202 | 203 | filePath =[TZHCachePath stringByAppendingPathComponent:subPath]; 204 | 205 | BOOL isDirectory = NO; 206 | 207 | BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory]; 208 | 209 | if (!isExist || isDirectory || [filePath containsString:@".DS"]){ 210 | 211 | continue; 212 | } 213 | 214 | NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; 215 | 216 | NSInteger size = [dict[@"NSFileSize"] integerValue]; 217 | 218 | 219 | totleSize += size; 220 | } 221 | 222 | 223 | NSString *totleStr = nil; 224 | 225 | if (totleSize > 1000 * 1000){ 226 | totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f]; 227 | 228 | }else if (totleSize > 1000){ 229 | totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ]; 230 | 231 | }else{ 232 | totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f]; 233 | } 234 | 235 | return totleStr; 236 | 237 | } 238 | 239 | +(void)deleteFileFromCache{ 240 | 241 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 242 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"]; 243 | 244 | NSFileManager *fileManager = [NSFileManager defaultManager]; 245 | NSArray *array = [fileManager contentsOfDirectoryAtPath:TZHCachePath error:nil]; 246 | 247 | for(NSString *fileName in array){ 248 | 249 | [fileManager removeItemAtPath:[TZHCachePath stringByAppendingPathComponent:fileName] error:nil]; 250 | } 251 | 252 | 253 | } 254 | @end 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadViewController.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TZHDownloadViewController : UIViewController 12 | 13 | @property(nonatomic,strong)NSArray *filesArray; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHDownloadViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHDownloadViewController.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "TZHDownloadViewController.h" 10 | #import "TZHFilesDownloadModel.h" 11 | #import "TZHDownloadCell.h" 12 | #import "TZHDownloadManager.h" 13 | #import "TZHLoadDownloadFileVC.h" 14 | 15 | @interface TZHDownloadViewController () 16 | 17 | @property(nonatomic,weak)UITableView *tableView; 18 | 19 | @end 20 | static NSString *cellID = @"cellId"; 21 | 22 | @implementation TZHDownloadViewController 23 | 24 | - (void)viewDidLoad { 25 | [super viewDidLoad]; 26 | 27 | UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem]; 28 | back.contentEdgeInsets = UIEdgeInsetsMake(0,-3*10, 0, 0); 29 | 30 | UIImage *backImage = [UIImage imageNamed:[@"TZHFileManager.bundle" stringByAppendingPathComponent:@"返回"]]; 31 | [back setImage:backImage forState:UIControlStateNormal]; 32 | back.titleLabel.font = [UIFont systemFontOfSize:17]; 33 | [back setTitle:@"返回" forState:UIControlStateNormal]; 34 | back.frame=CGRectMake(10,10,65,50); 35 | [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 36 | UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:back]; 37 | self.navigationItem.leftBarButtonItem=item; 38 | self.title = @"附件列表"; 39 | 40 | UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; 41 | [self.view addSubview:tableView]; 42 | tableView.delegate = self; 43 | tableView.dataSource = self; 44 | [tableView registerNib:[UINib nibWithNibName:@"FileDownloadCell" bundle:nil] forCellReuseIdentifier:cellID]; 45 | _tableView = tableView; 46 | tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 47 | tableView.estimatedRowHeight = 250; 48 | } 49 | 50 | -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 51 | 52 | return self.filesArray.count; 53 | } 54 | 55 | -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 56 | 57 | TZHDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 58 | 59 | TZHFilesDownloadModel *model = self.filesArray[indexPath.row]; 60 | cell.model = model; 61 | 62 | cell.selectionStyle = UITableViewCellSelectionStyleNone; 63 | return cell; 64 | } 65 | 66 | -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 67 | 68 | TZHFilesDownloadModel *model = self.filesArray[indexPath.row]; 69 | 70 | NSString *encodedString = (NSString *) 71 | CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 72 | (CFStringRef)model.FileURL, 73 | (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", 74 | NULL, 75 | kCFStringEncodingUTF8)); 76 | 77 | NSURL *url = [NSURL URLWithString:encodedString]; 78 | 79 | NSLog(@"打印打印 %@",model.FileURL); 80 | 81 | if([[TZHDownloadManager shared] isDownloadingAudioWithURL:url] ){ 82 | 83 | }else{ 84 | //下载代码 85 | NSString *format = model.FileName; 86 | NSRange range = [format rangeOfString:@"."]; 87 | format = [format substringFromIndex:range.location]; 88 | 89 | [[TZHDownloadManager shared] downloadAudioWithURL:url andFormat:format progress:^(float progress) { 90 | TZHDownloadCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 91 | cell.DownloadProgress.hidden = NO; 92 | 93 | model.progress = progress; 94 | cell.model = model; 95 | if(progress == 1){ 96 | cell.DownloadProgress.hidden = YES; 97 | 98 | } 99 | 100 | } complete:^(NSString *fileSavePath, NSError *error) { 101 | 102 | NSLog(@"打印存储地址%@",fileSavePath); 103 | TZHLoadDownloadFileVC *webVC = [[TZHLoadDownloadFileVC alloc]init]; 104 | 105 | [webVC loadLocalFileWebView:[TZHDownloadManager shared].fileName]; 106 | webVC.hidesBottomBarWhenPushed = YES; 107 | [self.navigationController pushViewController:webVC animated:YES]; 108 | }]; 109 | } 110 | 111 | } 112 | 113 | -(void)back{ 114 | [self.navigationController popViewControllerAnimated:YES]; 115 | } 116 | 117 | 118 | 119 | @end 120 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileDownload.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHFileDownload.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #ifndef TZHFileDownload_h 10 | #define TZHFileDownload_h 11 | 12 | 13 | #endif /* TZHFileDownload_h */ 14 | #import "TZHFilesDownloadModel.h" 15 | #import "TZHDownloadViewController.h" 16 | #import "TZHDownloadManager.h" 17 | #import "TZHDownloadCell.h" 18 | #import "TZHDownloadViewController.h" 19 | 20 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/Root.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | StringsTable 6 | Root 7 | PreferenceSpecifiers 8 | 9 | 10 | Type 11 | PSGroupSpecifier 12 | Title 13 | Group 14 | 15 | 16 | Type 17 | PSTextFieldSpecifier 18 | Title 19 | Name 20 | Key 21 | name_preference 22 | DefaultValue 23 | 24 | IsSecure 25 | 26 | KeyboardType 27 | Alphabet 28 | AutocapitalizationType 29 | None 30 | AutocorrectionType 31 | No 32 | 33 | 34 | Type 35 | PSToggleSwitchSpecifier 36 | Title 37 | Enabled 38 | Key 39 | enabled_preference 40 | DefaultValue 41 | 42 | 43 | 44 | Type 45 | PSSliderSpecifier 46 | Key 47 | slider_preference 48 | DefaultValue 49 | 0.5 50 | MinimumValue 51 | 0 52 | MaximumValue 53 | 1 54 | MinimumValueImage 55 | 56 | MaximumValueImage 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/en.lproj/Root.strings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/en.lproj/Root.strings -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/返回@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFileManager.bundle/返回@2x.png -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFilesDownloadModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHFilesDownloadModel.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TZHFilesDownloadModel : NSObject 12 | 13 | @property(nonatomic,strong)NSString *FileName; 14 | @property(nonatomic,strong)NSString *FileURL; 15 | @property(nonatomic,assign)float progress; 16 | 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHFilesDownloadModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHFilesDownloadModel.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "TZHFilesDownloadModel.h" 10 | 11 | @implementation TZHFilesDownloadModel 12 | 13 | 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHLoadDownloadFileVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TZHLoadDownloadFileVC.h 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TZHLoadDownloadFileVC : UIViewController 12 | 13 | - (void)loadLocalFileWebView:(NSString *)fileName; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/TZHLoadDownloadFileVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TZHLoadDownloadFileVC.m 3 | // 陶增辉文件下载预览 4 | // 5 | // Created by taozenghui on 2017/6/28. 6 | // Copyright © 2017年 taozenghui. All rights reserved. 7 | // 8 | 9 | #import "TZHLoadDownloadFileVC.h" 10 | 11 | #define SWIDTH self.view.frame.size.width //屏幕宽度 12 | #define SHEIGHT self.view.frame.size.height //屏幕高度 13 | 14 | 15 | @interface TZHLoadDownloadFileVC () 16 | 17 | @property(strong, nonatomic)UIWebView *loadWebView; 18 | 19 | @end 20 | 21 | @implementation TZHLoadDownloadFileVC 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | 26 | self.title = @"文件详情"; 27 | UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem]; 28 | back.contentEdgeInsets = UIEdgeInsetsMake(0,-3*10, 0, 0); 29 | 30 | UIImage *backImage = [UIImage imageNamed:[@"TZHFileManager.bundle" stringByAppendingPathComponent:@"返回"]]; 31 | [back setImage:backImage forState:UIControlStateNormal]; 32 | back.titleLabel.font = [UIFont systemFontOfSize:17]; 33 | [back setTitle:@"返回" forState:UIControlStateNormal]; 34 | back.frame=CGRectMake(10,10,65,50); 35 | [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 36 | UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:back]; 37 | self.navigationItem.leftBarButtonItem=item; 38 | 39 | self.view.backgroundColor = [UIColor whiteColor]; 40 | self.automaticallyAdjustsScrollViewInsets = NO; 41 | self.loadWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, SWIDTH, SHEIGHT - 64)]; 42 | _loadWebView.backgroundColor = [UIColor whiteColor]; 43 | _loadWebView.delegate = self; 44 | _loadWebView.scalesPageToFit = YES; 45 | } 46 | 47 | - (void)loadLocalFileWebView:(NSString *)fileName{ 48 | 49 | [self.view addSubview:_loadWebView]; 50 | 51 | NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 52 | 53 | NSString *TZHCachePath = [cachePath stringByAppendingPathComponent:@"TZHDownloadFile"]; 54 | 55 | NSString *filePath = [TZHCachePath stringByAppendingPathComponent:fileName]; 56 | NSURL *url = [NSURL fileURLWithPath:filePath]; 57 | 58 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; 59 | [_loadWebView loadRequest:request]; 60 | } 61 | 62 | -(void)back{ 63 | [self.navigationController popViewControllerAnimated:YES]; 64 | } 65 | @end 66 | -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/screenshots/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TZHui/TZHFileManager/f7b9490b41fc47645c75a1b09859c5b4adcc1672/TZHFileManagerDemo/TZHFileManagerDemo/TZHFileManager/screenshots/.DS_Store -------------------------------------------------------------------------------- /TZHFileManagerDemo/TZHFileManagerDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // TZHFileManagerDemo 4 | // 5 | // Created by swkj on 2017/6/30. 6 | // Copyright © 2017年 thirdnet. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------