Skip to content

Commit d2f63e4

Browse files
committed
Shorten and update motivation / introduction part of README
1 parent dfef87b commit d2f63e4

File tree

2 files changed

+58
-62
lines changed

2 files changed

+58
-62
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Comparing GetLaid Against its Alternatives
2+
3+
I invite you to extend this comparison. First, the example code itself could be extended and, second, it would be interesting how it looks written with the native API, SnapKit and Anchorage ...
4+
5+
## Before (PureLayout)
6+
7+
~~~swift
8+
item1.autoPinEdgesToSuperviewEdges()
9+
item1.autoPinEdge(toSuperviewEdge: .top)
10+
item1.autoSetDimension(.width, toSize: 42)
11+
item1.autoPinEdge(.left, to: .left, of: item2)
12+
item1.autoAlignAxis(.vertical, toSameAxisOf: item2)
13+
item1.autoSetDimensions(to: CGSize(width: 82, height: 42))
14+
item1.autoPinEdge(.bottom, to: .top, of: item2, withOffset: -20)
15+
item1.autoSetDimension(.height, toSize: 64, relation: .greaterThanOrEqual)
16+
item1.autoPinEdgesToSuperviewEdges(with: NSEdgeInsetsZero, excludingEdge: .top)
17+
item1.autoConstrainAttribute(.left, to: .right, of: parent, withMultiplier: 0.5)
18+
item1.autoConstrainAttribute(.width, to: .height, of: item1, withMultiplier: 16 / 9)
19+
item1.autoPinEdgesToSuperViewEdges(with: NSEdgeInsets(top: 10, left: 0, bottom: 0, right: 0))
20+
~~~
21+
22+
## After (GetLaid)
23+
24+
~~~swift
25+
item1 >> item1.parent
26+
item1 >> item1.parent?.top
27+
item1.width >> 42
28+
item1 >> item2.left
29+
item1 >> item2.centerX
30+
item1 >> (82, 42)
31+
item1.bottom >> item2.top.offset(-20)
32+
item1.height >> .min(64)
33+
item1 >> item1.parent?.allButTop
34+
item1.left >> item1.parent?.right.at(0.5)
35+
item1.width >> item1.height.at(16 / 9)
36+
item1 >> item1.parent?.all(topInset: 10)
37+
~~~
38+
39+
So, which is prettier, mh?

README.md

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,22 @@ GetLaid is a lean framework for laying out complex UIs through short readable co
1919

2020
### Features
2121

