Skip to content

Commit f176543

Browse files
author
Luc Dion
authored
Merge pull request #32 from mirego/add_more_unit_tests_and_doc
Add more unit tests related to sizeToFit() and margins + Doc update
2 parents 43abec3 + 72649a3 commit f176543

File tree

8 files changed

+263
-64
lines changed

8 files changed

+263
-64
lines changed

PinLayout.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "PinLayout"
19-
s.version = "1.0.5"
19+
s.version = "1.0.6"
2020
s.summary = "Swift manual views layouting without auto layout, no magic, pure code, full control. Concise syntax, readable & chainable."
2121

2222
# This description is used to generate tags and improve search results.

PinLayout/Helpers/Coordinates.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Coordinates {
7272
return CGPoint(x: view.frame.minX + view.frame.width, y: view.frame.minY + view.frame.height)
7373
}
7474

75-
static var displayScale: CGFloat = UIScreen.main.scale
75+
fileprivate static var displayScale: CGFloat = UIScreen.main.scale
7676

7777
static func adjustRectToDisplayScale(_ rect: CGRect) -> CGRect {
7878
return CGRect(x: roundFloatToDisplayScale(rect.origin.x),
@@ -89,3 +89,7 @@ class Coordinates {
8989
return CGFloat(ceilf(Float(pointValue * displayScale))) / displayScale
9090
}
9191
}
92+
93+
public func setUnitTest(displayScale: CGFloat) {
94+
Coordinates.displayScale = displayScale
95+
}

PinLayout/PinLayoutImpl.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,7 @@ extension PinLayoutImpl {
10871087
let marginToDistribute = newWidth! - sizeThatFits.width
10881088

10891089
// Distribute the width change to Margins
1090-
if let _ = _left, let _ = _right {
1091-
marginLeft = (marginLeft ?? 0) + (marginToDistribute / 2)
1092-
marginRight = (marginRight ?? 0) + (marginToDistribute / 2)
1093-
} else if let _ = _left {
1090+
if let _ = _left {
10941091
marginRight = (marginRight ?? 0) + marginToDistribute
10951092
} else if let _ = _right {
10961093
marginLeft = (marginLeft ?? 0) + marginToDistribute
@@ -1101,10 +1098,7 @@ extension PinLayoutImpl {
11011098
let marginToDistribute = newHeight! - sizeThatFits.height
11021099

11031100
// Distribute the height change to Margins
1104-
if let _ = _top, let _ = _bottom {
1105-
marginTop = (marginTop ?? 0) + (marginToDistribute / 2)
1106-
marginBottom = (marginBottom ?? 0) + (marginToDistribute / 2)
1107-
} else if let _ = _top {
1101+
if let _ = _top {
11081102
marginBottom = (marginBottom ?? 0) + marginToDistribute
11091103
} else if let _ = _bottom {
11101104
marginTop = (marginTop ?? 0) + marginToDistribute

PinLayoutSample/.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.2.5
1+
2.3.3

PinLayoutSample/Podfile

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ workspace 'PinLayoutSample.xcworkspace'
44

55
target 'PinLayoutSample' do
66
project 'PinLayoutSample.xcodeproj'
7-
#pod 'YogaKit', '~> 1.1'
87

98
# Debug only
10-
pod 'Reveal-SDK', :configurations => ['Debug']
11-
pod 'SwiftLint'
9+
pod 'Reveal-SDK', :configurations => ['Debug']
10+
pod 'SwiftLint'
1211
end
13-
14-
#target 'PinLayoutTests' do
15-
# project '../PinLayout.xcodeproj'
16-
# pod 'Quick'
17-
# pod 'Nimble'
18-
#end

PinLayoutSample/Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
PODS:
22
- Reveal-SDK (8)
3-
- SwiftLint (0.18.1)
3+
- SwiftLint (0.19.0)
44

55
DEPENDENCIES:
66
- Reveal-SDK
77
- SwiftLint
88

99
SPEC CHECKSUMS:
1010
Reveal-SDK: 43be4e662864e937960d0d04d005135e29c4e53b
11-
SwiftLint: b467d08f5b25dc3b3cfed243d8e1b74b91714c67
11+
SwiftLint: 3537a05b34060e78e7510f04fb7537d738247803
1212

13-
PODFILE CHECKSUM: 59a9a3ad8ae4f0bb64a6dadf661a354de61b0d83
13+
PODFILE CHECKSUM: 500d466f69a20c6292b04b938d6dce3e1f8f6e8b
1414

15-
COCOAPODS: 1.1.1
15+
COCOAPODS: 1.2.1.beta.1

PinLayoutTests/AdjustSizeSpec.swift

Lines changed: 218 additions & 38 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Swift manual views layouting without auto layout, no magic, pure code, full cont
4343
* Concise syntax. Layout most views using a single line.
4444

4545
* Stateless
46-
* The layout system doesn’t add any stored properties to UIViews. It simply computes the UIView.frame property, one view at a time.
46+
* PinLayout doesn’t add any stored properties to UIViews. It simply computes the UIView.frame property, one view at a time.
4747
* Since it is stateless, it can be used with any other layout framework without conflicts.
4848
Each view can use the layout system that better suit it (PinLayout, constraints, flexbox, grids, …)
4949
A view can be layouted using PinLayout and later with another method/framework.
@@ -55,6 +55,8 @@ A view can be layouted using PinLayout and later with another method/framework.
5555

5656
* Before applying the new sets of attributes, PinLayout always start with the view’s current frame. So it’s possible to set the view’s size during the initialization (ex: view.pin.width(100).height(200)), and later only position the view (ex: view.pin.top(10).left(20)). This makes PinLayout really animation friendly.
5757

58+
* Not too intrusive. PinLayout only adds three properties to existing iOS classes: `UIView.pin`, `UIView.anchor` and `UIView.edge`
59+
5860
* Minimize as much as possible calculations and constants when layouting views.
5961

6062
* Methods match as much as possible other layouting systems, including CSS, flexbox, reactive Flexbox, …
@@ -118,6 +120,8 @@ override func layoutSubviews() {
118120
}
119121
```
120122

123+
:pushpin: This example and some other examples are available in the **PinLayoutSample** project. Please note that you must do a `pod install` before running the sample project.
124+
121125
<br/>
122126

123127

@@ -692,6 +696,30 @@ Warnings can be disabled in debug mode too by setting the boolean PinLayoutLogCo
692696

693697
<br/>
694698

699+
## PinLayout style guide
700+
701+
* You should always specifies methods in the same order, it makes layout lines easier to understand. Here is our prefered ordering:
702+
`view.pin.[EDGE|ANCHOR|RELATIVE].[WIDTH|HEIGHT|SIZE].[pinEdges()].[MARGINS].[sizeToFit()]`
703+
704+
This order reflect the logic inside PinLayout. `pinEdges()` is applied before margins and margins are applied before `sizeToFit()`.
705+
706+
```javascript
707+
view.pin.top().left(10%).margin(10, 12, 10, 12)
708+
view.pin.left().width(100%).pinEdges().marginHorizontal(12)
709+
view.pin.left().right().margin(0, 12).sizeToFit()
710+
view.pin.width(100).height(100%)
711+
```
712+
713+
* If the layout line is too long, you can split in multiple lines:
714+
715+
```
716+
textLabel.pin.below(of: titleLabel)
717+
.right(of: statusIcon).left(of: accessoryView)
718+
.above(of: button).marginHorizontal(10)
719+
```
720+
721+
<br/>
722+
695723
## More examples<a name="more_examples"></a>
696724

697725
### Adjust to container size
@@ -746,7 +774,7 @@ Cell D:
746774
<br>
747775

748776
## Comments, ideas, suggestions, issues, ....
749-
For any (I really meant it) **comments**, **ideas**, **suggestions**, **issues**, simply open an [issue](https://github.com/mirego/PinLayout/issues).
777+
For any **comments**, **ideas**, **suggestions**, **issues**, simply open an [issue](https://github.com/mirego/PinLayout/issues).
750778

751779
<br>
752780

0 commit comments

Comments
 (0)