From 2a935e2cb50663c00ef1cce2070ec11fada9e7c3 Mon Sep 17 00:00:00 2001 From: Jezreel Barbosa Date: Fri, 1 Nov 2024 20:31:41 -0400 Subject: [PATCH 1/4] Implemented aspectRatio function with SteviaPercentage --- Sources/Stevia/Stevia+Percentage.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Sources/Stevia/Stevia+Percentage.swift b/Sources/Stevia/Stevia+Percentage.swift index fa0d153e..907280a9 100644 --- a/Sources/Stevia/Stevia+Percentage.swift +++ b/Sources/Stevia/Stevia+Percentage.swift @@ -27,6 +27,28 @@ public postfix func % (v: Int) -> SteviaPercentage { } public extension UIView { + + /** + Adds an Autolayout constraint to provide the aspect ratio for the view. + + ``` + image.aspectRatio(3/2) + image.aspectRatio(150%) + + // is equivalent to + + image.Width == image.Height * 1.5 + image.Width == 150 % image.Height + ``` + + - Returns: Itself, enabling chaining, + + */ + @discardableResult + func aspectRatio(_ p: SteviaPercentage) -> Self { + Width == p.value % Height + return self + } /** Adds an Autolayout constraint for sizing the view. From 71591609977ad27e24d52b21bd58d67ffebc25c8 Mon Sep 17 00:00:00 2001 From: Jezreel Barbosa Date: Fri, 1 Nov 2024 20:33:03 -0400 Subject: [PATCH 2/4] Implemented aspectRatio function with Double, CGFloat and Int types --- Sources/Stevia/Stevia+Size.swift | 68 +++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/Sources/Stevia/Stevia+Size.swift b/Sources/Stevia/Stevia+Size.swift index 24fd2d55..7c080e6c 100644 --- a/Sources/Stevia/Stevia+Size.swift +++ b/Sources/Stevia/Stevia+Size.swift @@ -10,7 +10,73 @@ import UIKit public extension UIView { - + + /** + Adds an Autolayout constraint to provide the aspect ratio for the view. + + ``` + image.aspectRatio(3.0/2.0) + image.aspectRatio(150%) + + // is equivalent to + + image.Width == image.Height * 1.5 + image.Width == 150 % image.Height + ``` + + - Returns: Itself, enabling chaining, + + */ + @discardableResult + func aspectRatio(_ ratio: Double = 1) -> Self { + Width == Height * ratio + return self + } + + /** + Adds an Autolayout constraint to provide the aspect ratio for the view. + + ``` + image.aspectRatio(3.0/2.0) + image.aspectRatio(150%) + + // is equivalent to + + image.Width == image.Height * 1.5 + image.Width == 150 % image.Height + ``` + + - Returns: Itself, enabling chaining, + + */ + @discardableResult + func aspectRatio(_ ratio: CGFloat = 1) -> Self { + aspectRatio(Double(ratio)) + return self + } + + /** + Adds an Autolayout constraint to provide the aspect ratio for the view. + + ``` + image.aspectRatio(3.0/2.0) + image.aspectRatio(150%) + + // is equivalent to + + image.Width == image.Height * 1.5 + image.Width == 150 % image.Height + ``` + + - Returns: Itself, enabling chaining, + + */ + @discardableResult + func aspectRatio(_ ratio: Int = 1) -> Self { + aspectRatio(Double(ratio)) + return self + } + /** Adds an Autolayout constraint for sizing the view. From c1bbae28febe65e1083e1179a0f4c64a0a243a40 Mon Sep 17 00:00:00 2001 From: Jezreel Barbosa Date: Fri, 1 Nov 2024 20:33:50 -0400 Subject: [PATCH 3/4] Implemented tests for AspectRatio --- Tests/SteviaTests/SizeTests.swift | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Tests/SteviaTests/SizeTests.swift b/Tests/SteviaTests/SizeTests.swift index b1b6fef1..acf1b049 100644 --- a/Tests/SteviaTests/SizeTests.swift +++ b/Tests/SteviaTests/SizeTests.swift @@ -29,6 +29,42 @@ class SizeTests: XCTestCase { override func tearDown() { super.tearDown() } + + func testAspectRatioDouble() { + v.width(150).aspectRatio(Double(3.0/2.0)) + ctrler.view.layoutIfNeeded() + XCTAssertEqual(v.frame.origin.y, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.origin.x, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.width, 150, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.height, 100, accuracy: CGFloat(Float.ulpOfOne)) + } + + func testAspectRatioCGFloat() { + v.width(150).aspectRatio(CGFloat(3.0/2.0)) + ctrler.view.layoutIfNeeded() + XCTAssertEqual(v.frame.origin.y, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.origin.x, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.width, 150, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.height, 100, accuracy: CGFloat(Float.ulpOfOne)) + } + + func testAspectRatioInt() { + v.width(150).aspectRatio(Int(3)) + ctrler.view.layoutIfNeeded() + XCTAssertEqual(v.frame.origin.y, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.origin.x, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.width, 150, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.height, 50, accuracy: CGFloat(Float.ulpOfOne)) + } + + func testAspectRatioPercentage() { + v.width(150).aspectRatio(150%) + ctrler.view.layoutIfNeeded() + XCTAssertEqual(v.frame.origin.y, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.origin.x, 0, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.width, 150, accuracy: CGFloat(Float.ulpOfOne)) + XCTAssertEqual(v.frame.height, 100, accuracy: CGFloat(Float.ulpOfOne)) + } func testSizeDouble() { v.size(Double(57)) From 04b1d3cc47782072d52abe3f00702d9259a3390d Mon Sep 17 00:00:00 2001 From: Architect <38290412+jezreelbarbosa@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:45:05 -0400 Subject: [PATCH 4/4] Update SizeTests.swift --- Tests/SteviaTests/SizeTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SteviaTests/SizeTests.swift b/Tests/SteviaTests/SizeTests.swift index fe83fccc..6d6103ee 100644 --- a/Tests/SteviaTests/SizeTests.swift +++ b/Tests/SteviaTests/SizeTests.swift @@ -51,7 +51,7 @@ import Stevia #expect(v.frame.origin.y == 0) #expect(v.frame.origin.x == 0) #expect(v.frame.width == 150) - #expect(v.frame.height == 100) + #expect(v.frame.height == 50) } @Test