22-
GetLaid has some advantages even over classic simple frameworks like [PureLayout](https://github.com/PureLayout/PureLayout):
23-
2422
* :white_check_mark: Readability
25-
- Functions are of the principle form "[constrained aspect] constrain to [constraining aspect]"
26-
- This semantic naming makes auto completion more valuable, as it quickly and meaningfully narrows down the available options.
27-
- All functions have the prefix `constrain` which expresses best what they really do: They constrain some attributes and return the resulting constraints.
28-
- PureLayout is much more convoluted with its 6 different function prefixes: `autoPin`, `autoAlign`, `autoMatch`, `autoCenter`, `autoSet` and `autoConstrain`.
23+
- The syntax is close to natural language instead of technically fancy.
24+
- All constraining has the form `source.constrain(to: target)`.
25+
- The operator `>>` can add further clarity: `source >> target`
2926
* :white_check_mark: Brevity
30-
- Shorter lines of code with less function arguments (see comparison below)
31-
* :white_check_mark: Applicable to Layout Guides
32-
- [UILayoutGuide](https://developer.apple.com/documentation/uikit/uilayoutguide)
33-
- [NSLayoutGuide](https://developer.apple.com/documentation/appkit/nslayoutguide)
34-
* :white_check_mark: Easy Relative Layouting
35-
- Relative positioning: `item1.constrainToParentLeft(at: 0.5)`
36-
- Relative sizing: `item1.constrain(to: item2.width.at(0.3))`
37-
- Aspect ratio: `item.constrainAspectRatio(to: 16/9)`
38-
* :white_check_mark: Easy Positioning of Items Next to Each Other
39-
- `item1.constrain(above: item2, gap: 10)`
40-
- `item1.constrain(after: item2)` (localized via leading / trailing)
27+
- Code lines are super short and involve few function arguments.
28+
- A single code line can do a lot, via combined targets like `allButTop` and `size`.
29+
* :white_check_mark: Integrated Simplicity
30+
- Simple consistent systematic design: Understand 1 thing to do everything.
31+
- Seemless coverage of parent views, safe areas and system spacings.
32+
* :white_check_mark: Easy Advanced Layouting
33+
- Modify any constrain target with `offset(CGFLoat)`, `min`, `max` and `at(_ factor: CGFloat)`.
34+
- Chain target modifications together: `item1 >> item2.size.at(0.5).min`.
35+
* :white_check_mark: Compatability
36+
- Works on iOS, tvOS and macOS
37+
- Works on [UILayoutGuide](https://developer.apple.com/documentation/uikit/uilayoutguide) and [NSLayoutGuide](https://developer.apple.com/documentation/appkit/nslayoutguide) just as well as on views
4138
* :white_check_mark: Modern Swift Under the Hood
4239
- No Objective-c
4340
- Extensive use of [Layout Anchors](https://developer.apple.com/documentation/uikit/nslayoutanchor)
@@ -50,13 +47,13 @@ Well, that [would be insane](https://www.flowtoolz.com/2019/09/27/the-reasons-fo
5047

5148
Programmatic AutoLayout without any such frameworks was never hard. It's all about creating objects of `NSLayoutConstraint`, which has only one [powerful initializer](https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526954-init).
5249

53-
Since iOS 9.0 and macOS 10.11, we also have `NSLayoutAnchor`, which adds a native abstraction layer on top of `NSLayoutConstraint`, further reducing the need for any AutoLayout wrappers at all.
50+
Since iOS/tvOS 9.0 and macOS 10.11, we also have [`NSLayoutAnchor`](https://developer.apple.com/documentation/uikit/nslayoutanchor), which adds a native abstraction layer on top of `NSLayoutConstraint`, further reducing the need for any AutoLayout wrappers at all.
5451

5552
At this point, all an AutoLayout wrapper can do is to make code even more meaningful, readable and succinct at the point of use. GetLaid does exactly that.
5653

5754
### Why Not Other AutoLayout Wrappers?
5855

59-
Modern AutoLayout wrappers like [SnapKit](https://github.com/SnapKit/SnapKit) are almost too clever for the simple task at hand. A SnapKit example:
56+
Modern AutoLayout wrappers like [SnapKit](https://github.com/SnapKit/SnapKit) are almost too clever for the simple task at hand. The first example from the SnapKit README:
6057

6158
~~~swift
6259
box.snp.makeConstraints { (make) -> Void in
@@ -72,53 +69,17 @@ box.autoSetDimensions(to: CGSize(width: 50, height: 50))
7269
box.autoCenterInSuperView()
7370
~~~
7471

75-
GetLaid trims AutoLayout further down to the essence. The operator `>>` can be read as "constrain to":
72+
GetLaid trims AutoLayout further down to the essence. Just read the operator `>>` as "constrain to":
7673

7774
~~~swift
7875
box >> 50
7976
box >> box.parent?.center
8077
~~~
8178

82-
Here's a richer comparison against PureLayout:
83-
84-
#### Before (PureLayout)
85-
86-
~~~swift
87-
item1.autoPinEdgesToSuperviewEdges()
88-
item1.autoPinEdge(toSuperviewEdge: .top)
89-
item1.autoSetDimension(.width, toSize: 42)
90-
item1.autoPinEdge(.left, to: .left, of: item2)
91-
item1.autoAlignAxis(.vertical, toSameAxisOf: item2)
92-
item1.autoSetDimensions(to: CGSize(width: 82, height: 42))
93-
item1.autoPinEdge(.bottom, to: .top, of: item2, withOffset: -20)
94-
item1.autoSetDimension(.height, toSize: 64, relation: .greaterThanOrEqual)
95-
item1.autoPinEdgesToSuperviewEdges(with: NSEdgeInsetsZero, excludingEdge: .top)
96-
item1.autoConstrainAttribute(.left, to: .right, of: parent, withMultiplier: 0.5)
97-
item1.autoConstrainAttribute(.width, to: .height, of: item1, withMultiplier: 16 / 9)
98-
item1.autoPinEdgesToSuperViewEdges(with: NSEdgeInsets(top: 10, left: 0, bottom: 0, right: 0))
99-
~~~
100-
101-
#### After (GetLaid)
102-
103-
~~~swift
104-
item1 >> item1.parent
105-
item1 >> item1.parent?.top
106-
item1.width >> 42
107-
item1 >> item2.left
108-
item1 >> item2.centerX
109-
item1 >> (82, 42)
110-
item1.bottom >> item2.top.offset(-20)
111-
item1.height >> .min(64)
112-
item1 >> item1.parent?.allButTop
113-
item1.left >> item1.parent?.right.at(0.5)
114-
item1.width >> item1.height.at(16 / 9)
115-
item1 >> item1.parent?.all(topInset: 10)
116-
~~~
117-
118-
So, which is prettier, mh?
119-
12079
If you can spare fancyness but appreciate readability, GetLaid might be for you.
12180

81+
Here is also a [richer comparison](Documentation/comparison_to_alternatives.md) of how layout code looks with GetLaid and its alternatives.
82+
12283
## Install
12384

12485
With the [**Swift Package Manager**](https://github.com/apple/swift-package-manager/tree/master/Documentation#swift-package-manager), you can just add the GetLaid package [via Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) (11+).
@@ -384,10 +345,6 @@ spacer.width >> .min(systemSpacing) // minimum spacer width
384345

385346
Remember that these constants are not hardcoded but dynamically calculated on the actual user device, so they are absolutely true to what Apple intents for sibling gaps and parent insets, on any system and on any iOS/tvOS version. But also note that these two values do not capture the system spacing magic that `NSLayoutAnchor` offers in conjunction with baselines and font sizes and possibly in other contexts.
386347

387-
## TO DOcument
388-
389-
* shorten and update motivational introduction
390-
391348
[badge-pod]: https://img.shields.io/cocoapods/v/GetLaid.svg?label=version&style=flat-square
392349

393350
[badge-pms]: https://img.shields.io/badge/supports-SPM%20%7C%20CocoaPods-green.svg?style=flat-square

0 commit comments

Comments
 (0)