Skip to content

Commit

Permalink
Merge pull request #174 from jezreelbarbosa/master
Browse files Browse the repository at this point in the history
Adding aspect ratio functions
  • Loading branch information
s4cha authored Nov 12, 2024
2 parents 1c2eaef + 04b1d3c commit 74ea064
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Sources/Stevia/Stevia+Percentage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 67 additions & 1 deletion Sources/Stevia/Stevia+Size.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions Tests/SteviaTests/SizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 74ea064

Please sign in to comment.