├── Demo ├── Demo.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcshareddata │ │ │ └── Demo.xccheckout │ │ └── xcuserdata │ │ │ └── abc.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── abc.xcuserdatad │ │ └── xcschemes │ │ ├── Demo.xcscheme │ │ └── xcschememanagement.plist ├── Demo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ └── LaunchScreen.xib │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ ├── breaddoge.imageset │ │ │ ├── Contents.json │ │ │ └── dogebread.png │ │ ├── doge.imageset │ │ │ ├── Contents.json │ │ │ └── doge@2x.png │ │ ├── forkingdog.imageset │ │ │ ├── Contents.json │ │ │ └── forkingdog@2x.png │ │ ├── phil.imageset │ │ │ ├── Contents.json │ │ │ └── phil.png │ │ ├── sark.imageset │ │ │ ├── Contents.json │ │ │ └── sark@2x.png │ │ ├── sinojerk.imageset │ │ │ ├── Contents.json │ │ │ └── 彪哥副本.png │ │ └── sunnyxx.imageset │ │ │ ├── Contents.json │ │ │ └── 下载.png │ ├── Info.plist │ ├── 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 │ ├── UITableView+FDTemplateLayoutCell.h │ ├── UITableView+FDTemplateLayoutCell.m │ ├── XQFeedCell.h │ ├── XQFeedCell.m │ ├── XQFeedModel.h │ ├── XQFeedModel.m │ ├── XQTableViewController.h │ ├── XQTableViewController.m │ ├── data.json │ ├── main.m │ ├── 横屏.gif │ └── 竖屏.gif ├── DemoTests │ ├── DemoTests.m │ └── Info.plist └── README.md └── README.md /Demo/Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2878A31B1B4B7B2A00C4AC64 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A31A1B4B7B2A00C4AC64 /* main.m */; }; 11 | 2878A31E1B4B7B2A00C4AC64 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A31D1B4B7B2A00C4AC64 /* AppDelegate.m */; }; 12 | 2878A3201B4B7B2A00C4AC64 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2878A31F1B4B7B2A00C4AC64 /* Images.xcassets */; }; 13 | 2878A3231B4B7B2A00C4AC64 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2878A3211B4B7B2A00C4AC64 /* LaunchScreen.xib */; }; 14 | 2878A32F1B4B7B2A00C4AC64 /* DemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A32E1B4B7B2A00C4AC64 /* DemoTests.m */; }; 15 | 2878A33A1B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3391B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.m */; }; 16 | 2878A33C1B4B7B5800C4AC64 /* data.json in Resources */ = {isa = PBXBuildFile; fileRef = 2878A33B1B4B7B5800C4AC64 /* data.json */; }; 17 | 2878A33F1B4B7B7200C4AC64 /* XQTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A33E1B4B7B7200C4AC64 /* XQTableViewController.m */; }; 18 | 2878A3421B4B7BD000C4AC64 /* XQFeedModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3411B4B7BD000C4AC64 /* XQFeedModel.m */; }; 19 | 2878A3451B4B7FC000C4AC64 /* XQFeedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3441B4B7FC000C4AC64 /* XQFeedCell.m */; }; 20 | 2878A3611B4B80DA00C4AC64 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 2878A3471B4B80DA00C4AC64 /* Info.plist */; }; 21 | 2878A3621B4B80DA00C4AC64 /* MASCompositeConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3491B4B80DA00C4AC64 /* MASCompositeConstraint.m */; }; 22 | 2878A3631B4B80DA00C4AC64 /* MASConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A34C1B4B80DA00C4AC64 /* MASConstraint.m */; }; 23 | 2878A3641B4B80DA00C4AC64 /* MASConstraintMaker.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A34E1B4B80DA00C4AC64 /* MASConstraintMaker.m */; }; 24 | 2878A3651B4B80DA00C4AC64 /* MASLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3501B4B80DA00C4AC64 /* MASLayoutConstraint.m */; }; 25 | 2878A3661B4B80DA00C4AC64 /* MASViewAttribute.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3541B4B80DA00C4AC64 /* MASViewAttribute.m */; }; 26 | 2878A3671B4B80DA00C4AC64 /* MASViewConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3561B4B80DA00C4AC64 /* MASViewConstraint.m */; }; 27 | 2878A3681B4B80DA00C4AC64 /* NSArray+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3581B4B80DA00C4AC64 /* NSArray+MASAdditions.m */; }; 28 | 2878A3691B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A35B1B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.m */; }; 29 | 2878A36A1B4B80DA00C4AC64 /* View+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A35D1B4B80DA00C4AC64 /* View+MASAdditions.m */; }; 30 | 2878A36B1B4B80DA00C4AC64 /* ViewController+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 2878A3601B4B80DA00C4AC64 /* ViewController+MASAdditions.m */; }; 31 | /* End PBXBuildFile section */ 32 | 33 | /* Begin PBXContainerItemProxy section */ 34 | 2878A3291B4B7B2A00C4AC64 /* PBXContainerItemProxy */ = { 35 | isa = PBXContainerItemProxy; 36 | containerPortal = 2878A30D1B4B7B2A00C4AC64 /* Project object */; 37 | proxyType = 1; 38 | remoteGlobalIDString = 2878A3141B4B7B2A00C4AC64; 39 | remoteInfo = Demo; 40 | }; 41 | /* End PBXContainerItemProxy section */ 42 | 43 | /* Begin PBXFileReference section */ 44 | 2878A3151B4B7B2A00C4AC64 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 45 | 2878A3191B4B7B2A00C4AC64 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 46 | 2878A31A1B4B7B2A00C4AC64 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 47 | 2878A31C1B4B7B2A00C4AC64 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 48 | 2878A31D1B4B7B2A00C4AC64 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 49 | 2878A31F1B4B7B2A00C4AC64 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 50 | 2878A3221B4B7B2A00C4AC64 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 51 | 2878A3281B4B7B2A00C4AC64 /* DemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 52 | 2878A32D1B4B7B2A00C4AC64 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 53 | 2878A32E1B4B7B2A00C4AC64 /* DemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DemoTests.m; sourceTree = ""; }; 54 | 2878A3381B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableView+FDTemplateLayoutCell.h"; sourceTree = ""; }; 55 | 2878A3391B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITableView+FDTemplateLayoutCell.m"; sourceTree = ""; }; 56 | 2878A33B1B4B7B5800C4AC64 /* data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = data.json; sourceTree = ""; }; 57 | 2878A33D1B4B7B7200C4AC64 /* XQTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XQTableViewController.h; sourceTree = ""; }; 58 | 2878A33E1B4B7B7200C4AC64 /* XQTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XQTableViewController.m; sourceTree = ""; }; 59 | 2878A3401B4B7BD000C4AC64 /* XQFeedModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XQFeedModel.h; sourceTree = ""; }; 60 | 2878A3411B4B7BD000C4AC64 /* XQFeedModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XQFeedModel.m; sourceTree = ""; }; 61 | 2878A3431B4B7FC000C4AC64 /* XQFeedCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XQFeedCell.h; sourceTree = ""; }; 62 | 2878A3441B4B7FC000C4AC64 /* XQFeedCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XQFeedCell.m; sourceTree = ""; }; 63 | 2878A3471B4B80DA00C4AC64 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 64 | 2878A3481B4B80DA00C4AC64 /* MASCompositeConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASCompositeConstraint.h; sourceTree = ""; }; 65 | 2878A3491B4B80DA00C4AC64 /* MASCompositeConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASCompositeConstraint.m; sourceTree = ""; }; 66 | 2878A34A1B4B80DA00C4AC64 /* MASConstraint+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MASConstraint+Private.h"; sourceTree = ""; }; 67 | 2878A34B1B4B80DA00C4AC64 /* MASConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASConstraint.h; sourceTree = ""; }; 68 | 2878A34C1B4B80DA00C4AC64 /* MASConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASConstraint.m; sourceTree = ""; }; 69 | 2878A34D1B4B80DA00C4AC64 /* MASConstraintMaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASConstraintMaker.h; sourceTree = ""; }; 70 | 2878A34E1B4B80DA00C4AC64 /* MASConstraintMaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASConstraintMaker.m; sourceTree = ""; }; 71 | 2878A34F1B4B80DA00C4AC64 /* MASLayoutConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASLayoutConstraint.h; sourceTree = ""; }; 72 | 2878A3501B4B80DA00C4AC64 /* MASLayoutConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASLayoutConstraint.m; sourceTree = ""; }; 73 | 2878A3511B4B80DA00C4AC64 /* Masonry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Masonry.h; sourceTree = ""; }; 74 | 2878A3521B4B80DA00C4AC64 /* MASUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASUtilities.h; sourceTree = ""; }; 75 | 2878A3531B4B80DA00C4AC64 /* MASViewAttribute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASViewAttribute.h; sourceTree = ""; }; 76 | 2878A3541B4B80DA00C4AC64 /* MASViewAttribute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASViewAttribute.m; sourceTree = ""; }; 77 | 2878A3551B4B80DA00C4AC64 /* MASViewConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASViewConstraint.h; sourceTree = ""; }; 78 | 2878A3561B4B80DA00C4AC64 /* MASViewConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASViewConstraint.m; sourceTree = ""; }; 79 | 2878A3571B4B80DA00C4AC64 /* NSArray+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MASAdditions.h"; sourceTree = ""; }; 80 | 2878A3581B4B80DA00C4AC64 /* NSArray+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+MASAdditions.m"; sourceTree = ""; }; 81 | 2878A3591B4B80DA00C4AC64 /* NSArray+MASShorthandAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MASShorthandAdditions.h"; sourceTree = ""; }; 82 | 2878A35A1B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSLayoutConstraint+MASDebugAdditions.h"; sourceTree = ""; }; 83 | 2878A35B1B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSLayoutConstraint+MASDebugAdditions.m"; sourceTree = ""; }; 84 | 2878A35C1B4B80DA00C4AC64 /* View+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "View+MASAdditions.h"; sourceTree = ""; }; 85 | 2878A35D1B4B80DA00C4AC64 /* View+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "View+MASAdditions.m"; sourceTree = ""; }; 86 | 2878A35E1B4B80DA00C4AC64 /* View+MASShorthandAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "View+MASShorthandAdditions.h"; sourceTree = ""; }; 87 | 2878A35F1B4B80DA00C4AC64 /* ViewController+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ViewController+MASAdditions.h"; sourceTree = ""; }; 88 | 2878A3601B4B80DA00C4AC64 /* ViewController+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "ViewController+MASAdditions.m"; sourceTree = ""; }; 89 | /* End PBXFileReference section */ 90 | 91 | /* Begin PBXFrameworksBuildPhase section */ 92 | 2878A3121B4B7B2A00C4AC64 /* Frameworks */ = { 93 | isa = PBXFrameworksBuildPhase; 94 | buildActionMask = 2147483647; 95 | files = ( 96 | ); 97 | runOnlyForDeploymentPostprocessing = 0; 98 | }; 99 | 2878A3251B4B7B2A00C4AC64 /* Frameworks */ = { 100 | isa = PBXFrameworksBuildPhase; 101 | buildActionMask = 2147483647; 102 | files = ( 103 | ); 104 | runOnlyForDeploymentPostprocessing = 0; 105 | }; 106 | /* End PBXFrameworksBuildPhase section */ 107 | 108 | /* Begin PBXGroup section */ 109 | 2878A30C1B4B7B2A00C4AC64 = { 110 | isa = PBXGroup; 111 | children = ( 112 | 2878A3171B4B7B2A00C4AC64 /* Demo */, 113 | 2878A32B1B4B7B2A00C4AC64 /* DemoTests */, 114 | 2878A3161B4B7B2A00C4AC64 /* Products */, 115 | ); 116 | sourceTree = ""; 117 | }; 118 | 2878A3161B4B7B2A00C4AC64 /* Products */ = { 119 | isa = PBXGroup; 120 | children = ( 121 | 2878A3151B4B7B2A00C4AC64 /* Demo.app */, 122 | 2878A3281B4B7B2A00C4AC64 /* DemoTests.xctest */, 123 | ); 124 | name = Products; 125 | sourceTree = ""; 126 | }; 127 | 2878A3171B4B7B2A00C4AC64 /* Demo */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | 2878A3461B4B80DA00C4AC64 /* Masonry */, 131 | 2878A3381B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.h */, 132 | 2878A3391B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.m */, 133 | 2878A31C1B4B7B2A00C4AC64 /* AppDelegate.h */, 134 | 2878A31D1B4B7B2A00C4AC64 /* AppDelegate.m */, 135 | 2878A33D1B4B7B7200C4AC64 /* XQTableViewController.h */, 136 | 2878A33E1B4B7B7200C4AC64 /* XQTableViewController.m */, 137 | 2878A3401B4B7BD000C4AC64 /* XQFeedModel.h */, 138 | 2878A3411B4B7BD000C4AC64 /* XQFeedModel.m */, 139 | 2878A3431B4B7FC000C4AC64 /* XQFeedCell.h */, 140 | 2878A3441B4B7FC000C4AC64 /* XQFeedCell.m */, 141 | 2878A31F1B4B7B2A00C4AC64 /* Images.xcassets */, 142 | 2878A3211B4B7B2A00C4AC64 /* LaunchScreen.xib */, 143 | 2878A3181B4B7B2A00C4AC64 /* Supporting Files */, 144 | ); 145 | path = Demo; 146 | sourceTree = ""; 147 | }; 148 | 2878A3181B4B7B2A00C4AC64 /* Supporting Files */ = { 149 | isa = PBXGroup; 150 | children = ( 151 | 2878A33B1B4B7B5800C4AC64 /* data.json */, 152 | 2878A3191B4B7B2A00C4AC64 /* Info.plist */, 153 | 2878A31A1B4B7B2A00C4AC64 /* main.m */, 154 | ); 155 | name = "Supporting Files"; 156 | sourceTree = ""; 157 | }; 158 | 2878A32B1B4B7B2A00C4AC64 /* DemoTests */ = { 159 | isa = PBXGroup; 160 | children = ( 161 | 2878A32E1B4B7B2A00C4AC64 /* DemoTests.m */, 162 | 2878A32C1B4B7B2A00C4AC64 /* Supporting Files */, 163 | ); 164 | path = DemoTests; 165 | sourceTree = ""; 166 | }; 167 | 2878A32C1B4B7B2A00C4AC64 /* Supporting Files */ = { 168 | isa = PBXGroup; 169 | children = ( 170 | 2878A32D1B4B7B2A00C4AC64 /* Info.plist */, 171 | ); 172 | name = "Supporting Files"; 173 | sourceTree = ""; 174 | }; 175 | 2878A3461B4B80DA00C4AC64 /* Masonry */ = { 176 | isa = PBXGroup; 177 | children = ( 178 | 2878A3471B4B80DA00C4AC64 /* Info.plist */, 179 | 2878A3481B4B80DA00C4AC64 /* MASCompositeConstraint.h */, 180 | 2878A3491B4B80DA00C4AC64 /* MASCompositeConstraint.m */, 181 | 2878A34A1B4B80DA00C4AC64 /* MASConstraint+Private.h */, 182 | 2878A34B1B4B80DA00C4AC64 /* MASConstraint.h */, 183 | 2878A34C1B4B80DA00C4AC64 /* MASConstraint.m */, 184 | 2878A34D1B4B80DA00C4AC64 /* MASConstraintMaker.h */, 185 | 2878A34E1B4B80DA00C4AC64 /* MASConstraintMaker.m */, 186 | 2878A34F1B4B80DA00C4AC64 /* MASLayoutConstraint.h */, 187 | 2878A3501B4B80DA00C4AC64 /* MASLayoutConstraint.m */, 188 | 2878A3511B4B80DA00C4AC64 /* Masonry.h */, 189 | 2878A3521B4B80DA00C4AC64 /* MASUtilities.h */, 190 | 2878A3531B4B80DA00C4AC64 /* MASViewAttribute.h */, 191 | 2878A3541B4B80DA00C4AC64 /* MASViewAttribute.m */, 192 | 2878A3551B4B80DA00C4AC64 /* MASViewConstraint.h */, 193 | 2878A3561B4B80DA00C4AC64 /* MASViewConstraint.m */, 194 | 2878A3571B4B80DA00C4AC64 /* NSArray+MASAdditions.h */, 195 | 2878A3581B4B80DA00C4AC64 /* NSArray+MASAdditions.m */, 196 | 2878A3591B4B80DA00C4AC64 /* NSArray+MASShorthandAdditions.h */, 197 | 2878A35A1B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.h */, 198 | 2878A35B1B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.m */, 199 | 2878A35C1B4B80DA00C4AC64 /* View+MASAdditions.h */, 200 | 2878A35D1B4B80DA00C4AC64 /* View+MASAdditions.m */, 201 | 2878A35E1B4B80DA00C4AC64 /* View+MASShorthandAdditions.h */, 202 | 2878A35F1B4B80DA00C4AC64 /* ViewController+MASAdditions.h */, 203 | 2878A3601B4B80DA00C4AC64 /* ViewController+MASAdditions.m */, 204 | ); 205 | path = Masonry; 206 | sourceTree = ""; 207 | }; 208 | /* End PBXGroup section */ 209 | 210 | /* Begin PBXNativeTarget section */ 211 | 2878A3141B4B7B2A00C4AC64 /* Demo */ = { 212 | isa = PBXNativeTarget; 213 | buildConfigurationList = 2878A3321B4B7B2A00C4AC64 /* Build configuration list for PBXNativeTarget "Demo" */; 214 | buildPhases = ( 215 | 2878A3111B4B7B2A00C4AC64 /* Sources */, 216 | 2878A3121B4B7B2A00C4AC64 /* Frameworks */, 217 | 2878A3131B4B7B2A00C4AC64 /* Resources */, 218 | ); 219 | buildRules = ( 220 | ); 221 | dependencies = ( 222 | ); 223 | name = Demo; 224 | productName = Demo; 225 | productReference = 2878A3151B4B7B2A00C4AC64 /* Demo.app */; 226 | productType = "com.apple.product-type.application"; 227 | }; 228 | 2878A3271B4B7B2A00C4AC64 /* DemoTests */ = { 229 | isa = PBXNativeTarget; 230 | buildConfigurationList = 2878A3351B4B7B2A00C4AC64 /* Build configuration list for PBXNativeTarget "DemoTests" */; 231 | buildPhases = ( 232 | 2878A3241B4B7B2A00C4AC64 /* Sources */, 233 | 2878A3251B4B7B2A00C4AC64 /* Frameworks */, 234 | 2878A3261B4B7B2A00C4AC64 /* Resources */, 235 | ); 236 | buildRules = ( 237 | ); 238 | dependencies = ( 239 | 2878A32A1B4B7B2A00C4AC64 /* PBXTargetDependency */, 240 | ); 241 | name = DemoTests; 242 | productName = DemoTests; 243 | productReference = 2878A3281B4B7B2A00C4AC64 /* DemoTests.xctest */; 244 | productType = "com.apple.product-type.bundle.unit-test"; 245 | }; 246 | /* End PBXNativeTarget section */ 247 | 248 | /* Begin PBXProject section */ 249 | 2878A30D1B4B7B2A00C4AC64 /* Project object */ = { 250 | isa = PBXProject; 251 | attributes = { 252 | LastUpgradeCheck = 0620; 253 | ORGANIZATIONNAME = "格式化油条"; 254 | TargetAttributes = { 255 | 2878A3141B4B7B2A00C4AC64 = { 256 | CreatedOnToolsVersion = 6.2; 257 | }; 258 | 2878A3271B4B7B2A00C4AC64 = { 259 | CreatedOnToolsVersion = 6.2; 260 | TestTargetID = 2878A3141B4B7B2A00C4AC64; 261 | }; 262 | }; 263 | }; 264 | buildConfigurationList = 2878A3101B4B7B2A00C4AC64 /* Build configuration list for PBXProject "Demo" */; 265 | compatibilityVersion = "Xcode 3.2"; 266 | developmentRegion = English; 267 | hasScannedForEncodings = 0; 268 | knownRegions = ( 269 | en, 270 | Base, 271 | ); 272 | mainGroup = 2878A30C1B4B7B2A00C4AC64; 273 | productRefGroup = 2878A3161B4B7B2A00C4AC64 /* Products */; 274 | projectDirPath = ""; 275 | projectRoot = ""; 276 | targets = ( 277 | 2878A3141B4B7B2A00C4AC64 /* Demo */, 278 | 2878A3271B4B7B2A00C4AC64 /* DemoTests */, 279 | ); 280 | }; 281 | /* End PBXProject section */ 282 | 283 | /* Begin PBXResourcesBuildPhase section */ 284 | 2878A3131B4B7B2A00C4AC64 /* Resources */ = { 285 | isa = PBXResourcesBuildPhase; 286 | buildActionMask = 2147483647; 287 | files = ( 288 | 2878A3611B4B80DA00C4AC64 /* Info.plist in Resources */, 289 | 2878A3231B4B7B2A00C4AC64 /* LaunchScreen.xib in Resources */, 290 | 2878A33C1B4B7B5800C4AC64 /* data.json in Resources */, 291 | 2878A3201B4B7B2A00C4AC64 /* Images.xcassets in Resources */, 292 | ); 293 | runOnlyForDeploymentPostprocessing = 0; 294 | }; 295 | 2878A3261B4B7B2A00C4AC64 /* Resources */ = { 296 | isa = PBXResourcesBuildPhase; 297 | buildActionMask = 2147483647; 298 | files = ( 299 | ); 300 | runOnlyForDeploymentPostprocessing = 0; 301 | }; 302 | /* End PBXResourcesBuildPhase section */ 303 | 304 | /* Begin PBXSourcesBuildPhase section */ 305 | 2878A3111B4B7B2A00C4AC64 /* Sources */ = { 306 | isa = PBXSourcesBuildPhase; 307 | buildActionMask = 2147483647; 308 | files = ( 309 | 2878A3631B4B80DA00C4AC64 /* MASConstraint.m in Sources */, 310 | 2878A3661B4B80DA00C4AC64 /* MASViewAttribute.m in Sources */, 311 | 2878A36B1B4B80DA00C4AC64 /* ViewController+MASAdditions.m in Sources */, 312 | 2878A3681B4B80DA00C4AC64 /* NSArray+MASAdditions.m in Sources */, 313 | 2878A33A1B4B7B4700C4AC64 /* UITableView+FDTemplateLayoutCell.m in Sources */, 314 | 2878A3671B4B80DA00C4AC64 /* MASViewConstraint.m in Sources */, 315 | 2878A3651B4B80DA00C4AC64 /* MASLayoutConstraint.m in Sources */, 316 | 2878A31E1B4B7B2A00C4AC64 /* AppDelegate.m in Sources */, 317 | 2878A3451B4B7FC000C4AC64 /* XQFeedCell.m in Sources */, 318 | 2878A36A1B4B80DA00C4AC64 /* View+MASAdditions.m in Sources */, 319 | 2878A3621B4B80DA00C4AC64 /* MASCompositeConstraint.m in Sources */, 320 | 2878A3691B4B80DA00C4AC64 /* NSLayoutConstraint+MASDebugAdditions.m in Sources */, 321 | 2878A3641B4B80DA00C4AC64 /* MASConstraintMaker.m in Sources */, 322 | 2878A3421B4B7BD000C4AC64 /* XQFeedModel.m in Sources */, 323 | 2878A33F1B4B7B7200C4AC64 /* XQTableViewController.m in Sources */, 324 | 2878A31B1B4B7B2A00C4AC64 /* main.m in Sources */, 325 | ); 326 | runOnlyForDeploymentPostprocessing = 0; 327 | }; 328 | 2878A3241B4B7B2A00C4AC64 /* Sources */ = { 329 | isa = PBXSourcesBuildPhase; 330 | buildActionMask = 2147483647; 331 | files = ( 332 | 2878A32F1B4B7B2A00C4AC64 /* DemoTests.m in Sources */, 333 | ); 334 | runOnlyForDeploymentPostprocessing = 0; 335 | }; 336 | /* End PBXSourcesBuildPhase section */ 337 | 338 | /* Begin PBXTargetDependency section */ 339 | 2878A32A1B4B7B2A00C4AC64 /* PBXTargetDependency */ = { 340 | isa = PBXTargetDependency; 341 | target = 2878A3141B4B7B2A00C4AC64 /* Demo */; 342 | targetProxy = 2878A3291B4B7B2A00C4AC64 /* PBXContainerItemProxy */; 343 | }; 344 | /* End PBXTargetDependency section */ 345 | 346 | /* Begin PBXVariantGroup section */ 347 | 2878A3211B4B7B2A00C4AC64 /* LaunchScreen.xib */ = { 348 | isa = PBXVariantGroup; 349 | children = ( 350 | 2878A3221B4B7B2A00C4AC64 /* Base */, 351 | ); 352 | name = LaunchScreen.xib; 353 | sourceTree = ""; 354 | }; 355 | /* End PBXVariantGroup section */ 356 | 357 | /* Begin XCBuildConfiguration section */ 358 | 2878A3301B4B7B2A00C4AC64 /* Debug */ = { 359 | isa = XCBuildConfiguration; 360 | buildSettings = { 361 | ALWAYS_SEARCH_USER_PATHS = NO; 362 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 363 | CLANG_CXX_LIBRARY = "libc++"; 364 | CLANG_ENABLE_MODULES = YES; 365 | CLANG_ENABLE_OBJC_ARC = YES; 366 | CLANG_WARN_BOOL_CONVERSION = YES; 367 | CLANG_WARN_CONSTANT_CONVERSION = YES; 368 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 369 | CLANG_WARN_EMPTY_BODY = YES; 370 | CLANG_WARN_ENUM_CONVERSION = YES; 371 | CLANG_WARN_INT_CONVERSION = YES; 372 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 373 | CLANG_WARN_UNREACHABLE_CODE = YES; 374 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 375 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 376 | COPY_PHASE_STRIP = NO; 377 | ENABLE_STRICT_OBJC_MSGSEND = YES; 378 | GCC_C_LANGUAGE_STANDARD = gnu99; 379 | GCC_DYNAMIC_NO_PIC = NO; 380 | GCC_OPTIMIZATION_LEVEL = 0; 381 | GCC_PREPROCESSOR_DEFINITIONS = ( 382 | "DEBUG=1", 383 | "$(inherited)", 384 | ); 385 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 386 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 387 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 388 | GCC_WARN_UNDECLARED_SELECTOR = YES; 389 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 390 | GCC_WARN_UNUSED_FUNCTION = YES; 391 | GCC_WARN_UNUSED_VARIABLE = YES; 392 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 393 | MTL_ENABLE_DEBUG_INFO = YES; 394 | ONLY_ACTIVE_ARCH = YES; 395 | SDKROOT = iphoneos; 396 | }; 397 | name = Debug; 398 | }; 399 | 2878A3311B4B7B2A00C4AC64 /* Release */ = { 400 | isa = XCBuildConfiguration; 401 | buildSettings = { 402 | ALWAYS_SEARCH_USER_PATHS = NO; 403 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 404 | CLANG_CXX_LIBRARY = "libc++"; 405 | CLANG_ENABLE_MODULES = YES; 406 | CLANG_ENABLE_OBJC_ARC = YES; 407 | CLANG_WARN_BOOL_CONVERSION = YES; 408 | CLANG_WARN_CONSTANT_CONVERSION = YES; 409 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 410 | CLANG_WARN_EMPTY_BODY = YES; 411 | CLANG_WARN_ENUM_CONVERSION = YES; 412 | CLANG_WARN_INT_CONVERSION = YES; 413 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 414 | CLANG_WARN_UNREACHABLE_CODE = YES; 415 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 416 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 417 | COPY_PHASE_STRIP = NO; 418 | ENABLE_NS_ASSERTIONS = NO; 419 | ENABLE_STRICT_OBJC_MSGSEND = YES; 420 | GCC_C_LANGUAGE_STANDARD = gnu99; 421 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 422 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 423 | GCC_WARN_UNDECLARED_SELECTOR = YES; 424 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 425 | GCC_WARN_UNUSED_FUNCTION = YES; 426 | GCC_WARN_UNUSED_VARIABLE = YES; 427 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 428 | MTL_ENABLE_DEBUG_INFO = NO; 429 | SDKROOT = iphoneos; 430 | VALIDATE_PRODUCT = YES; 431 | }; 432 | name = Release; 433 | }; 434 | 2878A3331B4B7B2A00C4AC64 /* Debug */ = { 435 | isa = XCBuildConfiguration; 436 | buildSettings = { 437 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 438 | INFOPLIST_FILE = Demo/Info.plist; 439 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 440 | PRODUCT_NAME = "$(TARGET_NAME)"; 441 | }; 442 | name = Debug; 443 | }; 444 | 2878A3341B4B7B2A00C4AC64 /* Release */ = { 445 | isa = XCBuildConfiguration; 446 | buildSettings = { 447 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 448 | INFOPLIST_FILE = Demo/Info.plist; 449 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 450 | PRODUCT_NAME = "$(TARGET_NAME)"; 451 | }; 452 | name = Release; 453 | }; 454 | 2878A3361B4B7B2A00C4AC64 /* Debug */ = { 455 | isa = XCBuildConfiguration; 456 | buildSettings = { 457 | BUNDLE_LOADER = "$(TEST_HOST)"; 458 | FRAMEWORK_SEARCH_PATHS = ( 459 | "$(SDKROOT)/Developer/Library/Frameworks", 460 | "$(inherited)", 461 | ); 462 | GCC_PREPROCESSOR_DEFINITIONS = ( 463 | "DEBUG=1", 464 | "$(inherited)", 465 | ); 466 | INFOPLIST_FILE = DemoTests/Info.plist; 467 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 468 | PRODUCT_NAME = "$(TARGET_NAME)"; 469 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo"; 470 | }; 471 | name = Debug; 472 | }; 473 | 2878A3371B4B7B2A00C4AC64 /* Release */ = { 474 | isa = XCBuildConfiguration; 475 | buildSettings = { 476 | BUNDLE_LOADER = "$(TEST_HOST)"; 477 | FRAMEWORK_SEARCH_PATHS = ( 478 | "$(SDKROOT)/Developer/Library/Frameworks", 479 | "$(inherited)", 480 | ); 481 | INFOPLIST_FILE = DemoTests/Info.plist; 482 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 483 | PRODUCT_NAME = "$(TARGET_NAME)"; 484 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo"; 485 | }; 486 | name = Release; 487 | }; 488 | /* End XCBuildConfiguration section */ 489 | 490 | /* Begin XCConfigurationList section */ 491 | 2878A3101B4B7B2A00C4AC64 /* Build configuration list for PBXProject "Demo" */ = { 492 | isa = XCConfigurationList; 493 | buildConfigurations = ( 494 | 2878A3301B4B7B2A00C4AC64 /* Debug */, 495 | 2878A3311B4B7B2A00C4AC64 /* Release */, 496 | ); 497 | defaultConfigurationIsVisible = 0; 498 | defaultConfigurationName = Release; 499 | }; 500 | 2878A3321B4B7B2A00C4AC64 /* Build configuration list for PBXNativeTarget "Demo" */ = { 501 | isa = XCConfigurationList; 502 | buildConfigurations = ( 503 | 2878A3331B4B7B2A00C4AC64 /* Debug */, 504 | 2878A3341B4B7B2A00C4AC64 /* Release */, 505 | ); 506 | defaultConfigurationIsVisible = 0; 507 | }; 508 | 2878A3351B4B7B2A00C4AC64 /* Build configuration list for PBXNativeTarget "DemoTests" */ = { 509 | isa = XCConfigurationList; 510 | buildConfigurations = ( 511 | 2878A3361B4B7B2A00C4AC64 /* Debug */, 512 | 2878A3371B4B7B2A00C4AC64 /* Release */, 513 | ); 514 | defaultConfigurationIsVisible = 0; 515 | }; 516 | /* End XCConfigurationList section */ 517 | }; 518 | rootObject = 2878A30D1B4B7B2A00C4AC64 /* Project object */; 519 | } 520 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/xcshareddata/Demo.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | E289C7C9-5081-4235-932A-3B543CB60563 9 | IDESourceControlProjectName 10 | Demo 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 7E02FFFB77511A53E4703B9E056B597846DB571D 14 | https://github.com/XQBoy/MasonryExercise.git 15 | 16 | IDESourceControlProjectPath 17 | Demo/Demo.xcodeproj 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 7E02FFFB77511A53E4703B9E056B597846DB571D 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/XQBoy/MasonryExercise.git 25 | IDESourceControlProjectVersion 26 | 111 27 | IDESourceControlProjectWCCIdentifier 28 | 7E02FFFB77511A53E4703B9E056B597846DB571D 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 7E02FFFB77511A53E4703B9E056B597846DB571D 36 | IDESourceControlWCCName 37 | MasonryExercise 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/project.xcworkspace/xcuserdata/abc.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo.xcodeproj/project.xcworkspace/xcuserdata/abc.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/xcuserdata/abc.xcuserdatad/xcschemes/Demo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 75 | 77 | 83 | 84 | 85 | 86 | 87 | 88 | 94 | 96 | 102 | 103 | 104 | 105 | 107 | 108 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /Demo/Demo.xcodeproj/xcuserdata/abc.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Demo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 2878A3141B4B7B2A00C4AC64 16 | 17 | primary 18 | 19 | 20 | 2878A3271B4B7B2A00C4AC64 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Demo/Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. 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 | -------------------------------------------------------------------------------- /Demo/Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "XQTableViewController.h" 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 20 | 21 | self.window.backgroundColor = [UIColor whiteColor]; 22 | 23 | [self.window makeKeyAndVisible]; 24 | 25 | self.window.rootViewController = [[XQTableViewController alloc] init]; 26 | 27 | return YES; 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game. 33 | } 34 | 35 | - (void)applicationDidEnterBackground:(UIApplication *)application { 36 | // 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. 37 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 38 | } 39 | 40 | - (void)applicationWillEnterForeground:(UIApplication *)application { 41 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 42 | } 43 | 44 | - (void)applicationDidBecomeActive:(UIApplication *)application { 45 | // 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. 46 | } 47 | 48 | - (void)applicationWillTerminate:(UIApplication *)application { 49 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /Demo/Demo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/breaddoge.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "dogebread.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/breaddoge.imageset/dogebread.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/breaddoge.imageset/dogebread.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/doge.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "doge@2x.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/doge.imageset/doge@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/doge.imageset/doge@2x.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/forkingdog.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "forkingdog@2x.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/forkingdog.imageset/forkingdog@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/forkingdog.imageset/forkingdog@2x.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/phil.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "phil.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/phil.imageset/phil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/phil.imageset/phil.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sark.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "sark@2x.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sark.imageset/sark@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/sark.imageset/sark@2x.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sinojerk.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "彪哥副本.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sinojerk.imageset/彪哥副本.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/sinojerk.imageset/彪哥副本.png -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sunnyxx.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x", 10 | "filename" : "下载.png" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /Demo/Demo/Images.xcassets/sunnyxx.imageset/下载.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/Images.xcassets/sunnyxx.imageset/下载.png -------------------------------------------------------------------------------- /Demo/Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | cn.-----.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Demo/Demo/Masonry/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.cloudling.$(PRODUCT_NAME:rfc1034identifier) 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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 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)setOffset:(CGFloat)offset { 133 | for (MASConstraint *constraint in self.childConstraints) { 134 | constraint.offset = offset; 135 | } 136 | } 137 | 138 | - (void)setSizeOffset:(CGSize)sizeOffset { 139 | for (MASConstraint *constraint in self.childConstraints) { 140 | constraint.sizeOffset = sizeOffset; 141 | } 142 | } 143 | 144 | - (void)setCenterOffset:(CGPoint)centerOffset { 145 | for (MASConstraint *constraint in self.childConstraints) { 146 | constraint.centerOffset = centerOffset; 147 | } 148 | } 149 | 150 | #pragma mark - MASConstraint 151 | 152 | - (void)activate { 153 | for (MASConstraint *constraint in self.childConstraints) { 154 | [constraint activate]; 155 | } 156 | } 157 | 158 | - (void)deactivate { 159 | for (MASConstraint *constraint in self.childConstraints) { 160 | [constraint deactivate]; 161 | } 162 | } 163 | 164 | - (void)install { 165 | for (MASConstraint *constraint in self.childConstraints) { 166 | constraint.updateExisting = self.updateExisting; 167 | [constraint install]; 168 | } 169 | } 170 | 171 | - (void)uninstall { 172 | for (MASConstraint *constraint in self.childConstraints) { 173 | [constraint uninstall]; 174 | } 175 | } 176 | 177 | @end 178 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 31 | */ 32 | - (MASConstraint * (^)(CGSize offset))sizeOffset; 33 | 34 | /** 35 | * Modifies the NSLayoutConstraint constant, 36 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 37 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 38 | */ 39 | - (MASConstraint * (^)(CGPoint offset))centerOffset; 40 | 41 | /** 42 | * Modifies the NSLayoutConstraint constant 43 | */ 44 | - (MASConstraint * (^)(CGFloat offset))offset; 45 | 46 | /** 47 | * Modifies the NSLayoutConstraint constant based on a value type 48 | */ 49 | - (MASConstraint * (^)(NSValue *value))valueOffset; 50 | 51 | /** 52 | * Sets the NSLayoutConstraint multiplier property 53 | */ 54 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy; 55 | 56 | /** 57 | * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy 58 | */ 59 | - (MASConstraint * (^)(CGFloat divider))dividedBy; 60 | 61 | /** 62 | * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority 63 | */ 64 | - (MASConstraint * (^)(MASLayoutPriority priority))priority; 65 | 66 | /** 67 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow 68 | */ 69 | - (MASConstraint * (^)())priorityLow; 70 | 71 | /** 72 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium 73 | */ 74 | - (MASConstraint * (^)())priorityMedium; 75 | 76 | /** 77 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh 78 | */ 79 | - (MASConstraint * (^)())priorityHigh; 80 | 81 | /** 82 | * Sets the constraint relation to NSLayoutRelationEqual 83 | * returns a block which accepts one of the following: 84 | * MASViewAttribute, UIView, NSValue, NSArray 85 | * see readme for more details. 86 | */ 87 | - (MASConstraint * (^)(id attr))equalTo; 88 | 89 | /** 90 | * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual 91 | * returns a block which accepts one of the following: 92 | * MASViewAttribute, UIView, NSValue, NSArray 93 | * see readme for more details. 94 | */ 95 | - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; 96 | 97 | /** 98 | * Sets the constraint relation to NSLayoutRelationLessThanOrEqual 99 | * returns a block which accepts one of the following: 100 | * MASViewAttribute, UIView, NSValue, NSArray 101 | * see readme for more details. 102 | */ 103 | - (MASConstraint * (^)(id attr))lessThanOrEqualTo; 104 | 105 | /** 106 | * Optional semantic property which has no effect but improves the readability of constraint 107 | */ 108 | - (MASConstraint *)with; 109 | 110 | /** 111 | * Optional semantic property which has no effect but improves the readability of constraint 112 | */ 113 | - (MASConstraint *)and; 114 | 115 | /** 116 | * Creates a new MASCompositeConstraint with the called attribute and reciever 117 | */ 118 | - (MASConstraint *)left; 119 | - (MASConstraint *)top; 120 | - (MASConstraint *)right; 121 | - (MASConstraint *)bottom; 122 | - (MASConstraint *)leading; 123 | - (MASConstraint *)trailing; 124 | - (MASConstraint *)width; 125 | - (MASConstraint *)height; 126 | - (MASConstraint *)centerX; 127 | - (MASConstraint *)centerY; 128 | - (MASConstraint *)baseline; 129 | 130 | #if TARGET_OS_IPHONE 131 | 132 | - (MASConstraint *)leftMargin; 133 | - (MASConstraint *)rightMargin; 134 | - (MASConstraint *)topMargin; 135 | - (MASConstraint *)bottomMargin; 136 | - (MASConstraint *)leadingMargin; 137 | - (MASConstraint *)trailingMargin; 138 | - (MASConstraint *)centerXWithinMargins; 139 | - (MASConstraint *)centerYWithinMargins; 140 | 141 | #endif 142 | 143 | 144 | /** 145 | * Sets the constraint debug name 146 | */ 147 | - (MASConstraint * (^)(id key))key; 148 | 149 | // NSLayoutConstraint constant Setters 150 | // for use outside of mas_updateConstraints/mas_makeConstraints blocks 151 | 152 | /** 153 | * Modifies the NSLayoutConstraint constant, 154 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 155 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 156 | */ 157 | - (void)setInsets:(MASEdgeInsets)insets; 158 | 159 | /** 160 | * Modifies the NSLayoutConstraint constant, 161 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 162 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 163 | */ 164 | - (void)setSizeOffset:(CGSize)sizeOffset; 165 | 166 | /** 167 | * Modifies the NSLayoutConstraint constant, 168 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 169 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 170 | */ 171 | - (void)setCenterOffset:(CGPoint)centerOffset; 172 | 173 | /** 174 | * Modifies the NSLayoutConstraint constant 175 | */ 176 | - (void)setOffset:(CGFloat)offset; 177 | 178 | 179 | // NSLayoutConstraint Installation support 180 | 181 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 182 | /** 183 | * Whether or not to go through the animator proxy when modifying the constraint 184 | */ 185 | @property (nonatomic, copy, readonly) MASConstraint *animator; 186 | #endif 187 | 188 | /** 189 | * Activates an NSLayoutConstraint if it's supported by an OS. 190 | * Invokes install otherwise. 191 | */ 192 | - (void)activate; 193 | 194 | /** 195 | * Deactivates previously installed/activated NSLayoutConstraint. 196 | */ 197 | - (void)deactivate; 198 | 199 | /** 200 | * Creates a NSLayoutConstraint and adds it to the appropriate view. 201 | */ 202 | - (void)install; 203 | 204 | /** 205 | * Removes previously installed NSLayoutConstraint 206 | */ 207 | - (void)uninstall; 208 | 209 | @end 210 | 211 | 212 | /** 213 | * Convenience auto-boxing macros for MASConstraint methods. 214 | * 215 | * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax. 216 | * A potential drawback of this is that the unprefixed macros will appear in global scope. 217 | */ 218 | #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__))) 219 | #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 220 | #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 221 | 222 | #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__))) 223 | 224 | 225 | #ifdef MAS_SHORTHAND_GLOBALS 226 | 227 | #define equalTo(...) mas_equalTo(__VA_ARGS__) 228 | #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__) 229 | #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__) 230 | 231 | #define offset(...) mas_offset(__VA_ARGS__) 232 | 233 | #endif 234 | 235 | 236 | @interface MASConstraint (AutoboxingSupport) 237 | 238 | /** 239 | * Aliases to corresponding relation methods (for shorthand macros) 240 | * Also needed to aid autocompletion 241 | */ 242 | - (MASConstraint * (^)(id attr))mas_equalTo; 243 | - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo; 244 | - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo; 245 | 246 | /** 247 | * A dummy method to aid autocompletion 248 | */ 249 | - (MASConstraint * (^)(id offset))mas_offset; 250 | 251 | @end 252 | -------------------------------------------------------------------------------- /Demo/Demo/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 * (^)(CGSize))sizeOffset { 96 | return ^id(CGSize offset) { 97 | self.sizeOffset = offset; 98 | return self; 99 | }; 100 | } 101 | 102 | - (MASConstraint * (^)(CGPoint))centerOffset { 103 | return ^id(CGPoint offset) { 104 | self.centerOffset = offset; 105 | return self; 106 | }; 107 | } 108 | 109 | - (MASConstraint * (^)(CGFloat))offset { 110 | return ^id(CGFloat offset){ 111 | self.offset = offset; 112 | return self; 113 | }; 114 | } 115 | 116 | - (MASConstraint * (^)(NSValue *value))valueOffset { 117 | return ^id(NSValue *offset) { 118 | NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset); 119 | [self setLayoutConstantWithValue:offset]; 120 | return self; 121 | }; 122 | } 123 | 124 | - (MASConstraint * (^)(id offset))mas_offset { 125 | // Will never be called due to macro 126 | return nil; 127 | } 128 | 129 | #pragma mark - NSLayoutConstraint constant setter 130 | 131 | - (void)setLayoutConstantWithValue:(NSValue *)value { 132 | if ([value isKindOfClass:NSNumber.class]) { 133 | self.offset = [(NSNumber *)value doubleValue]; 134 | } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { 135 | CGPoint point; 136 | [value getValue:&point]; 137 | self.centerOffset = point; 138 | } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { 139 | CGSize size; 140 | [value getValue:&size]; 141 | self.sizeOffset = size; 142 | } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { 143 | MASEdgeInsets insets; 144 | [value getValue:&insets]; 145 | self.insets = insets; 146 | } else { 147 | NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); 148 | } 149 | } 150 | 151 | #pragma mark - Semantic properties 152 | 153 | - (MASConstraint *)with { 154 | return self; 155 | } 156 | 157 | - (MASConstraint *)and { 158 | return self; 159 | } 160 | 161 | #pragma mark - Chaining 162 | 163 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute { 164 | MASMethodNotImplemented(); 165 | } 166 | 167 | - (MASConstraint *)left { 168 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 169 | } 170 | 171 | - (MASConstraint *)top { 172 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 173 | } 174 | 175 | - (MASConstraint *)right { 176 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 177 | } 178 | 179 | - (MASConstraint *)bottom { 180 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 181 | } 182 | 183 | - (MASConstraint *)leading { 184 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 185 | } 186 | 187 | - (MASConstraint *)trailing { 188 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 189 | } 190 | 191 | - (MASConstraint *)width { 192 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 193 | } 194 | 195 | - (MASConstraint *)height { 196 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 197 | } 198 | 199 | - (MASConstraint *)centerX { 200 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 201 | } 202 | 203 | - (MASConstraint *)centerY { 204 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 205 | } 206 | 207 | - (MASConstraint *)baseline { 208 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 209 | } 210 | 211 | #if TARGET_OS_IPHONE 212 | 213 | - (MASConstraint *)leftMargin { 214 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 215 | } 216 | 217 | - (MASConstraint *)rightMargin { 218 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 219 | } 220 | 221 | - (MASConstraint *)topMargin { 222 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 223 | } 224 | 225 | - (MASConstraint *)bottomMargin { 226 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 227 | } 228 | 229 | - (MASConstraint *)leadingMargin { 230 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 231 | } 232 | 233 | - (MASConstraint *)trailingMargin { 234 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 235 | } 236 | 237 | - (MASConstraint *)centerXWithinMargins { 238 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 239 | } 240 | 241 | - (MASConstraint *)centerYWithinMargins { 242 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 243 | } 244 | 245 | #endif 246 | 247 | #pragma mark - Abstract 248 | 249 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } 250 | 251 | - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); } 252 | 253 | - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); } 254 | 255 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); } 256 | 257 | - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); } 258 | 259 | - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); } 260 | 261 | - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); } 262 | 263 | - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); } 264 | 265 | - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); } 266 | 267 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 268 | 269 | - (MASConstraint *)animator { MASMethodNotImplemented(); } 270 | 271 | #endif 272 | 273 | - (void)activate { MASMethodNotImplemented(); } 274 | 275 | - (void)deactivate { MASMethodNotImplemented(); } 276 | 277 | - (void)install { MASMethodNotImplemented(); } 278 | 279 | - (void)uninstall { MASMethodNotImplemented(); } 280 | 281 | @end 282 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 26 | 27 | MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, 28 | MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, 29 | MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, 30 | MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, 31 | MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, 32 | MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, 33 | MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, 34 | MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, 35 | 36 | #endif 37 | 38 | }; 39 | 40 | /** 41 | * Provides factory methods for creating MASConstraints. 42 | * Constraints are collected until they are ready to be installed 43 | * 44 | */ 45 | @interface MASConstraintMaker : NSObject 46 | 47 | /** 48 | * The following properties return a new MASViewConstraint 49 | * with the first item set to the makers associated view and the appropriate MASViewAttribute 50 | */ 51 | @property (nonatomic, strong, readonly) MASConstraint *left; 52 | @property (nonatomic, strong, readonly) MASConstraint *top; 53 | @property (nonatomic, strong, readonly) MASConstraint *right; 54 | @property (nonatomic, strong, readonly) MASConstraint *bottom; 55 | @property (nonatomic, strong, readonly) MASConstraint *leading; 56 | @property (nonatomic, strong, readonly) MASConstraint *trailing; 57 | @property (nonatomic, strong, readonly) MASConstraint *width; 58 | @property (nonatomic, strong, readonly) MASConstraint *height; 59 | @property (nonatomic, strong, readonly) MASConstraint *centerX; 60 | @property (nonatomic, strong, readonly) MASConstraint *centerY; 61 | @property (nonatomic, strong, readonly) MASConstraint *baseline; 62 | 63 | #if TARGET_OS_IPHONE 64 | 65 | @property (nonatomic, strong, readonly) MASConstraint *leftMargin; 66 | @property (nonatomic, strong, readonly) MASConstraint *rightMargin; 67 | @property (nonatomic, strong, readonly) MASConstraint *topMargin; 68 | @property (nonatomic, strong, readonly) MASConstraint *bottomMargin; 69 | @property (nonatomic, strong, readonly) MASConstraint *leadingMargin; 70 | @property (nonatomic, strong, readonly) MASConstraint *trailingMargin; 71 | @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; 72 | @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; 73 | 74 | #endif 75 | 76 | /** 77 | * Returns a block which creates a new MASCompositeConstraint with the first item set 78 | * to the makers associated view and children corresponding to the set bits in the 79 | * MASAttribute parameter. Combine multiple attributes via binary-or. 80 | */ 81 | @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs); 82 | 83 | /** 84 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges 85 | * which generates the appropriate MASViewConstraint children (top, left, bottom, right) 86 | * with the first item set to the makers associated view 87 | */ 88 | @property (nonatomic, strong, readonly) MASConstraint *edges; 89 | 90 | /** 91 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize 92 | * which generates the appropriate MASViewConstraint children (width, height) 93 | * with the first item set to the makers associated view 94 | */ 95 | @property (nonatomic, strong, readonly) MASConstraint *size; 96 | 97 | /** 98 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter 99 | * which generates the appropriate MASViewConstraint children (centerX, centerY) 100 | * with the first item set to the makers associated view 101 | */ 102 | @property (nonatomic, strong, readonly) MASConstraint *center; 103 | 104 | /** 105 | * Whether or not to check for an existing constraint instead of adding constraint 106 | */ 107 | @property (nonatomic, assign) BOOL updateExisting; 108 | 109 | /** 110 | * Whether or not to remove existing constraints prior to installing 111 | */ 112 | @property (nonatomic, assign) BOOL removeExisting; 113 | 114 | /** 115 | * initialises the maker with a default view 116 | * 117 | * @param view any MASConstrait are created with this view as the first item 118 | * 119 | * @return a new MASConstraintMaker 120 | */ 121 | - (id)initWithView:(MAS_VIEW *)view; 122 | 123 | /** 124 | * Calls install method on any MASConstraints which have been created by this maker 125 | * 126 | * @return an array of all the installed MASConstraints 127 | */ 128 | - (NSArray *)install; 129 | 130 | - (MASConstraint * (^)(dispatch_block_t))group; 131 | 132 | @end 133 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 82 | | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin 83 | | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins 84 | | MASAttributeCenterYWithinMargins 85 | #endif 86 | ); 87 | 88 | NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); 89 | 90 | NSMutableArray *attributes = [NSMutableArray array]; 91 | 92 | if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left]; 93 | if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right]; 94 | if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top]; 95 | if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom]; 96 | if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading]; 97 | if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing]; 98 | if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width]; 99 | if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height]; 100 | if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX]; 101 | if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; 102 | if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; 103 | 104 | #if TARGET_OS_IPHONE 105 | 106 | if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; 107 | if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; 108 | if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; 109 | if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; 110 | if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; 111 | if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; 112 | if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; 113 | if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; 114 | 115 | #endif 116 | 117 | NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; 118 | 119 | for (MASViewAttribute *a in attributes) { 120 | [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]]; 121 | } 122 | 123 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 124 | constraint.delegate = self; 125 | [self.constraints addObject:constraint]; 126 | return constraint; 127 | } 128 | 129 | #pragma mark - standard Attributes 130 | 131 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 132 | return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute]; 133 | } 134 | 135 | - (MASConstraint *)left { 136 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 137 | } 138 | 139 | - (MASConstraint *)top { 140 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 141 | } 142 | 143 | - (MASConstraint *)right { 144 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 145 | } 146 | 147 | - (MASConstraint *)bottom { 148 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 149 | } 150 | 151 | - (MASConstraint *)leading { 152 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 153 | } 154 | 155 | - (MASConstraint *)trailing { 156 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 157 | } 158 | 159 | - (MASConstraint *)width { 160 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 161 | } 162 | 163 | - (MASConstraint *)height { 164 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 165 | } 166 | 167 | - (MASConstraint *)centerX { 168 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 169 | } 170 | 171 | - (MASConstraint *)centerY { 172 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 173 | } 174 | 175 | - (MASConstraint *)baseline { 176 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 177 | } 178 | 179 | - (MASConstraint *(^)(MASAttribute))attributes { 180 | return ^(MASAttribute attrs){ 181 | return [self addConstraintWithAttributes:attrs]; 182 | }; 183 | } 184 | 185 | #if TARGET_OS_IPHONE 186 | 187 | - (MASConstraint *)leftMargin { 188 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 189 | } 190 | 191 | - (MASConstraint *)rightMargin { 192 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 193 | } 194 | 195 | - (MASConstraint *)topMargin { 196 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 197 | } 198 | 199 | - (MASConstraint *)bottomMargin { 200 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 201 | } 202 | 203 | - (MASConstraint *)leadingMargin { 204 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 205 | } 206 | 207 | - (MASConstraint *)trailingMargin { 208 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 209 | } 210 | 211 | - (MASConstraint *)centerXWithinMargins { 212 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 213 | } 214 | 215 | - (MASConstraint *)centerYWithinMargins { 216 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 217 | } 218 | 219 | #endif 220 | 221 | 222 | #pragma mark - composite Attributes 223 | 224 | - (MASConstraint *)edges { 225 | return [self addConstraintWithAttributes:MASAttributeTop | MASAttributeLeft | MASAttributeRight | MASAttributeBottom]; 226 | } 227 | 228 | - (MASConstraint *)size { 229 | return [self addConstraintWithAttributes:MASAttributeWidth | MASAttributeHeight]; 230 | } 231 | 232 | - (MASConstraint *)center { 233 | return [self addConstraintWithAttributes:MASAttributeCenterX | MASAttributeCenterY]; 234 | } 235 | 236 | #pragma mark - grouping 237 | 238 | - (MASConstraint *(^)(dispatch_block_t group))group { 239 | return ^id(dispatch_block_t group) { 240 | NSInteger previousCount = self.constraints.count; 241 | group(); 242 | 243 | NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)]; 244 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 245 | constraint.delegate = self; 246 | return constraint; 247 | }; 248 | } 249 | 250 | @end 251 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | #if TARGET_OS_IPHONE 12 | 13 | #import 14 | #define MAS_VIEW UIView 15 | #define MAS_VIEW_CONTROLLER UIViewController 16 | #define MASEdgeInsets UIEdgeInsets 17 | 18 | typedef UILayoutPriority MASLayoutPriority; 19 | static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired; 20 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh; 21 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500; 22 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow; 23 | static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel; 24 | 25 | #elif TARGET_OS_MAC 26 | 27 | #import 28 | #define MAS_VIEW NSView 29 | #define MASEdgeInsets NSEdgeInsets 30 | 31 | typedef NSLayoutPriority MASLayoutPriority; 32 | static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired; 33 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh; 34 | static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow; 35 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501; 36 | static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut; 37 | static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow; 38 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow; 39 | static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression; 40 | 41 | #endif 42 | 43 | /** 44 | * Allows you to attach keys to objects matching the variable names passed. 45 | * 46 | * view1.mas_key = @"view1", view2.mas_key = @"view2"; 47 | * 48 | * is equivalent to: 49 | * 50 | * MASAttachKeys(view1, view2); 51 | */ 52 | #define MASAttachKeys(...) \ 53 | NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \ 54 | for (id key in keyPairs.allKeys) { \ 55 | id obj = keyPairs[key]; \ 56 | NSAssert([obj respondsToSelector:@selector(setMas_key:)], \ 57 | @"Cannot attach mas_key to %@", obj); \ 58 | [obj setMas_key:key]; \ 59 | } 60 | 61 | /** 62 | * Used to create object hashes 63 | * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html 64 | */ 65 | #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger)) 66 | #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch))) 67 | 68 | /** 69 | * Given a scalar or struct value, wraps it in NSValue 70 | * Based on EXPObjectify: https://github.com/specta/expecta 71 | */ 72 | static inline id _MASBoxValue(const char *type, ...) { 73 | va_list v; 74 | va_start(v, type); 75 | id obj = nil; 76 | if (strcmp(type, @encode(id)) == 0) { 77 | id actual = va_arg(v, id); 78 | obj = actual; 79 | } else if (strcmp(type, @encode(CGPoint)) == 0) { 80 | CGPoint actual = (CGPoint)va_arg(v, CGPoint); 81 | obj = [NSValue value:&actual withObjCType:type]; 82 | } else if (strcmp(type, @encode(CGSize)) == 0) { 83 | CGSize actual = (CGSize)va_arg(v, CGSize); 84 | obj = [NSValue value:&actual withObjCType:type]; 85 | } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) { 86 | MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets); 87 | obj = [NSValue value:&actual withObjCType:type]; 88 | } else if (strcmp(type, @encode(double)) == 0) { 89 | double actual = (double)va_arg(v, double); 90 | obj = [NSNumber numberWithDouble:actual]; 91 | } else if (strcmp(type, @encode(float)) == 0) { 92 | float actual = (float)va_arg(v, double); 93 | obj = [NSNumber numberWithFloat:actual]; 94 | } else if (strcmp(type, @encode(int)) == 0) { 95 | int actual = (int)va_arg(v, int); 96 | obj = [NSNumber numberWithInt:actual]; 97 | } else if (strcmp(type, @encode(long)) == 0) { 98 | long actual = (long)va_arg(v, long); 99 | obj = [NSNumber numberWithLong:actual]; 100 | } else if (strcmp(type, @encode(long long)) == 0) { 101 | long long actual = (long long)va_arg(v, long long); 102 | obj = [NSNumber numberWithLongLong:actual]; 103 | } else if (strcmp(type, @encode(short)) == 0) { 104 | short actual = (short)va_arg(v, int); 105 | obj = [NSNumber numberWithShort:actual]; 106 | } else if (strcmp(type, @encode(char)) == 0) { 107 | char actual = (char)va_arg(v, int); 108 | obj = [NSNumber numberWithChar:actual]; 109 | } else if (strcmp(type, @encode(bool)) == 0) { 110 | bool actual = (bool)va_arg(v, int); 111 | obj = [NSNumber numberWithBool:actual]; 112 | } else if (strcmp(type, @encode(unsigned char)) == 0) { 113 | unsigned char actual = (unsigned char)va_arg(v, unsigned int); 114 | obj = [NSNumber numberWithUnsignedChar:actual]; 115 | } else if (strcmp(type, @encode(unsigned int)) == 0) { 116 | unsigned int actual = (unsigned int)va_arg(v, unsigned int); 117 | obj = [NSNumber numberWithUnsignedInt:actual]; 118 | } else if (strcmp(type, @encode(unsigned long)) == 0) { 119 | unsigned long actual = (unsigned long)va_arg(v, unsigned long); 120 | obj = [NSNumber numberWithUnsignedLong:actual]; 121 | } else if (strcmp(type, @encode(unsigned long long)) == 0) { 122 | unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long); 123 | obj = [NSNumber numberWithUnsignedLongLong:actual]; 124 | } else if (strcmp(type, @encode(unsigned short)) == 0) { 125 | unsigned short actual = (unsigned short)va_arg(v, unsigned int); 126 | obj = [NSNumber numberWithUnsignedShort:actual]; 127 | } 128 | va_end(v); 129 | return obj; 130 | } 131 | 132 | #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) 133 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/Masonry/MASViewConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.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 "MASViewConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASLayoutConstraint.h" 13 | #import "View+MASAdditions.h" 14 | #import 15 | 16 | @interface MAS_VIEW (MASConstraints) 17 | 18 | @property (nonatomic, readonly) NSMutableSet *mas_installedConstraints; 19 | 20 | @end 21 | 22 | @implementation MAS_VIEW (MASConstraints) 23 | 24 | static char kInstalledConstraintsKey; 25 | 26 | - (NSMutableSet *)mas_installedConstraints { 27 | NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey); 28 | if (!constraints) { 29 | constraints = [NSMutableSet set]; 30 | objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 31 | } 32 | return constraints; 33 | } 34 | 35 | @end 36 | 37 | 38 | @interface MASViewConstraint () 39 | 40 | @property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute; 41 | @property (nonatomic, weak) MAS_VIEW *installedView; 42 | @property (nonatomic, weak) MASLayoutConstraint *layoutConstraint; 43 | @property (nonatomic, assign) NSLayoutRelation layoutRelation; 44 | @property (nonatomic, assign) MASLayoutPriority layoutPriority; 45 | @property (nonatomic, assign) CGFloat layoutMultiplier; 46 | @property (nonatomic, assign) CGFloat layoutConstant; 47 | @property (nonatomic, assign) BOOL hasLayoutRelation; 48 | @property (nonatomic, strong) id mas_key; 49 | @property (nonatomic, assign) BOOL useAnimator; 50 | 51 | @end 52 | 53 | @implementation MASViewConstraint 54 | 55 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute { 56 | self = [super init]; 57 | if (!self) return nil; 58 | 59 | _firstViewAttribute = firstViewAttribute; 60 | self.layoutPriority = MASLayoutPriorityRequired; 61 | self.layoutMultiplier = 1; 62 | 63 | return self; 64 | } 65 | 66 | #pragma mark - NSCoping 67 | 68 | - (id)copyWithZone:(NSZone __unused *)zone { 69 | MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute]; 70 | constraint.layoutConstant = self.layoutConstant; 71 | constraint.layoutRelation = self.layoutRelation; 72 | constraint.layoutPriority = self.layoutPriority; 73 | constraint.layoutMultiplier = self.layoutMultiplier; 74 | constraint.delegate = self.delegate; 75 | return constraint; 76 | } 77 | 78 | #pragma mark - Public 79 | 80 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view { 81 | return [view.mas_installedConstraints allObjects]; 82 | } 83 | 84 | #pragma mark - Private 85 | 86 | - (void)setLayoutConstant:(CGFloat)layoutConstant { 87 | _layoutConstant = layoutConstant; 88 | 89 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 90 | if (self.useAnimator) { 91 | [self.layoutConstraint.animator setConstant:layoutConstant]; 92 | } else { 93 | self.layoutConstraint.constant = layoutConstant; 94 | } 95 | #else 96 | self.layoutConstraint.constant = layoutConstant; 97 | #endif 98 | } 99 | 100 | - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation { 101 | _layoutRelation = layoutRelation; 102 | self.hasLayoutRelation = YES; 103 | } 104 | 105 | - (BOOL)supportsActiveProperty { 106 | return [self.layoutConstraint respondsToSelector:@selector(isActive)]; 107 | } 108 | 109 | - (BOOL)isActive { 110 | BOOL active = YES; 111 | if ([self supportsActiveProperty]) { 112 | active = [self.layoutConstraint isActive]; 113 | } 114 | 115 | return active; 116 | } 117 | 118 | - (BOOL)hasBeenInstalled { 119 | return (self.layoutConstraint != nil) && [self isActive]; 120 | } 121 | 122 | - (void)setSecondViewAttribute:(id)secondViewAttribute { 123 | if ([secondViewAttribute isKindOfClass:NSValue.class]) { 124 | [self setLayoutConstantWithValue:secondViewAttribute]; 125 | } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) { 126 | _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute]; 127 | } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) { 128 | _secondViewAttribute = secondViewAttribute; 129 | } else { 130 | NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute); 131 | } 132 | } 133 | 134 | #pragma mark - NSLayoutConstraint multiplier proxies 135 | 136 | - (MASConstraint * (^)(CGFloat))multipliedBy { 137 | return ^id(CGFloat multiplier) { 138 | NSAssert(!self.hasBeenInstalled, 139 | @"Cannot modify constraint multiplier after it has been installed"); 140 | 141 | self.layoutMultiplier = multiplier; 142 | return self; 143 | }; 144 | } 145 | 146 | 147 | - (MASConstraint * (^)(CGFloat))dividedBy { 148 | return ^id(CGFloat divider) { 149 | NSAssert(!self.hasBeenInstalled, 150 | @"Cannot modify constraint multiplier after it has been installed"); 151 | 152 | self.layoutMultiplier = 1.0/divider; 153 | return self; 154 | }; 155 | } 156 | 157 | #pragma mark - MASLayoutPriority proxy 158 | 159 | - (MASConstraint * (^)(MASLayoutPriority))priority { 160 | return ^id(MASLayoutPriority priority) { 161 | NSAssert(!self.hasBeenInstalled, 162 | @"Cannot modify constraint priority after it has been installed"); 163 | 164 | self.layoutPriority = priority; 165 | return self; 166 | }; 167 | } 168 | 169 | #pragma mark - NSLayoutRelation proxy 170 | 171 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 172 | return ^id(id attribute, NSLayoutRelation relation) { 173 | if ([attribute isKindOfClass:NSArray.class]) { 174 | NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation"); 175 | NSMutableArray *children = NSMutableArray.new; 176 | for (id attr in attribute) { 177 | MASViewConstraint *viewConstraint = [self copy]; 178 | viewConstraint.secondViewAttribute = attr; 179 | [children addObject:viewConstraint]; 180 | } 181 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 182 | compositeConstraint.delegate = self.delegate; 183 | [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint]; 184 | return compositeConstraint; 185 | } else { 186 | NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation"); 187 | self.layoutRelation = relation; 188 | self.secondViewAttribute = attribute; 189 | return self; 190 | } 191 | }; 192 | } 193 | 194 | #pragma mark - Semantic properties 195 | 196 | - (MASConstraint *)with { 197 | return self; 198 | } 199 | 200 | - (MASConstraint *)and { 201 | return self; 202 | } 203 | 204 | #pragma mark - attribute chaining 205 | 206 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 207 | NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation"); 208 | 209 | return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 210 | } 211 | 212 | #pragma mark - Animator proxy 213 | 214 | #if TARGET_OS_MAC && !TARGET_OS_IPHONE 215 | 216 | - (MASConstraint *)animator { 217 | self.useAnimator = YES; 218 | return self; 219 | } 220 | 221 | #endif 222 | 223 | #pragma mark - debug helpers 224 | 225 | - (MASConstraint * (^)(id))key { 226 | return ^id(id key) { 227 | self.mas_key = key; 228 | return self; 229 | }; 230 | } 231 | 232 | #pragma mark - NSLayoutConstraint constant setters 233 | 234 | - (void)setInsets:(MASEdgeInsets)insets { 235 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 236 | switch (layoutAttribute) { 237 | case NSLayoutAttributeLeft: 238 | case NSLayoutAttributeLeading: 239 | self.layoutConstant = insets.left; 240 | break; 241 | case NSLayoutAttributeTop: 242 | self.layoutConstant = insets.top; 243 | break; 244 | case NSLayoutAttributeBottom: 245 | self.layoutConstant = -insets.bottom; 246 | break; 247 | case NSLayoutAttributeRight: 248 | case NSLayoutAttributeTrailing: 249 | self.layoutConstant = -insets.right; 250 | break; 251 | default: 252 | break; 253 | } 254 | } 255 | 256 | - (void)setOffset:(CGFloat)offset { 257 | self.layoutConstant = offset; 258 | } 259 | 260 | - (void)setSizeOffset:(CGSize)sizeOffset { 261 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 262 | switch (layoutAttribute) { 263 | case NSLayoutAttributeWidth: 264 | self.layoutConstant = sizeOffset.width; 265 | break; 266 | case NSLayoutAttributeHeight: 267 | self.layoutConstant = sizeOffset.height; 268 | break; 269 | default: 270 | break; 271 | } 272 | } 273 | 274 | - (void)setCenterOffset:(CGPoint)centerOffset { 275 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 276 | switch (layoutAttribute) { 277 | case NSLayoutAttributeCenterX: 278 | self.layoutConstant = centerOffset.x; 279 | break; 280 | case NSLayoutAttributeCenterY: 281 | self.layoutConstant = centerOffset.y; 282 | break; 283 | default: 284 | break; 285 | } 286 | } 287 | 288 | #pragma mark - MASConstraint 289 | 290 | - (void)activate { 291 | if ([self supportsActiveProperty] && self.layoutConstraint) { 292 | if (self.hasBeenInstalled) { 293 | return; 294 | } 295 | self.layoutConstraint.active = YES; 296 | [self.firstViewAttribute.view.mas_installedConstraints addObject:self]; 297 | } else { 298 | [self install]; 299 | } 300 | } 301 | 302 | - (void)deactivate { 303 | if ([self supportsActiveProperty]) { 304 | self.layoutConstraint.active = NO; 305 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 306 | } else { 307 | [self uninstall]; 308 | } 309 | } 310 | 311 | - (void)install { 312 | if (self.hasBeenInstalled) { 313 | return; 314 | } 315 | 316 | MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item; 317 | NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute; 318 | MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item; 319 | NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute; 320 | 321 | // alignment attributes must have a secondViewAttribute 322 | // therefore we assume that is refering to superview 323 | // eg make.left.equalTo(@10) 324 | if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) { 325 | secondLayoutItem = self.firstViewAttribute.view.superview; 326 | secondLayoutAttribute = firstLayoutAttribute; 327 | } 328 | 329 | MASLayoutConstraint *layoutConstraint 330 | = [MASLayoutConstraint constraintWithItem:firstLayoutItem 331 | attribute:firstLayoutAttribute 332 | relatedBy:self.layoutRelation 333 | toItem:secondLayoutItem 334 | attribute:secondLayoutAttribute 335 | multiplier:self.layoutMultiplier 336 | constant:self.layoutConstant]; 337 | 338 | layoutConstraint.priority = self.layoutPriority; 339 | layoutConstraint.mas_key = self.mas_key; 340 | 341 | if (self.secondViewAttribute.view) { 342 | MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view]; 343 | NSAssert(closestCommonSuperview, 344 | @"couldn't find a common superview for %@ and %@", 345 | self.firstViewAttribute.view, self.secondViewAttribute.view); 346 | self.installedView = closestCommonSuperview; 347 | } else if (self.firstViewAttribute.isSizeAttribute) { 348 | self.installedView = self.firstViewAttribute.view; 349 | } else { 350 | self.installedView = self.firstViewAttribute.view.superview; 351 | } 352 | 353 | 354 | MASLayoutConstraint *existingConstraint = nil; 355 | if (self.updateExisting) { 356 | existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint]; 357 | } 358 | if (existingConstraint) { 359 | // just update the constant 360 | existingConstraint.constant = layoutConstraint.constant; 361 | self.layoutConstraint = existingConstraint; 362 | } else { 363 | [self.installedView addConstraint:layoutConstraint]; 364 | self.layoutConstraint = layoutConstraint; 365 | [firstLayoutItem.mas_installedConstraints addObject:self]; 366 | } 367 | } 368 | 369 | - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint { 370 | // check if any constraints are the same apart from the only mutable property constant 371 | 372 | // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints 373 | // and they are likely to be added first. 374 | for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) { 375 | if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue; 376 | if (existingConstraint.firstItem != layoutConstraint.firstItem) continue; 377 | if (existingConstraint.secondItem != layoutConstraint.secondItem) continue; 378 | if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue; 379 | if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue; 380 | if (existingConstraint.relation != layoutConstraint.relation) continue; 381 | if (existingConstraint.multiplier != layoutConstraint.multiplier) continue; 382 | if (existingConstraint.priority != layoutConstraint.priority) continue; 383 | 384 | return (id)existingConstraint; 385 | } 386 | return nil; 387 | } 388 | 389 | - (void)uninstall { 390 | [self.installedView removeConstraint:self.layoutConstraint]; 391 | self.layoutConstraint = nil; 392 | self.installedView = nil; 393 | 394 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 395 | } 396 | 397 | @end 398 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | @interface NSArray (MASAdditions) 14 | 15 | /** 16 | * Creates a MASConstraintMaker with each view in the callee. 17 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view 18 | * 19 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 20 | * 21 | * @return Array of created MASConstraints 22 | */ 23 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; 24 | 25 | /** 26 | * Creates a MASConstraintMaker with each view in the callee. 27 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 28 | * If an existing constraint exists then it will be updated instead. 29 | * 30 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 31 | * 32 | * @return Array of created/updated MASConstraints 33 | */ 34 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; 35 | 36 | /** 37 | * Creates a MASConstraintMaker with each view in the callee. 38 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 39 | * All constraints previously installed for the views will be removed. 40 | * 41 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 42 | * 43 | * @return Array of created/updated MASConstraints 44 | */ 45 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Demo/Demo/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 | @end 42 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 48 | @(NSLayoutAttributeLeftMargin) : @"leftMargin", 49 | @(NSLayoutAttributeRightMargin) : @"rightMargin", 50 | @(NSLayoutAttributeTopMargin) : @"topMargin", 51 | @(NSLayoutAttributeBottomMargin) : @"bottomMargin", 52 | @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", 53 | @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", 54 | @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", 55 | @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", 56 | #endif 57 | 58 | }; 59 | 60 | }); 61 | return descriptionMap; 62 | } 63 | 64 | 65 | + (NSDictionary *)layoutPriorityDescriptionsByValue { 66 | static dispatch_once_t once; 67 | static NSDictionary *descriptionMap; 68 | dispatch_once(&once, ^{ 69 | #if TARGET_OS_IPHONE 70 | descriptionMap = @{ 71 | @(MASLayoutPriorityDefaultHigh) : @"high", 72 | @(MASLayoutPriorityDefaultLow) : @"low", 73 | @(MASLayoutPriorityDefaultMedium) : @"medium", 74 | @(MASLayoutPriorityRequired) : @"required", 75 | @(MASLayoutPriorityFittingSizeLevel) : @"fitting size", 76 | }; 77 | #elif TARGET_OS_MAC 78 | descriptionMap = @{ 79 | @(MASLayoutPriorityDefaultHigh) : @"high", 80 | @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window", 81 | @(MASLayoutPriorityDefaultMedium) : @"medium", 82 | @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put", 83 | @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window", 84 | @(MASLayoutPriorityDefaultLow) : @"low", 85 | @(MASLayoutPriorityFittingSizeCompression) : @"fitting size", 86 | @(MASLayoutPriorityRequired) : @"required", 87 | }; 88 | #endif 89 | }); 90 | return descriptionMap; 91 | } 92 | 93 | #pragma mark - description override 94 | 95 | + (NSString *)descriptionForObject:(id)obj { 96 | if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) { 97 | return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]]; 98 | } 99 | return [NSString stringWithFormat:@"%@:%p", [obj class], obj]; 100 | } 101 | 102 | - (NSString *)description { 103 | NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"]; 104 | 105 | [description appendString:[self.class descriptionForObject:self]]; 106 | 107 | [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]]; 108 | if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) { 109 | [description appendFormat:@".%@", [self.class.layoutAttributeDescriptionsByValue objectForKey:@(self.firstAttribute)]]; 110 | } 111 | 112 | [description appendFormat:@" %@", [self.class.layoutRelationDescriptionsByValue objectForKey:@(self.relation)]]; 113 | 114 | if (self.secondItem) { 115 | [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]]; 116 | } 117 | if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) { 118 | [description appendFormat:@".%@", [self.class.layoutAttributeDescriptionsByValue objectForKey:@(self.secondAttribute)]]; 119 | } 120 | 121 | if (self.multiplier != 1) { 122 | [description appendFormat:@" * %g", self.multiplier]; 123 | } 124 | 125 | if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) { 126 | [description appendFormat:@" %g", self.constant]; 127 | } else { 128 | if (self.constant) { 129 | [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)]; 130 | } 131 | } 132 | 133 | if (self.priority != MASLayoutPriorityRequired) { 134 | [description appendFormat:@" ^%@", [self.class.layoutPriorityDescriptionsByValue objectForKey:@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]]; 135 | } 136 | 137 | [description appendString:@">"]; 138 | return description; 139 | } 140 | 141 | @end 142 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 36 | 37 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; 39 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; 40 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; 41 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; 45 | 46 | #endif 47 | 48 | /** 49 | * a key to associate with this view 50 | */ 51 | @property (nonatomic, strong) id mas_key; 52 | 53 | /** 54 | * Finds the closest common superview between this view and another view 55 | * 56 | * @param view other view 57 | * 58 | * @return returns nil if common superview could not be found 59 | */ 60 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view; 61 | 62 | /** 63 | * Creates a MASConstraintMaker with the callee view. 64 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing 65 | * 66 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 67 | * 68 | * @return Array of created MASConstraints 69 | */ 70 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; 71 | 72 | /** 73 | * Creates a MASConstraintMaker with the callee view. 74 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 75 | * If an existing constraint exists then it will be updated instead. 76 | * 77 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 78 | * 79 | * @return Array of created/updated MASConstraints 80 | */ 81 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; 82 | 83 | /** 84 | * Creates a MASConstraintMaker with the callee view. 85 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 86 | * All constraints previously installed for the view will be removed. 87 | * 88 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 89 | * 90 | * @return Array of created/updated MASConstraints 91 | */ 92 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block; 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 91 | 92 | - (MASViewAttribute *)mas_leftMargin { 93 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; 94 | } 95 | 96 | - (MASViewAttribute *)mas_rightMargin { 97 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; 98 | } 99 | 100 | - (MASViewAttribute *)mas_topMargin { 101 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; 102 | } 103 | 104 | - (MASViewAttribute *)mas_bottomMargin { 105 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; 106 | } 107 | 108 | - (MASViewAttribute *)mas_leadingMargin { 109 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; 110 | } 111 | 112 | - (MASViewAttribute *)mas_trailingMargin { 113 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; 114 | } 115 | 116 | - (MASViewAttribute *)mas_centerXWithinMargins { 117 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 118 | } 119 | 120 | - (MASViewAttribute *)mas_centerYWithinMargins { 121 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 122 | } 123 | 124 | #endif 125 | 126 | #pragma mark - associated properties 127 | 128 | - (id)mas_key { 129 | return objc_getAssociatedObject(self, @selector(mas_key)); 130 | } 131 | 132 | - (void)setMas_key:(id)key { 133 | objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 134 | } 135 | 136 | #pragma mark - heirachy 137 | 138 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { 139 | MAS_VIEW *closestCommonSuperview = nil; 140 | 141 | MAS_VIEW *secondViewSuperview = view; 142 | while (!closestCommonSuperview && secondViewSuperview) { 143 | MAS_VIEW *firstViewSuperview = self; 144 | while (!closestCommonSuperview && firstViewSuperview) { 145 | if (secondViewSuperview == firstViewSuperview) { 146 | closestCommonSuperview = secondViewSuperview; 147 | } 148 | firstViewSuperview = firstViewSuperview.superview; 149 | } 150 | secondViewSuperview = secondViewSuperview.superview; 151 | } 152 | return closestCommonSuperview; 153 | } 154 | 155 | @end 156 | -------------------------------------------------------------------------------- /Demo/Demo/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 TARGET_OS_IPHONE 33 | 34 | @property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; 35 | @property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; 36 | @property (nonatomic, strong, readonly) MASViewAttribute *topMargin; 37 | @property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; 39 | @property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; 40 | @property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; 41 | @property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; 42 | 43 | #endif 44 | 45 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 46 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 47 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 48 | 49 | @end 50 | 51 | #define MAS_ATTR_FORWARD(attr) \ 52 | - (MASViewAttribute *)attr { \ 53 | return [self mas_##attr]; \ 54 | } 55 | 56 | @implementation MAS_VIEW (MASShorthandAdditions) 57 | 58 | MAS_ATTR_FORWARD(top); 59 | MAS_ATTR_FORWARD(left); 60 | MAS_ATTR_FORWARD(bottom); 61 | MAS_ATTR_FORWARD(right); 62 | MAS_ATTR_FORWARD(leading); 63 | MAS_ATTR_FORWARD(trailing); 64 | MAS_ATTR_FORWARD(width); 65 | MAS_ATTR_FORWARD(height); 66 | MAS_ATTR_FORWARD(centerX); 67 | MAS_ATTR_FORWARD(centerY); 68 | MAS_ATTR_FORWARD(baseline); 69 | 70 | #if TARGET_OS_IPHONE 71 | 72 | MAS_ATTR_FORWARD(leftMargin); 73 | MAS_ATTR_FORWARD(rightMargin); 74 | MAS_ATTR_FORWARD(topMargin); 75 | MAS_ATTR_FORWARD(bottomMargin); 76 | MAS_ATTR_FORWARD(leadingMargin); 77 | MAS_ATTR_FORWARD(trailingMargin); 78 | MAS_ATTR_FORWARD(centerXWithinMargins); 79 | MAS_ATTR_FORWARD(centerYWithinMargins); 80 | 81 | #endif 82 | 83 | - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { 84 | return [self mas_attribute]; 85 | } 86 | 87 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 88 | return [self mas_makeConstraints:block]; 89 | } 90 | 91 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 92 | return [self mas_updateConstraints:block]; 93 | } 94 | 95 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 96 | return [self mas_remakeConstraints:block]; 97 | } 98 | 99 | @end 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /Demo/Demo/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 UILa 8youtGuide and NSLayoutAttribute 19 | */ 20 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide; 22 | 23 | @end 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Demo/Demo/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 | 19 | - (MASViewAttribute *)mas_bottomLayoutGuide { 20 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 21 | } 22 | 23 | @end 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Demo/Demo/UITableView+FDTemplateLayoutCell.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog ) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #import 24 | 25 | @interface UITableView (FDTemplateLayoutCell) 26 | 27 | /// Returns height of cell of type specifed by a reuse identifier and configured 28 | /// by the configuration block. 29 | /// 30 | /// The cell would be layed out on a fixed-width, vertically expanding basis with 31 | /// respect to its dynamic content, using auto layout. Thus, it is imperative that 32 | /// the cell was set up to be self-satisfied, i.e. its content always determines 33 | /// its height given the width is equal to the tableview's. 34 | /// 35 | /// @param identifier A string identifier for retrieving and maintaining template 36 | /// cells with system's "-dequeueReusableCellWithIdentifier:" call. 37 | /// @param configuration An optional block for configuring and providing content 38 | /// to the template cell. The configuration should be minimal for scrolling 39 | /// performance yet sufficient for calculating cell's height. 40 | /// 41 | - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration; 42 | 43 | /// This method does what "-fd_heightForCellWithIdentifier:configuration" does, and 44 | /// calculated height will be cached by its index path, returns a cached height 45 | /// when needed. Therefore lots of extra height calculations could be saved. 46 | /// 47 | /// No need to worry about invalidating cached heights when data source changes, it 48 | /// will be done automatically when you call "-reloadData" or any method that triggers 49 | /// UITableView's reloading. 50 | /// 51 | /// @param indexPath where this cell's height cache belongs. 52 | /// 53 | - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration; 54 | 55 | /// Helps to debug or inspect what is this "FDTemplateLayoutCell" extention doing, 56 | /// turning on to print logs when "creating", "calculating", "precaching" or "hitting cache". 57 | /// 58 | /// Default to "NO", log by "NSLog". 59 | /// 60 | @property (nonatomic, assign) BOOL fd_debugLogEnabled; 61 | 62 | @end 63 | 64 | @interface UITableViewCell (FDTemplateLayoutCell) 65 | 66 | /// Indicate this is a template layout cell for calculation only. 67 | /// You may need this when there are non-UI side effects when configure a cell. 68 | /// Like: 69 | /// - (void)configureCell:(FooCell *)cell atIndexPath:(NSIndexPath *)indexPath { 70 | /// cell.entity = [self entityAtIndexPath:indexPath]; 71 | /// if (!cell.fd_isTemplateLayoutCell) { 72 | /// [self notifySomething]; // non-UI side effects 73 | /// } 74 | /// } 75 | /// 76 | @property (nonatomic, assign) BOOL fd_isTemplateLayoutCell; 77 | 78 | /// Enable to enforce this template layout cell to use "frame layout" rather than "auto layout", 79 | /// and will ask cell's height by calling "-sizeThatFits:", so you must override this method. 80 | /// Note: 81 | /// If no layout constraints have been added to cell's content view, it will automatically 82 | /// switch to "frame layout" mode. Use this property only when you want to manually control 83 | /// this template layout cell's height calculation mode. Default to NO. 84 | /// 85 | @property (nonatomic, assign) BOOL fd_enforceFrameLayout; 86 | 87 | @end 88 | -------------------------------------------------------------------------------- /Demo/Demo/UITableView+FDTemplateLayoutCell.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog ) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #import "UITableView+FDTemplateLayoutCell.h" 24 | #import 25 | 26 | #pragma mark - _FDTemplateLayoutCellHeightCache 27 | 28 | @interface _FDTemplateLayoutCellHeightCache : NSObject 29 | // 2 dimensions array, sections-rows-height 30 | @property (nonatomic, strong) NSMutableArray *sections; 31 | @end 32 | 33 | // Tag a absent height cache value which will be set to a real value. 34 | static CGFloat const _FDTemplateLayoutCellHeightCacheAbsentValue = -1; 35 | 36 | @implementation _FDTemplateLayoutCellHeightCache 37 | 38 | - (void)buildHeightCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths 39 | { 40 | if (indexPaths.count == 0) { 41 | return; 42 | } 43 | 44 | if (!self.sections) { 45 | self.sections = @[].mutableCopy; 46 | } 47 | 48 | // Build every section array or row array which is smaller than given index path. 49 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 50 | for (NSInteger section = 0; section <= indexPath.section; ++section) { 51 | if (section >= self.sections.count) { 52 | self.sections[section] = @[].mutableCopy; 53 | } 54 | } 55 | NSMutableArray *rows = self.sections[indexPath.section]; 56 | for (NSInteger row = 0; row <= indexPath.row; ++row) { 57 | if (row >= rows.count) { 58 | rows[row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue); 59 | } 60 | } 61 | }]; 62 | } 63 | 64 | - (BOOL)hasCachedHeightAtIndexPath:(NSIndexPath *)indexPath 65 | { 66 | [self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]]; 67 | NSNumber *cachedNumber = self.sections[indexPath.section][indexPath.row]; 68 | return ![cachedNumber isEqualToNumber:@(_FDTemplateLayoutCellHeightCacheAbsentValue)]; 69 | } 70 | 71 | - (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath 72 | { 73 | [self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]]; 74 | self.sections[indexPath.section][indexPath.row] = @(height); 75 | } 76 | 77 | - (CGFloat)cachedHeightAtIndexPath:(NSIndexPath *)indexPath 78 | { 79 | [self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]]; 80 | #if CGFLOAT_IS_DOUBLE 81 | return [self.sections[indexPath.section][indexPath.row] doubleValue]; 82 | #else 83 | return [self.sections[indexPath.section][indexPath.row] floatValue]; 84 | #endif 85 | } 86 | 87 | @end 88 | 89 | #pragma mark - UITableView + FDTemplateLayoutCellPrivate 90 | 91 | /// These methods are private for internal use, maybe public some day. 92 | @interface UITableView (FDTemplateLayoutCellPrivate) 93 | 94 | /// Returns a template cell created by reuse identifier, it has to be registered to table view. 95 | /// Lazy getter, and associated to table view. 96 | - (id)fd_templateCellForReuseIdentifier:(NSString *)identifier; 97 | 98 | /// A private height cache data structure. 99 | @property (nonatomic, strong, readonly) _FDTemplateLayoutCellHeightCache *fd_cellHeightCache; 100 | 101 | /// This is a private switch that I don't think caller should concern. 102 | /// Auto turn on when you use "-fd_heightForCellWithIdentifier:cacheByIndexPath:configuration". 103 | @property (nonatomic, assign) BOOL fd_autoCacheInvalidationEnabled; 104 | 105 | /// It helps to improve scroll performance by "pre-cache" height of cells that have not 106 | /// been displayed on screen. These calculation tasks are collected and performed only 107 | /// when "RunLoop" is in "idle" time. 108 | /// 109 | /// Auto turn on when you use "-fd_heightForCellWithIdentifier:cacheByIndexPath:configuration". 110 | @property (nonatomic, assign) BOOL fd_precacheEnabled; 111 | 112 | /// Debug log controlled by "fd_debugLogEnabled". 113 | - (void)fd_debugLog:(NSString *)message; 114 | 115 | @end 116 | 117 | @implementation UITableView (FDTemplateLayoutCellPrivate) 118 | 119 | - (id)fd_templateCellForReuseIdentifier:(NSString *)identifier 120 | { 121 | NSAssert(identifier.length > 0, @"Expect a valid identifier - %@", identifier); 122 | 123 | NSMutableDictionary *templateCellsByIdentifiers = objc_getAssociatedObject(self, _cmd); 124 | if (!templateCellsByIdentifiers) { 125 | templateCellsByIdentifiers = @{}.mutableCopy; 126 | objc_setAssociatedObject(self, _cmd, templateCellsByIdentifiers, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 127 | } 128 | 129 | UITableViewCell *templateCell = templateCellsByIdentifiers[identifier]; 130 | 131 | if (!templateCell) { 132 | templateCell = [self dequeueReusableCellWithIdentifier:identifier]; 133 | NSAssert(templateCell != nil, @"Cell must be registered to table view for identifier - %@", identifier); 134 | templateCell.fd_isTemplateLayoutCell = YES; 135 | templateCell.contentView.translatesAutoresizingMaskIntoConstraints = NO; 136 | templateCellsByIdentifiers[identifier] = templateCell; 137 | [self fd_debugLog:[NSString stringWithFormat:@"layout cell created - %@", identifier]]; 138 | } 139 | 140 | return templateCell; 141 | } 142 | 143 | - (_FDTemplateLayoutCellHeightCache *)fd_cellHeightCache 144 | { 145 | _FDTemplateLayoutCellHeightCache *cache = objc_getAssociatedObject(self, _cmd); 146 | if (!cache) { 147 | cache = [_FDTemplateLayoutCellHeightCache new]; 148 | objc_setAssociatedObject(self, _cmd, cache, OBJC_ASSOCIATION_RETAIN); 149 | } 150 | return cache; 151 | } 152 | 153 | - (BOOL)fd_autoCacheInvalidationEnabled 154 | { 155 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 156 | } 157 | 158 | - (void)setFd_autoCacheInvalidationEnabled:(BOOL)enabled 159 | { 160 | objc_setAssociatedObject(self, @selector(fd_autoCacheInvalidationEnabled), @(enabled), OBJC_ASSOCIATION_RETAIN); 161 | } 162 | 163 | - (BOOL)fd_precacheEnabled 164 | { 165 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 166 | } 167 | 168 | - (void)setFd_precacheEnabled:(BOOL)precacheEnabled 169 | { 170 | objc_setAssociatedObject(self, @selector(fd_precacheEnabled), @(precacheEnabled), OBJC_ASSOCIATION_RETAIN); 171 | } 172 | 173 | - (void)fd_debugLog:(NSString *)message 174 | { 175 | if (!self.fd_debugLogEnabled) { 176 | return; 177 | } 178 | NSLog(@"** FDTemplateLayoutCell ** %@", message); 179 | } 180 | 181 | @end 182 | 183 | #pragma mark - UITableView + FDTemplateLayoutCellPrecache 184 | 185 | @implementation UITableView (FDTemplateLayoutCellPrecache) 186 | 187 | - (void)fd_precacheIfNeeded 188 | { 189 | if (!self.fd_precacheEnabled) { 190 | return; 191 | } 192 | 193 | // Delegate could use "rowHeight" rather than implements this method. 194 | if (![self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) { 195 | return; 196 | } 197 | 198 | CFRunLoopRef runLoop = CFRunLoopGetCurrent(); 199 | 200 | // This is a idle mode of RunLoop, when UIScrollView scrolls, it jumps into "UITrackingRunLoopMode" 201 | // and won't perform any cache task to keep a smooth scroll. 202 | CFStringRef runLoopMode = kCFRunLoopDefaultMode; 203 | 204 | // Collect all index paths to be precached. 205 | NSMutableArray *mutableIndexPathsToBePrecached = self.fd_allIndexPathsToBePrecached.mutableCopy; 206 | 207 | // Setup a observer to get a perfect moment for precaching tasks. 208 | // We use a "kCFRunLoopBeforeWaiting" state to keep RunLoop has done everything and about to sleep 209 | // (mach_msg_trap), when all tasks finish, it will remove itself. 210 | CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler 211 | (kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity _) { 212 | // Remove observer when all precache tasks are done. 213 | if (mutableIndexPathsToBePrecached.count == 0) { 214 | CFRunLoopRemoveObserver(runLoop, observer, runLoopMode); 215 | CFRelease(observer); 216 | return; 217 | } 218 | // Pop first index path record as this RunLoop iteration's task. 219 | NSIndexPath *indexPath = mutableIndexPathsToBePrecached.firstObject; 220 | [mutableIndexPathsToBePrecached removeObject:indexPath]; 221 | 222 | // This method creates a "source 0" task in "idle" mode of RunLoop, and will be 223 | // performed in a future RunLoop iteration only when user is not scrolling. 224 | [self performSelector:@selector(fd_precacheIndexPathIfNeeded:) 225 | onThread:[NSThread mainThread] 226 | withObject:indexPath 227 | waitUntilDone:NO 228 | modes:@[NSDefaultRunLoopMode]]; 229 | }); 230 | 231 | CFRunLoopAddObserver(runLoop, observer, runLoopMode); 232 | } 233 | 234 | - (void)fd_precacheIndexPathIfNeeded:(NSIndexPath *)indexPath 235 | { 236 | // A cached indexPath 237 | if ([self.fd_cellHeightCache hasCachedHeightAtIndexPath:indexPath]) { 238 | return; 239 | } 240 | 241 | // This RunLoop source may have been invalid at this point when data source 242 | // changes during precache's dispatching. 243 | if (indexPath.section >= [self numberOfSections] || 244 | indexPath.row >= [self numberOfRowsInSection:indexPath.section]) { 245 | return; 246 | } 247 | 248 | CGFloat height = [self.delegate tableView:self heightForRowAtIndexPath:indexPath]; 249 | [self.fd_cellHeightCache cacheHeight:height byIndexPath:indexPath]; 250 | [self fd_debugLog:[NSString stringWithFormat: 251 | @"finished precache - [%@:%@] %@", 252 | @(indexPath.section), 253 | @(indexPath.row), 254 | @(height)]]; 255 | } 256 | 257 | - (NSArray *)fd_allIndexPathsToBePrecached 258 | { 259 | NSMutableArray *allIndexPaths = @[].mutableCopy; 260 | for (NSInteger section = 0; section < [self numberOfSections]; ++section) { 261 | for (NSInteger row = 0; row < [self numberOfRowsInSection:section]; ++row) { 262 | NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 263 | if (![self.fd_cellHeightCache hasCachedHeightAtIndexPath:indexPath]) { 264 | [allIndexPaths addObject:indexPath]; 265 | } 266 | } 267 | } 268 | return allIndexPaths.copy; 269 | } 270 | 271 | @end 272 | 273 | #pragma mark - UITableView + FDTemplateLayoutCellAutomaticallyCacheInvalidation 274 | 275 | @implementation UITableView (FDTemplateLayoutCellAutomaticallyCacheInvalidation) 276 | 277 | + (void)load 278 | { 279 | // All methods that trigger height cache's invalidation 280 | SEL selectors[] = { 281 | @selector(reloadData), 282 | @selector(insertSections:withRowAnimation:), 283 | @selector(deleteSections:withRowAnimation:), 284 | @selector(reloadSections:withRowAnimation:), 285 | @selector(moveSection:toSection:), 286 | @selector(insertRowsAtIndexPaths:withRowAnimation:), 287 | @selector(deleteRowsAtIndexPaths:withRowAnimation:), 288 | @selector(reloadRowsAtIndexPaths:withRowAnimation:), 289 | @selector(moveRowAtIndexPath:toIndexPath:) 290 | }; 291 | 292 | for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) { 293 | SEL originalSelector = selectors[index]; 294 | SEL swizzledSelector = NSSelectorFromString([@"fd_" stringByAppendingString:NSStringFromSelector(originalSelector)]); 295 | 296 | Method originalMethod = class_getInstanceMethod(self, originalSelector); 297 | Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); 298 | 299 | method_exchangeImplementations(originalMethod, swizzledMethod); 300 | } 301 | } 302 | 303 | - (void)fd_reloadData 304 | { 305 | if (self.fd_autoCacheInvalidationEnabled) { 306 | [self.fd_cellHeightCache.sections removeAllObjects]; 307 | } 308 | [self fd_reloadData]; // Primary call 309 | [self fd_precacheIfNeeded]; 310 | } 311 | 312 | - (void)fd_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 313 | { 314 | if (self.fd_autoCacheInvalidationEnabled) { 315 | [sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 316 | [self.fd_cellHeightCache.sections insertObject:@[].mutableCopy atIndex:idx]; 317 | }]; 318 | } 319 | [self fd_insertSections:sections withRowAnimation:animation]; // Primary call 320 | [self fd_precacheIfNeeded]; 321 | } 322 | 323 | - (void)fd_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 324 | { 325 | if (self.fd_autoCacheInvalidationEnabled) { 326 | [sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 327 | [self.fd_cellHeightCache.sections removeObjectAtIndex:idx]; 328 | }]; 329 | } 330 | [self fd_deleteSections:sections withRowAnimation:animation]; // Primary call 331 | } 332 | 333 | - (void)fd_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 334 | { 335 | if (self.fd_autoCacheInvalidationEnabled) { 336 | [sections enumerateIndexesUsingBlock: ^(NSUInteger idx, BOOL *stop) { 337 | if (idx < self.fd_cellHeightCache.sections.count) { 338 | NSMutableArray *rows = self.fd_cellHeightCache.sections[idx]; 339 | for (NSInteger row = 0; row < rows.count; ++row) { 340 | rows[row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue); 341 | } 342 | } 343 | }]; 344 | } 345 | [self fd_reloadSections:sections withRowAnimation:animation]; // Primary call 346 | [self fd_precacheIfNeeded]; 347 | } 348 | 349 | - (void)fd_moveSection:(NSInteger)section toSection:(NSInteger)newSection 350 | { 351 | if (self.fd_autoCacheInvalidationEnabled) { 352 | NSInteger sectionCount = self.fd_cellHeightCache.sections.count; 353 | if (section < sectionCount && newSection < sectionCount) { 354 | [self.fd_cellHeightCache.sections exchangeObjectAtIndex:section withObjectAtIndex:newSection]; 355 | } 356 | } 357 | [self fd_moveSection:section toSection:newSection]; // Primary call 358 | } 359 | 360 | - (void)fd_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 361 | { 362 | if (self.fd_autoCacheInvalidationEnabled) { 363 | [self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths]; 364 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 365 | NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section]; 366 | [rows insertObject:@(_FDTemplateLayoutCellHeightCacheAbsentValue) atIndex:indexPath.row]; 367 | }]; 368 | } 369 | [self fd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; // Primary call 370 | [self fd_precacheIfNeeded]; 371 | } 372 | 373 | - (void)fd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 374 | { 375 | if (self.fd_autoCacheInvalidationEnabled) { 376 | [self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths]; 377 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 378 | NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section]; 379 | [rows removeObjectAtIndex:indexPath.row]; 380 | }]; 381 | } 382 | [self fd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; // Primary call 383 | } 384 | 385 | - (void)fd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 386 | { 387 | if (self.fd_autoCacheInvalidationEnabled) { 388 | [self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths]; 389 | [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { 390 | NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section]; 391 | rows[indexPath.row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue); 392 | }]; 393 | } 394 | [self fd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; // Primary call 395 | [self fd_precacheIfNeeded]; 396 | } 397 | 398 | - (void)fd_moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 399 | { 400 | if (self.fd_autoCacheInvalidationEnabled) { 401 | [self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:@[sourceIndexPath, destinationIndexPath]]; 402 | 403 | NSMutableArray *sourceRows = self.fd_cellHeightCache.sections[sourceIndexPath.section]; 404 | NSMutableArray *destinationRows = self.fd_cellHeightCache.sections[destinationIndexPath.section]; 405 | 406 | NSNumber *sourceValue = sourceRows[sourceIndexPath.row]; 407 | NSNumber *destinationValue = destinationRows[destinationIndexPath.row]; 408 | 409 | sourceRows[sourceIndexPath.row] = destinationValue; 410 | destinationRows[destinationIndexPath.row] = sourceValue; 411 | } 412 | [self fd_moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]; // Primary call 413 | } 414 | 415 | @end 416 | 417 | #pragma mark - [Public] UITableView + FDTemplateLayoutCell 418 | 419 | @implementation UITableView (FDTemplateLayoutCell) 420 | 421 | - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration 422 | { 423 | if (!identifier) { 424 | return 0; 425 | } 426 | 427 | // Fetch a cached template cell for `identifier`. 428 | UITableViewCell *cell = [self fd_templateCellForReuseIdentifier:identifier]; 429 | 430 | // Manually calls to ensure consistent behavior with actual cells (that are displayed on screen). 431 | [cell prepareForReuse]; 432 | 433 | // Customize and provide content for our template cell. 434 | if (configuration) { 435 | configuration(cell); 436 | } 437 | 438 | CGFloat contentViewWidth = CGRectGetWidth(self.frame); 439 | 440 | // If a cell has accessory view or system accessory type, its content view's width is smaller 441 | // than cell's by some fixed value. 442 | if (cell.accessoryView) { 443 | contentViewWidth -= 16 + CGRectGetWidth(cell.accessoryView.frame); 444 | } else { 445 | static CGFloat systemAccessoryWidths[] = { 446 | [UITableViewCellAccessoryNone] = 0, 447 | [UITableViewCellAccessoryDisclosureIndicator] = 34, 448 | [UITableViewCellAccessoryDetailDisclosureButton] = 68, 449 | [UITableViewCellAccessoryCheckmark] = 40, 450 | [UITableViewCellAccessoryDetailButton] = 48 451 | }; 452 | contentViewWidth -= systemAccessoryWidths[cell.accessoryType]; 453 | } 454 | 455 | CGSize fittingSize = CGSizeZero; 456 | 457 | // If auto layout enabled, cell's contentView must have some constraints. 458 | BOOL autoLayoutEnabled = cell.contentView.constraints.count > 0 && !cell.fd_enforceFrameLayout; 459 | if (autoLayoutEnabled) { 460 | 461 | // Add a hard width constraint to make dynamic content views (like labels) expand vertically instead 462 | // of growing horizontally, in a flow-layout manner. 463 | NSLayoutConstraint *tempWidthConstraint = 464 | [NSLayoutConstraint constraintWithItem:cell.contentView 465 | attribute:NSLayoutAttributeWidth 466 | relatedBy:NSLayoutRelationEqual 467 | toItem:nil 468 | attribute:NSLayoutAttributeNotAnAttribute 469 | multiplier:1.0 470 | constant:contentViewWidth]; 471 | [cell.contentView addConstraint:tempWidthConstraint]; 472 | // Auto layout engine does its math 473 | fittingSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 474 | [cell.contentView removeConstraint:tempWidthConstraint]; 475 | 476 | } else { 477 | 478 | // If not using auto layout, you have to override "-sizeThatFits:" to provide a fitting size by yourself. 479 | // This is the same method used in iOS8 self-sizing cell's implementation. 480 | // Note: fitting height should not include separator view. 481 | SEL selector = @selector(sizeThatFits:); 482 | BOOL inherited = ![cell isMemberOfClass:UITableViewCell.class]; 483 | BOOL overrided = [cell.class instanceMethodForSelector:selector] != [UITableViewCell instanceMethodForSelector:selector]; 484 | if (inherited && !overrided) { 485 | NSAssert(NO, @"Customized cell must override '-sizeThatFits:' method if not using auto layout."); 486 | } 487 | fittingSize = [cell sizeThatFits:CGSizeMake(contentViewWidth, 0)]; 488 | } 489 | 490 | // Add 1px extra space for separator line if needed, simulating default UITableViewCell. 491 | if (self.separatorStyle != UITableViewCellSeparatorStyleNone) { 492 | fittingSize.height += 1.0 / [UIScreen mainScreen].scale; 493 | } 494 | 495 | if (autoLayoutEnabled) { 496 | [self fd_debugLog:[NSString stringWithFormat:@"calculate using auto layout - %@", @(fittingSize.height)]]; 497 | } else { 498 | [self fd_debugLog:[NSString stringWithFormat:@"calculate using frame layout - %@", @(fittingSize.height)]]; 499 | } 500 | 501 | return fittingSize.height; 502 | } 503 | 504 | - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id))configuration 505 | { 506 | if (!identifier || !indexPath) { 507 | return 0; 508 | } 509 | 510 | // Enable auto cache invalidation if you use this "cacheByIndexPath" API. 511 | if (!self.fd_autoCacheInvalidationEnabled) { 512 | self.fd_autoCacheInvalidationEnabled = YES; 513 | } 514 | 515 | // Enable precache if you use this "cacheByIndexPath" API. 516 | if (!self.fd_precacheEnabled) { 517 | self.fd_precacheEnabled = YES; 518 | // Manually trigger precache only for the first time. 519 | [self fd_precacheIfNeeded]; 520 | } 521 | 522 | // Hit the cache 523 | if ([self.fd_cellHeightCache hasCachedHeightAtIndexPath:indexPath]) { 524 | [self fd_debugLog:[NSString stringWithFormat: 525 | @"hit cache - [%@:%@] %@", 526 | @(indexPath.section), 527 | @(indexPath.row), 528 | @([self.fd_cellHeightCache cachedHeightAtIndexPath:indexPath])]]; 529 | return [self.fd_cellHeightCache cachedHeightAtIndexPath:indexPath]; 530 | } 531 | 532 | // Call basic height calculation method. 533 | CGFloat height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration]; 534 | [self fd_debugLog:[NSString stringWithFormat: 535 | @"cached - [%@:%@] %@", 536 | @(indexPath.section), 537 | @(indexPath.row), 538 | @(height)]]; 539 | 540 | // Cache it 541 | [self.fd_cellHeightCache cacheHeight:height byIndexPath:indexPath]; 542 | 543 | return height; 544 | } 545 | 546 | - (BOOL)fd_debugLogEnabled 547 | { 548 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 549 | } 550 | 551 | - (void)setFd_debugLogEnabled:(BOOL)debugLogEnabled 552 | { 553 | objc_setAssociatedObject(self, @selector(fd_debugLogEnabled), @(debugLogEnabled), OBJC_ASSOCIATION_RETAIN); 554 | } 555 | 556 | @end 557 | 558 | #pragma mark - [Public] UITableViewCell + FDTemplateLayoutCell 559 | 560 | @implementation UITableViewCell (FDTemplateLayoutCell) 561 | 562 | - (BOOL)fd_isTemplateLayoutCell 563 | { 564 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 565 | } 566 | 567 | - (void)setFd_isTemplateLayoutCell:(BOOL)isTemplateLayoutCell 568 | { 569 | objc_setAssociatedObject(self, @selector(fd_isTemplateLayoutCell), @(isTemplateLayoutCell), OBJC_ASSOCIATION_RETAIN); 570 | } 571 | 572 | - (BOOL)fd_enforceFrameLayout 573 | { 574 | return [objc_getAssociatedObject(self, _cmd) boolValue]; 575 | } 576 | 577 | - (void)setFd_enforceFrameLayout:(BOOL)enforceFrameLayout 578 | { 579 | objc_setAssociatedObject(self, @selector(fd_enforceFrameLayout), @(enforceFrameLayout), OBJC_ASSOCIATION_RETAIN); 580 | } 581 | 582 | @end 583 | -------------------------------------------------------------------------------- /Demo/Demo/XQFeedCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // XQFeedCell.h 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import 10 | @class XQFeedModel; 11 | @interface XQFeedCell : UITableViewCell 12 | 13 | @property (strong, nonatomic) XQFeedModel *feed; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /Demo/Demo/XQFeedCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // XQFeedCell.m 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import "XQFeedCell.h" 10 | #import "Masonry.h" 11 | #import "XQFeedModel.h" 12 | @interface XQFeedCell () 13 | 14 | @property (strong, nonatomic) UILabel *titleLabel; 15 | @property (strong, nonatomic) UILabel *contentLabel; 16 | @property (strong, nonatomic) UIImageView *contentImageView; 17 | @property (strong, nonatomic) UILabel *userLabel; 18 | @property (strong, nonatomic) UILabel *timeLabel; 19 | 20 | @end 21 | 22 | @implementation XQFeedCell 23 | 24 | - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 25 | 26 | if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 27 | 28 | [self createView]; 29 | 30 | [self setttingViewAtuoLayout]; 31 | } 32 | 33 | return self; 34 | } 35 | #pragma make 创建子控件 36 | - (void) createView { 37 | 38 | UILabel *titleLabel = [[UILabel alloc] init]; 39 | [self.contentView addSubview:titleLabel]; 40 | self.titleLabel = titleLabel; 41 | 42 | UILabel *contentLabel = [[UILabel alloc] init]; 43 | contentLabel.numberOfLines = 0; 44 | [self.contentView addSubview:contentLabel]; 45 | self.contentLabel = contentLabel; 46 | 47 | UIImageView *contentImageView = [[UIImageView alloc] init]; 48 | contentImageView.contentMode = UIViewContentModeScaleAspectFit; 49 | [self.contentView addSubview:contentImageView]; 50 | self.contentImageView = contentImageView; 51 | 52 | UILabel *userLabel = [[UILabel alloc] init]; 53 | [self.contentView addSubview:userLabel]; 54 | self.userLabel = userLabel; 55 | 56 | UILabel *timeLabel = [[UILabel alloc] init]; 57 | [self.contentView addSubview:timeLabel]; 58 | self.timeLabel = timeLabel; 59 | } 60 | 61 | 62 | #pragma mark - 在此方法内使用 Masonry 设置控件的约束,设置约束不需要在layoutSubviews中设置,只需要在初始化的时候设置 63 | - (void) setttingViewAtuoLayout { 64 | 65 | int magin = 4; 66 | int padding = 10; 67 | 68 | [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { 69 | make.top.and.left.mas_equalTo(self.contentView).offset(padding); // 设置titleLabel上边跟左边与父控件的偏移量 70 | make.right.mas_equalTo(self.contentView.mas_right).offset(-padding); // 设置titleLabel右边与父控件的偏移量 71 | }]; 72 | 73 | [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { 74 | make.left.and.right.equalTo(self.titleLabel); // 设置contentLabel左边和右边对于titleLabel左右对齐 75 | make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(magin); // 设置contentLabel的上边对于titleLabel的下边的偏移量 76 | }]; 77 | 78 | [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) { 79 | make.left.equalTo(self.titleLabel.mas_left); // 设置contentImageView的左边对于titleLabel的左边对齐 80 | make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(magin); // 设置contentImageView的上边对于contentLabel的下面的偏移量 81 | }]; 82 | 83 | [self.userLabel mas_makeConstraints:^(MASConstraintMaker *make) { 84 | make.left.equalTo(self.titleLabel.mas_left); // 设置userLabel的左边对于titleLabel的左边对齐 85 | make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(magin); // 设置userLabel的上边对于contentImageView的下边的偏移量 86 | make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-magin); // 设置userLabel的下边对于父控件的下面的偏移量 87 | }]; 88 | 89 | [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { 90 | make.bottom.and.top.equalTo(self.userLabel); // 设置timeLabel的上边与下边对于userLabel对齐 91 | make.right.equalTo(self.titleLabel.mas_right); // 设置timeLabel的右边对于titleLabel的右边对齐 92 | }]; 93 | 94 | } 95 | 96 | // 如果你是自动布局子控件,就不需要实现此方法,如果是计算子控件frame的话就需要实现此方法 97 | //- (CGSize)sizeThatFits:(CGSize)size { 98 | // 99 | // CGFloat cellHeight = 0; 100 | // 101 | // cellHeight += [self.titleLabel sizeThatFits:size].height; 102 | // cellHeight += [self.contentLabel sizeThatFits:size].height; 103 | // cellHeight += [self.contentImageView sizeThatFits:size].height; 104 | // cellHeight += [self.userLabel sizeThatFits:size].height; 105 | // cellHeight += 40; 106 | // 107 | // return CGSizeMake(size.width, cellHeight); 108 | //} 109 | 110 | /** 111 | * 设置控件属性 112 | */ 113 | - (void)setFeed:(XQFeedModel *)feed { 114 | 115 | _feed = feed; 116 | 117 | self.titleLabel.text = feed.title; 118 | self.contentLabel.text = feed.content; 119 | self.contentImageView.image = feed.imageName.length > 0 ? [UIImage imageNamed:feed.imageName] : nil; 120 | self.userLabel.text = feed.username; 121 | self.timeLabel.text = feed.time; 122 | } 123 | 124 | 125 | @end 126 | -------------------------------------------------------------------------------- /Demo/Demo/XQFeedModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // XQFeedModel.h 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XQFeedModel : NSObject 12 | 13 | @property (copy, nonatomic) NSString *username; 14 | @property (copy, nonatomic) NSString *title; 15 | @property (copy, nonatomic) NSString *content; 16 | @property (copy, nonatomic) NSString *imageName; 17 | @property (copy, nonatomic) NSString *time; 18 | 19 | + (instancetype) feedWithDictionary:(NSDictionary *) dictionary; 20 | 21 | - (instancetype) initWithDictionary:(NSDictionary *) dictionary; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /Demo/Demo/XQFeedModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // XQFeedModel.m 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import "XQFeedModel.h" 10 | 11 | @implementation XQFeedModel 12 | 13 | + (instancetype)feedWithDictionary:(NSDictionary *)dictionary { 14 | 15 | return [[self alloc] initWithDictionary:dictionary]; 16 | } 17 | 18 | 19 | - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 20 | 21 | if (self = [super init]) { 22 | 23 | self.title = dictionary[@"title"]; 24 | self.content = dictionary[@"content"]; 25 | self.username = dictionary[@"username"]; 26 | self.time = dictionary[@"time"]; 27 | self.imageName = dictionary[@"imageName"]; 28 | } 29 | return self; 30 | } 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /Demo/Demo/XQTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // XQTableViewController.h 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface XQTableViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /Demo/Demo/XQTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // XQTableViewController.m 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import "XQTableViewController.h" 10 | #import "XQFeedModel.h" 11 | #import "XQFeedCell.h" 12 | #import "UITableView+FDTemplateLayoutCell.h" 13 | 14 | @interface XQTableViewController () 15 | 16 | /** 17 | * 解析json数据后得到的数据 18 | */ 19 | @property (strong, nonatomic) NSArray *feedsDataFormJSON; 20 | 21 | /** 22 | * 用于给数据源使用的数组 23 | */ 24 | @property (strong, nonatomic) NSMutableArray *feeds; 25 | @end 26 | 27 | @implementation XQTableViewController 28 | 29 | - (void)viewDidLoad { 30 | [super viewDidLoad]; 31 | 32 | self.tableView.delegate = self; 33 | self.tableView.dataSource = self; 34 | 35 | [self loadJSONData:^{ // 加载完josn数据后要做的操作 36 | 37 | self.feeds = @[].mutableCopy; 38 | 39 | [self.feeds addObject:self.feedsDataFormJSON.mutableCopy]; 40 | 41 | // 给一个标识符,告诉tableView要创建哪个类 42 | [self.tableView registerClass:[XQFeedCell class] forCellReuseIdentifier:@"feedCell"]; 43 | 44 | [self.tableView reloadData]; 45 | 46 | }]; 47 | } 48 | 49 | #pragma mark - 加载json数据 50 | - (void) loadJSONData:(void(^)()) then { 51 | 52 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 53 | 54 | NSString *dataFilePath =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"]; 55 | 56 | NSData *data = [NSData dataWithContentsOfFile:dataFilePath]; 57 | 58 | NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error:nil]; 59 | 60 | NSArray *feedArray = dataDictionary[@"feed"]; 61 | 62 | NSMutableArray *feedArrayM = @[].mutableCopy; 63 | 64 | [feedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 65 | 66 | [feedArrayM addObject:[XQFeedModel feedWithDictionary:obj]]; 67 | }]; 68 | 69 | self.feedsDataFormJSON = feedArrayM; 70 | 71 | dispatch_async(dispatch_get_main_queue(), ^{ 72 | 73 | !then ? : then(); 74 | }); 75 | }); 76 | } 77 | 78 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 79 | 80 | return [self.feeds count]; 81 | } 82 | 83 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 84 | 85 | return [self.feeds[section] count]; 86 | } 87 | 88 | 89 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 90 | 91 | XQFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"feedCell"]; 92 | 93 | [self setupModelOfCell:cell atIndexPath:indexPath]; 94 | 95 | return cell; 96 | } 97 | 98 | - (void) setupModelOfCell:(XQFeedCell *) cell atIndexPath:(NSIndexPath *) indexPath { 99 | 100 | // 采用计算frame模式还是自动布局模式,默认为NO,自动布局模式 101 | // cell.fd_enforceFrameLayout = NO; 102 | 103 | cell.feed = self.feeds[indexPath.section][indexPath.row]; 104 | } 105 | 106 | 107 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 108 | 109 | return [self.tableView fd_heightForCellWithIdentifier:@"feedCell" cacheByIndexPath:indexPath configuration:^(XQFeedCell *cell) { 110 | 111 | // 在这个block中,重新cell配置数据源 112 | [self setupModelOfCell:cell atIndexPath:indexPath]; 113 | }]; 114 | } 115 | 116 | @end 117 | -------------------------------------------------------------------------------- /Demo/Demo/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "feed": [ 3 | { 4 | "title": "Hello world", 5 | "content": "This is forkingdog team. Here's our logo?\nGithub: \"forkingdog\"", 6 | "username": "forkingdog", 7 | "time": "2015.04.10", 8 | "imageName": "forkingdog" 9 | }, 10 | { 11 | "title": "Team member - sunnyxx", 12 | "content": "Working at Baidu, Zhidao iOS team, weibo: @我就叫Sunny怎么了", 13 | "username": "sunnyxx", 14 | "time": "2015.04.11", 15 | "imageName": "sunnyxx" 16 | }, 17 | { 18 | "title": "Team member - SinoJerk", 19 | "content": "Zhidao iOS team, Daifu Tang (aka 彪哥)", 20 | "username": "sinojerk", 21 | "time": "2015.04.15", 22 | "imageName": "sinojerk" 23 | }, 24 | { 25 | "title": "Team member - Phil", 26 | "content": "Zhidao iOS team, Jiaqi Guo, Github: philcn", 27 | "username": "phil", 28 | "time": "2015.04.15", 29 | "imageName": "phil" 30 | }, 31 | { 32 | "title": "William Shakespeare", 33 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 34 | "username": "sunnyxx", 35 | "time": "2015.04.12", 36 | "imageName": "breaddoge" 37 | }, 38 | { 39 | "title": "William Shakespeare", 40 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 41 | "username": "sunnyxx", 42 | "time": "2015.04.16", 43 | "imageName": "" 44 | }, 45 | { 46 | "title": "Sark's bad guy (gay)", 47 | "content": "", 48 | "username": "sunnyxx", 49 | "time": "2015.04.16", 50 | "imageName": "sark" 51 | }, 52 | { 53 | "title": "", 54 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 55 | "username": "sunnyxx", 56 | "time": "2015.04.17", 57 | "imageName": "" 58 | }, 59 | { 60 | "title": "William Shakespeare", 61 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 62 | "username": "sunnyxx", 63 | "time": "2015.04.12", 64 | "imageName": "breaddoge" 65 | }, 66 | { 67 | "title": "William Shakespeare", 68 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 69 | "username": "sunnyxx", 70 | "time": "2015.04.16", 71 | "imageName": "" 72 | }, 73 | { 74 | "title": "Sark's bad guy (gay)", 75 | "content": "", 76 | "username": "sunnyxx", 77 | "time": "2015.04.16", 78 | "imageName": "sark" 79 | }, 80 | { 81 | "title": "", 82 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 83 | "username": "sunnyxx", 84 | "time": "2015.04.17", 85 | "imageName": "" 86 | }, 87 | { 88 | "title": "William Shakespeare", 89 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 90 | "username": "sunnyxx", 91 | "time": "2015.04.16", 92 | "imageName": "" 93 | }, 94 | { 95 | "title": "Sark's bad guy (gay)", 96 | "content": "", 97 | "username": "sunnyxx", 98 | "time": "2015.04.16", 99 | "imageName": "sark" 100 | }, 101 | { 102 | "title": "", 103 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 104 | "username": "sunnyxx", 105 | "time": "2015.04.17", 106 | "imageName": "" 107 | }, 108 | { 109 | "title": "William Shakespeare", 110 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 111 | "username": "sunnyxx", 112 | "time": "2015.04.16", 113 | "imageName": "" 114 | }, 115 | { 116 | "title": "Sark's bad guy (gay)", 117 | "content": "", 118 | "username": "sunnyxx", 119 | "time": "2015.04.16", 120 | "imageName": "sark" 121 | }, 122 | { 123 | "title": "", 124 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 125 | "username": "sunnyxx", 126 | "time": "2015.04.17", 127 | "imageName": "" 128 | }, 129 | { 130 | "title": "Hello world", 131 | "content": "This is forkingdog team. Here's our logo?\nGithub: \"forkingdog\"", 132 | "username": "forkingdog", 133 | "time": "2015.04.10", 134 | "imageName": "forkingdog" 135 | }, 136 | { 137 | "title": "Team member - sunnyxx", 138 | "content": "Working at Baidu, Zhidao iOS team, weibo: @我就叫Sunny怎么了", 139 | "username": "sunnyxx", 140 | "time": "2015.04.11", 141 | "imageName": "sunnyxx" 142 | }, 143 | { 144 | "title": "Team member - SinoJerk", 145 | "content": "Zhidao iOS team, Daifu Tang (aka 彪哥)", 146 | "username": "sinojerk", 147 | "time": "2015.04.15", 148 | "imageName": "sinojerk" 149 | }, 150 | { 151 | "title": "Team member - Phil", 152 | "content": "Zhidao iOS team, Jiaqi Guo, Github: philcn", 153 | "username": "phil", 154 | "time": "2015.04.15", 155 | "imageName": "phil" 156 | }, 157 | { 158 | "title": "William Shakespeare", 159 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 160 | "username": "sunnyxx", 161 | "time": "2015.04.12", 162 | "imageName": "breaddoge" 163 | }, 164 | { 165 | "title": "William Shakespeare", 166 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 167 | "username": "sunnyxx", 168 | "time": "2015.04.16", 169 | "imageName": "" 170 | }, 171 | { 172 | "title": "Sark's bad guy (gay)", 173 | "content": "", 174 | "username": "sunnyxx", 175 | "time": "2015.04.16", 176 | "imageName": "sark" 177 | }, 178 | { 179 | "title": "", 180 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 181 | "username": "sunnyxx", 182 | "time": "2015.04.17", 183 | "imageName": "" 184 | }, 185 | { 186 | "title": "William Shakespeare", 187 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 188 | "username": "sunnyxx", 189 | "time": "2015.04.12", 190 | "imageName": "breaddoge" 191 | }, 192 | { 193 | "title": "William Shakespeare", 194 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 195 | "username": "sunnyxx", 196 | "time": "2015.04.16", 197 | "imageName": "" 198 | }, 199 | { 200 | "title": "Sark's bad guy (gay)", 201 | "content": "", 202 | "username": "sunnyxx", 203 | "time": "2015.04.16", 204 | "imageName": "sark" 205 | }, 206 | { 207 | "title": "", 208 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 209 | "username": "sunnyxx", 210 | "time": "2015.04.17", 211 | "imageName": "" 212 | }, 213 | { 214 | "title": "William Shakespeare", 215 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 216 | "username": "sunnyxx", 217 | "time": "2015.04.16", 218 | "imageName": "" 219 | }, 220 | { 221 | "title": "Sark's bad guy (gay)", 222 | "content": "", 223 | "username": "sunnyxx", 224 | "time": "2015.04.16", 225 | "imageName": "sark" 226 | }, 227 | { 228 | "title": "", 229 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 230 | "username": "sunnyxx", 231 | "time": "2015.04.17", 232 | "imageName": "" 233 | }, 234 | { 235 | "title": "William Shakespeare", 236 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 237 | "username": "sunnyxx", 238 | "time": "2015.04.16", 239 | "imageName": "" 240 | }, 241 | { 242 | "title": "Sark's bad guy (gay)", 243 | "content": "", 244 | "username": "sunnyxx", 245 | "time": "2015.04.16", 246 | "imageName": "sark" 247 | }, 248 | { 249 | "title": "", 250 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 251 | "username": "sunnyxx", 252 | "time": "2015.04.17", 253 | "imageName": "" 254 | },{ 255 | "title": "Hello world", 256 | "content": "This is forkingdog team. Here's our logo?\nGithub: \"forkingdog\"", 257 | "username": "forkingdog", 258 | "time": "2015.04.10", 259 | "imageName": "forkingdog" 260 | }, 261 | { 262 | "title": "Team member - sunnyxx", 263 | "content": "Working at Baidu, Zhidao iOS team, weibo: @我就叫Sunny怎么了", 264 | "username": "sunnyxx", 265 | "time": "2015.04.11", 266 | "imageName": "sunnyxx" 267 | }, 268 | { 269 | "title": "Team member - SinoJerk", 270 | "content": "Zhidao iOS team, Daifu Tang (aka 彪哥)", 271 | "username": "sinojerk", 272 | "time": "2015.04.15", 273 | "imageName": "sinojerk" 274 | }, 275 | { 276 | "title": "Team member - Phil", 277 | "content": "Zhidao iOS team, Jiaqi Guo, Github: philcn", 278 | "username": "phil", 279 | "time": "2015.04.15", 280 | "imageName": "phil" 281 | }, 282 | { 283 | "title": "William Shakespeare", 284 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 285 | "username": "sunnyxx", 286 | "time": "2015.04.12", 287 | "imageName": "breaddoge" 288 | }, 289 | { 290 | "title": "William Shakespeare", 291 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 292 | "username": "sunnyxx", 293 | "time": "2015.04.16", 294 | "imageName": "" 295 | }, 296 | { 297 | "title": "Sark's bad guy (gay)", 298 | "content": "", 299 | "username": "sunnyxx", 300 | "time": "2015.04.16", 301 | "imageName": "sark" 302 | }, 303 | { 304 | "title": "", 305 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 306 | "username": "sunnyxx", 307 | "time": "2015.04.17", 308 | "imageName": "" 309 | }, 310 | { 311 | "title": "William Shakespeare", 312 | "content": "Good name in man and woman, dear my lord, is the immediate jewel of their souls: Who steals my purse steals trash; ’tis something, nothing. (Othello 3.3) ", 313 | "username": "sunnyxx", 314 | "time": "2015.04.12", 315 | "imageName": "breaddoge" 316 | }, 317 | { 318 | "title": "William Shakespeare", 319 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 320 | "username": "sunnyxx", 321 | "time": "2015.04.16", 322 | "imageName": "" 323 | }, 324 | { 325 | "title": "Sark's bad guy (gay)", 326 | "content": "", 327 | "username": "sunnyxx", 328 | "time": "2015.04.16", 329 | "imageName": "sark" 330 | }, 331 | { 332 | "title": "", 333 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 334 | "username": "sunnyxx", 335 | "time": "2015.04.17", 336 | "imageName": "" 337 | }, 338 | { 339 | "title": "William Shakespeare", 340 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 341 | "username": "sunnyxx", 342 | "time": "2015.04.16", 343 | "imageName": "" 344 | }, 345 | { 346 | "title": "Sark's bad guy (gay)", 347 | "content": "", 348 | "username": "sunnyxx", 349 | "time": "2015.04.16", 350 | "imageName": "sark" 351 | }, 352 | { 353 | "title": "", 354 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 355 | "username": "sunnyxx", 356 | "time": "2015.04.17", 357 | "imageName": "" 358 | }, 359 | { 360 | "title": "William Shakespeare", 361 | "content": "To be, or not to be —that is the question, Whether'tis nobler in the mind to suffer. The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die —to sleep", 362 | "username": "sunnyxx", 363 | "time": "2015.04.16", 364 | "imageName": "" 365 | }, 366 | { 367 | "title": "Sark's bad guy (gay)", 368 | "content": "", 369 | "username": "sunnyxx", 370 | "time": "2015.04.16", 371 | "imageName": "sark" 372 | }, 373 | { 374 | "title": "", 375 | "content": "Things base and vile, holding no quantity, love can transpose to from and dignity: love looks not with the eyes, but with mind. (A Midsummer Night’s Dream 1.1)", 376 | "username": "sunnyxx", 377 | "time": "2015.04.17", 378 | "imageName": "" 379 | } 380 | ] 381 | } 382 | -------------------------------------------------------------------------------- /Demo/Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Demo 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. 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 | -------------------------------------------------------------------------------- /Demo/Demo/横屏.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/横屏.gif -------------------------------------------------------------------------------- /Demo/Demo/竖屏.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XQBoy/MasonryExercise/bc7926274aee7f4496719ac0259a6829c2bbb97d/Demo/Demo/竖屏.gif -------------------------------------------------------------------------------- /Demo/DemoTests/DemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoTests.m 3 | // DemoTests 4 | // 5 | // Created by 格式化油条 on 15/7/7. 6 | // Copyright (c) 2015年 格式化油条. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface DemoTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation DemoTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /Demo/DemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | cn.-----.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Demo/README.md: -------------------------------------------------------------------------------- 1 | ##Demo介绍 2 | 3 | 本内容为本人学习使用 Masonry 的一个例子。 4 | 5 | Masonry是一个轻量级的自动布局框架。 6 | github:[Masonry](https://github.com/SnapKit/Masonry) 7 | 8 | 其中使用在计算tableView的cell的高度的时候,也使用到了阳神的优化cell高度的一个工具类:UITableView+FDTemplateLayoutCell 9 | github:[UITableView-FDTemplateLayoutCell](https://github.com/forkingdog/UITableView-FDTemplateLayoutCell) 10 | 11 | 此Demo是根据阳神的例子参考来做的。只是阳神的是storyboard,而我用的是 Masonry。 12 | 看着阳神的代码,让我学习了好多... 13 | 14 | 此文记录本人学习的内容。 15 | 16 | ##gif演示 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MasonryExercise 2 | ##Demo介绍 3 | 4 | 本内容为本人学习使用 Masonry 的一个例子。 5 | 6 | Masonry是一个轻量级的自动布局框架。 7 | github:[Masonry](https://github.com/SnapKit/Masonry) 8 | 9 | 其中使用在计算tableView的cell的高度的时候,也使用到了阳神的优化cell高度的一个工具类:UITableView+FDTemplateLayoutCell 10 | github:[UITableView-FDTemplateLayoutCell](https://github.com/forkingdog/UITableView-FDTemplateLayoutCell) 11 | 12 | 此Demo是根据阳神的例子参考来做的。只是阳神的是storyboard,而我用的是 Masonry。 13 | 看着阳神的代码,让我学习了好多... 14 | 15 | 此文记录本人学习的内容。 16 | 17 | ##gif演示 18 | 横屏: 19 | 20 | ![Demo Demo](https://github.com/XQBoy/MasonryExercise/blob/master/Demo/Demo/%E6%A8%AA%E5%B1%8F.gif) 21 | 22 | 竖屏: 23 | 24 | ![Demo Demo](https://github.com/XQBoy/MasonryExercise/blob/master/Demo/Demo/%E7%AB%96%E5%B1%8F.gif) 25 | --------------------------------------------------------------------------------