Skip to content

Commit 74ea064

Browse files
authored
Merge pull request #174 from jezreelbarbosa/master
Adding aspect ratio functions
2 parents 1c2eaef + 04b1d3c commit 74ea064

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

Sources/Stevia/Stevia+Percentage.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ public postfix func % (v: Int) -> SteviaPercentage {
2727
}
2828

2929
public extension UIView {
30+
31+
/**
32+
Adds an Autolayout constraint to provide the aspect ratio for the view.
33+
34+
```
35+
image.aspectRatio(3/2)
36+
image.aspectRatio(150%)
37+
38+
// is equivalent to
39+
40+
image.Width == image.Height * 1.5
41+
image.Width == 150 % image.Height
42+
```
43+
44+
- Returns: Itself, enabling chaining,
45+
46+
*/
47+
@discardableResult
48+
func aspectRatio(_ p: SteviaPercentage) -> Self {
49+
Width == p.value % Height
50+
return self
51+
}
3052

3153
/**
3254
Adds an Autolayout constraint for sizing the view.

Sources/Stevia/Stevia+Size.swift

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,73 @@
1010
import UIKit
1111

1212
public extension UIView {
13-
13+
14+
/**
15+
Adds an Autolayout constraint to provide the aspect ratio for the view.
16+
17+
```
18+
image.aspectRatio(3.0/2.0)
19+
image.aspectRatio(150%)
20+
21+
// is equivalent to
22+
23+
image.Width == image.Height * 1.5
24+
image.Width == 150 % image.Height
25+
```
26+
27+
- Returns: Itself, enabling chaining,
28+
29+
*/
30+
@discardableResult
31+
func aspectRatio(_ ratio: Double = 1) -> Self {
32+
Width == Height * ratio
33+
return self
34+
}
35+
36+
/**
37+
Adds an Autolayout constraint to provide the aspect ratio for the view.
38+
39+
```
40+
image.aspectRatio(3.0/2.0)
41+
image.aspectRatio(150%)
42+
43+
// is equivalent to
44+
45+
image.Width == image.Height * 1.5
46+
image.Width == 150 % image.Height
47+
```
48+
49+
- Returns: Itself, enabling chaining,
50+
51+
*/
52+
@discardableResult
53+
func aspectRatio(_ ratio: CGFloat = 1) -> Self {
54+
aspectRatio(Double(ratio))
55+
return self
56+
}
57+
58+
/**
59+
Adds an Autolayout constraint to provide the aspect ratio for the view.
60+
61+
```
62+
image.aspectRatio(3.0/2.0)
63+
image.aspectRatio(150%)
64+
65+
// is equivalent to
66+
67+
image.Width == image.Height * 1.5
68+
image.Width == 150 % image.Height
69+
```
70+
71+
- Returns: Itself, enabling chaining,
72+
73+
*/
74+
@discardableResult
75+
func aspectRatio(_ ratio: Int = 1) -> Self {
76+
aspectRatio(Double(ratio))
77+
return self
78+
}
79+
1480
/**
1581
Adds an Autolayout constraint for sizing the view.
1682

Tests/SteviaTests/SizeTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@ import Stevia
2424
}
2525
}
2626

27+
@Test
28+
func testAspectRatioDouble() {
29+
v.width(150).aspectRatio(Double(3.0/2.0))
30+
ctrler.view.layoutIfNeeded()
31+
#expect(v.frame.origin.y == 0)
32+
#expect(v.frame.origin.x == 0)
33+
#expect(v.frame.width == 150)
34+
#expect(v.frame.height == 100)
35+
}
36+
37+
@Test
38+
func testAspectRatioCGFloat() {
39+
v.width(150).aspectRatio(CGFloat(3.0/2.0))
40+
ctrler.view.layoutIfNeeded()
41+
#expect(v.frame.origin.y == 0)
42+
#expect(v.frame.origin.x == 0)
43+
#expect(v.frame.width == 150)
44+
#expect(v.frame.height == 100)
45+
}
46+
47+
@Test
48+
func testAspectRatioInt() {
49+
v.width(150).aspectRatio(Int(3))
50+
ctrler.view.layoutIfNeeded()
51+
#expect(v.frame.origin.y == 0)
52+
#expect(v.frame.origin.x == 0)
53+
#expect(v.frame.width == 150)
54+
#expect(v.frame.height == 50)
55+
}
56+
57+
@Test
58+
func testAspectRatioPercentage() {
59+
v.width(150).aspectRatio(150%)
60+
ctrler.view.layoutIfNeeded()
61+
#expect(v.frame.origin.y == 0)
62+
#expect(v.frame.origin.x == 0)
63+
#expect(v.frame.width == 150)
64+
#expect(v.frame.height == 100)
65+
}
66+
2767
@Test
2868
func testSizeDouble() {
2969
v.size(Double(57))

0 commit comments

Comments
 (0)