diff --git a/Sources/Stevia/Stevia+Percentage.swift b/Sources/Stevia/Stevia+Percentage.swift index fa0d153..907280a 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. diff --git a/Sources/Stevia/Stevia+Size.swift b/Sources/Stevia/Stevia+Size.swift index 9746acb..3f0d52e 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. diff --git a/Tests/SteviaTests/SizeTests.swift b/Tests/SteviaTests/SizeTests.swift index b468f33..6d6103e 100644 --- a/Tests/SteviaTests/SizeTests.swift +++ b/Tests/SteviaTests/SizeTests.swift @@ -24,6 +24,46 @@ import Stevia } } + @Test + func testAspectRatioDouble() { + v.width(150).aspectRatio(Double(3.0/2.0)) + ctrler.view.layoutIfNeeded() + #expect(v.frame.origin.y == 0) + #expect(v.frame.origin.x == 0) + #expect(v.frame.width == 150) + #expect(v.frame.height == 100) + } + + @Test + func testAspectRatioCGFloat() { + v.width(150).aspectRatio(CGFloat(3.0/2.0)) + ctrler.view.layoutIfNeeded() + #expect(v.frame.origin.y == 0) + #expect(v.frame.origin.x == 0) + #expect(v.frame.width == 150) + #expect(v.frame.height == 100) + } + + @Test + func testAspectRatioInt() { + v.width(150).aspectRatio(Int(3)) + ctrler.view.layoutIfNeeded() + #expect(v.frame.origin.y == 0) + #expect(v.frame.origin.x == 0) + #expect(v.frame.width == 150) + #expect(v.frame.height == 50) + } + + @Test + func testAspectRatioPercentage() { + v.width(150).aspectRatio(150%) + ctrler.view.layoutIfNeeded() + #expect(v.frame.origin.y == 0) + #expect(v.frame.origin.x == 0) + #expect(v.frame.width == 150) + #expect(v.frame.height == 100) + } + @Test func testSizeDouble() { v.size(Double(57))