Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding aspect ratio functions #174

Merged
merged 5 commits into from
Nov 12, 2024
Merged
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
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
Loading