diff --git a/Neon.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Neon.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Neon.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/NeonUnitTests/NeonUnitTests.swift b/NeonUnitTests/NeonUnitTests.swift index 1dec207..f2d690e 100644 --- a/NeonUnitTests/NeonUnitTests.swift +++ b/NeonUnitTests/NeonUnitTests.swift @@ -10,7 +10,7 @@ import XCTest @testable import Neon class NeonTests: XCTestCase { - let testSuperview : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000)) + let testSuperview : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000)) let testAnchorView : UIView = UIView() let testAnchorView2 : UIView = UIView() let testSiblingView : UIView = UIView() @@ -30,7 +30,7 @@ class NeonTests: XCTestCase { testAnchorView.frame = CGRect.zero testAnchorView2.frame = CGRect.zero - testSiblingView.frame = CGRect(x: 500, y: 500, width: 100, height: 100) + testSiblingView.frame = CGRect(x: 500, y: 500, width: 100, height: 100) testSiblingView2.frame = CGRect.zero testSiblingView3.frame = CGRect.zero testSiblingView4.frame = CGRect.zero @@ -47,6 +47,30 @@ class NeonTests: XCTestCase { super.tearDown() } + func testAutoHeightWithRestrictedWidth() { + let label = UILabel() + label.text = String(repeating: "some text ", count: 100) + label.numberOfLines = 0 + + testSuperview.addSubview(label) + label.anchorInCorner(.topLeft, xPad: 0, yPad: 0, width: 100, height: AutoHeight) + + let targetLabelHeight = label.sizeThatFits(CGSize(width: 100, height: 0)).height + XCTAssertEqual(label.frame, CGRect(x: 0, y: 0, width: 100, height: targetLabelHeight)) + } + + func testAutoWidthHeight() { + let label = UILabel() + label.text = "some text" + label.numberOfLines = 0 + + testSuperview.addSubview(label) + label.anchorInCorner(.topLeft, xPad: 0, yPad: 0, width: AutoWidth, height: AutoHeight) + + let targetLabelSize = label.sizeThatFits(.zero) + XCTAssertEqual(label.frame, CGRect(x: 0, y: 0, width: targetLabelSize.width, height: targetLabelSize.height)) + } + func testFrameUtils() { testAnchorView.frame = CGRect.zero XCTAssertEqual(testAnchorView.x, 0) @@ -58,7 +82,7 @@ class NeonTests: XCTestCase { XCTAssertEqual(testAnchorView.width, 0) XCTAssertEqual(testAnchorView.height, 0) - testAnchorView.frame = CGRect(x: 10, y: 20, width: 30, height: 40) + testAnchorView.frame = CGRect(x: 10, y: 20, width: 30, height: 40) XCTAssertEqual(testAnchorView.x, 10) XCTAssertEqual(testAnchorView.xMid, 25) XCTAssertEqual(testAnchorView.xMax, 40) @@ -83,7 +107,7 @@ class NeonTests: XCTestCase { XCTAssert(testAnchorView.frame == testSuperview.frame) testAnchorView.fillSuperview(left: 10, right: 30, top: 55, bottom: 110) - XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 55, width: 960, height: 835)) + XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 55, width: 960, height: 835)) } func testAnchorInCenter() { @@ -94,10 +118,10 @@ class NeonTests: XCTestCase { testSuperview.addSubview(testAnchorView) testAnchorView.anchorInCenter(width: 0, height: 0) - XCTAssert(testAnchorView.frame == CGRect(x: 500, y: 500, width: 0, height: 0)) + XCTAssert(testAnchorView.frame == CGRect(x: 500, y: 500, width: 0, height: 0)) testAnchorView.anchorInCenter(width: 20, height: 30) - XCTAssert(testAnchorView.frame == CGRect(x: 490, y: 485, width: 20, height: 30)) + XCTAssert(testAnchorView.frame == CGRect(x: 490, y: 485, width: 20, height: 30)) } func testAnchorInCorner() { @@ -111,16 +135,16 @@ class NeonTests: XCTestCase { XCTAssert(testAnchorView.frame == CGRect.zero) testAnchorView.anchorInCorner(.topLeft, xPad: 10, yPad: 20, width: 30, height: 40) - XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 20, width: 30, height: 40)) + XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 20, width: 30, height: 40)) testAnchorView.anchorInCorner(.topRight, xPad: 10, yPad: 20, width: 30, height: 40) - XCTAssert(testAnchorView.frame == CGRect(x: 960, y: 20, width: 30, height: 40)) + XCTAssert(testAnchorView.frame == CGRect(x: 960, y: 20, width: 30, height: 40)) testAnchorView.anchorInCorner(.bottomLeft, xPad: 10, yPad: 940, width: 30, height: 40) - XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 20, width: 30, height: 40)) + XCTAssert(testAnchorView.frame == CGRect(x: 10, y: 20, width: 30, height: 40)) testAnchorView.anchorInCorner(.bottomRight, xPad: 10, yPad: 20, width: 30, height: 40) - XCTAssert(testAnchorView.frame == CGRect(x: 960, y: 940, width: 30, height: 40)) + XCTAssert(testAnchorView.frame == CGRect(x: 960, y: 940, width: 30, height: 40)) } func testAnchorToEdge() { diff --git a/Source/NeonAlignable.swift b/Source/NeonAlignable.swift index 52dc075..94feb88 100644 --- a/Source/NeonAlignable.swift +++ b/Source/NeonAlignable.swift @@ -93,7 +93,7 @@ public extension Alignable { yOrigin = sibling.y - padding - height } - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -193,11 +193,7 @@ public extension Alignable { width = superviewWidth - (2 * padding) } - if width < 0.0 { - width = 0.0 - } - - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -292,15 +288,11 @@ public extension Alignable { height = sibling.y - (2 * padding) } - if height < 0.0 { - height = 0.0 - } - - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) - if height == AutoHeight { + if width == AutoWidth { self.setDimensionAutomatically() - self.alignAndFillHeight(align: align, relativeTo: sibling, padding: padding, width: self.height, offset: offset) + self.alignAndFillHeight(align: align, relativeTo: sibling, padding: padding, width: self.width, offset: offset) } } @@ -403,15 +395,7 @@ public extension Alignable { height = superviewHeight - (superviewHeight - sibling.y) - (2 * padding) } - if width < 0.0 { - width = 0.0 - } - - if height < 0.0 { - height = 0.0 - } - - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) } @@ -474,11 +458,7 @@ public extension Alignable { fatalError("[NEON] Invalid Align specified for alignBetweenHorizonal().") } - if width < 0.0 { - width = 0.0 - } - - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -546,11 +526,7 @@ public extension Alignable { fatalError("[NEON] Invalid Align specified for alignBetweenVertical().") } - if height < 0 { - height = 0 - } - - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if width == AutoWidth { self.setDimensionAutomatically() diff --git a/Source/NeonAnchorable.swift b/Source/NeonAnchorable.swift index a6d232c..c182fdc 100644 --- a/Source/NeonAnchorable.swift +++ b/Source/NeonAnchorable.swift @@ -49,7 +49,7 @@ public extension Anchorable { let xOrigin : CGFloat = (superFrame.width / 2.0) - (width / 2.0) let yOrigin : CGFloat = (superFrame.height / 2.0) - (height / 2.0) - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -100,7 +100,7 @@ public extension Anchorable { yOrigin = superFrame.height - height - yPad } - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -153,7 +153,7 @@ public extension Anchorable { yOrigin = (superFrame.height / 2.0) - (height / 2.0) } - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight { self.setDimensionAutomatically() @@ -224,7 +224,7 @@ public extension Anchorable { height = superFrame.height - (2 * yPad) } - frame = CGRect(x: xOrigin, y: yOrigin, width: width, height: height) + frame = CGRect(x: xOrigin, y: yOrigin, width: max(width, 0), height: max(height, 0)) if height == AutoHeight && autoSize { self.setDimensionAutomatically()