Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
44 changes: 34 additions & 10 deletions NeonUnitTests/NeonUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
40 changes: 8 additions & 32 deletions Source/NeonAlignable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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))
}


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions Source/NeonAnchorable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down