Skip to content

Commit ef0bed7

Browse files
Luc DionLuc Dion
authored andcommitted
Add methods that takes percentage parameters
It is now possible to user ‘%’ characters. ex: view.pin.top().left(25%).width(25%).height(100%) * top(_ percent: Percent) * left(_ percent: Percent) * bottom(_ percent: Percent) * right(_ percent: Percent) * hCenter(_ percent: Percent) * vCenter(_ percent: Percent) Plus: * Implement hCenter() & vCenter() * Implement margin(_ vertical: CGFloat, _ horizontal: CGFloat) & margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) * Add more unit tests * Removed few forced optionals * Removed deprecated UIView+PinLayout extension.
1 parent b2255e4 commit ef0bed7

File tree

11 files changed

+509
-364
lines changed

11 files changed

+509
-364
lines changed

Docs/Notes.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
TODO:
1010
===============================================
1111
- Nice introduction https://github.com/stevestreza/Relayout
12-
12+
13+
- Add a doc example using width(40%).height(100%)
14+
1315
- Antoine Lamy: Suggestion pour ton système de layout (pt déjà le cas): Utiliser une interface entre l'engin de layout et
1416
UIKit afin de potentiellement être capable de le bridger vers d'autres entitées visuelles qu'une
1517
UIView (exemple: CALayer, NSView, ASDisplayNode, etc). L'engin n'a probablement à dealer qu'avec des
@@ -30,13 +32,18 @@
3032
pin.left(20%)
3133
pin.bottom(10%)
3234
pin.right(15%)
33-
pin.width(45%)
34-
pin.height(100%)
35-
pin.size(100%)
36-
pin.Height == 47 % button.Width
3735

3836
- Add parameter to sizeToFit indiquant si on doit caster ou pas le résultat
39-
- Add minHeight, maxHeight, ...
37+
38+
- max/max height/width
39+
- minWidth(CGFloat)
40+
- minWidth(Percent)
41+
- maxWidth(CGFloat)
42+
- maxWidth(Percent)
43+
- minHeight(CGFloat)
44+
- minHeight(Percent)
45+
- maxHeight(CGFloat)
46+
- maxHeight(Percent)
4047

4148
- Add size(_ width: CGFloat, _ height: CGFloat)
4249
- Add size(widthAndHeight: CGFloat)

PinLayout.xcodeproj/project.pbxproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
249EFE891E64FB4C00165E39 /* PinLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249EFE881E64FB4C00165E39 /* PinLayoutTests.swift */; };
2424
249EFE8B1E64FB4C00165E39 /* PinLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 249EFE7D1E64FB4C00165E39 /* PinLayout.h */; settings = {ATTRIBUTES = (Public, ); }; };
2525
DF11A3711E833F03008B33E5 /* TypesImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF11A3701E833F03008B33E5 /* TypesImpl.swift */; };
26-
DF11A3781E834B32008B33E5 /* UIView+PinLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF11A3771E834B32008B33E5 /* UIView+PinLayout.swift */; };
2726
DF11A37A1E834B3F008B33E5 /* Coordinates.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF11A3791E834B3F008B33E5 /* Coordinates.swift */; };
2827
DF7A36BD1E918301000F9856 /* PinEdgesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF7A36BC1E918301000F9856 /* PinEdgesSpec.swift */; };
2928
DFC97CA51E8A8EB3001545D5 /* PinLayoutImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFC97CA41E8A8EB3001545D5 /* PinLayoutImpl.swift */; };
@@ -72,7 +71,6 @@
7271
249EFE881E64FB4C00165E39 /* PinLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinLayoutTests.swift; sourceTree = "<group>"; };
7372
249EFE8A1E64FB4C00165E39 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7473
DF11A3701E833F03008B33E5 /* TypesImpl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypesImpl.swift; sourceTree = "<group>"; };
75-
DF11A3771E834B32008B33E5 /* UIView+PinLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+PinLayout.swift"; sourceTree = "<group>"; };
7674
DF11A3791E834B3F008B33E5 /* Coordinates.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Coordinates.swift; sourceTree = "<group>"; };
7775
DF7A36BC1E918301000F9856 /* PinEdgesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinEdgesSpec.swift; sourceTree = "<group>"; };
7876
DFC97CA41E8A8EB3001545D5 /* PinLayoutImpl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinLayoutImpl.swift; sourceTree = "<group>"; };
@@ -134,7 +132,6 @@
134132
children = (
135133
DFC97CA61E8A8F2C001545D5 /* PinLayout.swift */,
136134
DFA06B031E8B38B300B6D5E7 /* Impl */,
137-
DF11A3751E834194008B33E5 /* Extensions */,
138135
DF11A3761E8341A2008B33E5 /* Helpers */,
139136
);
140137
path = PinLayout;
@@ -167,14 +164,6 @@
167164
path = "Supporting Files";
168165
sourceTree = "<group>";
169166
};
170-
DF11A3751E834194008B33E5 /* Extensions */ = {
171-
isa = PBXGroup;
172-
children = (
173-
DF11A3771E834B32008B33E5 /* UIView+PinLayout.swift */,
174-
);
175-
path = Extensions;
176-
sourceTree = "<group>";
177-
};
178167
DF11A3761E8341A2008B33E5 /* Helpers */ = {
179168
isa = PBXGroup;
180169
children = (
@@ -325,7 +314,6 @@
325314
buildActionMask = 2147483647;
326315
files = (
327316
DF11A3711E833F03008B33E5 /* TypesImpl.swift in Sources */,
328-
DF11A3781E834B32008B33E5 /* UIView+PinLayout.swift in Sources */,
329317
DF11A37A1E834B3F008B33E5 /* Coordinates.swift in Sources */,
330318
DFC97CA71E8A8F2C001545D5 /* PinLayout.swift in Sources */,
331319
DFC97CA51E8A8EB3001545D5 /* PinLayoutImpl.swift in Sources */,

PinLayout/Extensions/UIView+PinLayout.swift

Lines changed: 0 additions & 152 deletions
This file was deleted.

PinLayout/Helpers/Coordinates.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828
import UIKit
2929

3030
class Coordinates {
31-
static func top(_ view: UIView) -> CGFloat {
32-
return view.frame.minY
33-
}
34-
35-
static func left(_ view: UIView) -> CGFloat {
36-
return view.frame.minX
37-
}
38-
39-
static func bottom(_ view: UIView) -> CGFloat {
40-
return view.frame.maxY
41-
}
42-
43-
static func right(_ view: UIView) -> CGFloat {
44-
return view.frame.maxX
45-
}
46-
4731
static func hCenter(_ view: UIView) -> CGFloat {
4832
return view.frame.minX + (view.frame.width / 2)
4933
}
@@ -52,17 +36,6 @@ class Coordinates {
5236
return view.frame.minY + (view.frame.height / 2)
5337
}
5438

55-
// static func size(_ view: UIView) -> CGSize {
56-
// return view.frame.size
57-
// }
58-
// static func width(_ view: UIView) -> CGFloat {
59-
// return view.frame.width
60-
// }
61-
//
62-
// static func height(_ view: UIView) -> CGFloat {
63-
// return view.frame.height
64-
// }
65-
6639
static func topLeft(_ view: UIView) -> CGPoint {
6740
return CGPoint(x: view.frame.minX, y: view.frame.minY)
6841
}
@@ -115,8 +88,4 @@ class Coordinates {
11588
static func ceilFloatToDisplayScale(_ pointValue: CGFloat) -> CGFloat {
11689
return CGFloat(ceilf(Float(pointValue * displayScale))) / displayScale
11790
}
118-
119-
static func overwriteDisplayScaleForUnitTest(scale: CGFloat) {
120-
displayScale = scale
121-
}
12291
}

PinLayout/PinLayout.swift

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ public protocol AnchorList {
101101
bottom
102102
*/
103103

104-
/// UIView's horizontal edges (left/right) definition
105-
public protocol HorizontalEdge {
106-
var x: CGFloat { get }
107-
}
108-
109-
/// UIView's vertical edges (top/bottom) definition
110-
public protocol VerticalEdge {
111-
var y: CGFloat { get }
112-
}
113-
114104
/// UIViews's list of edges
115105
public protocol EdgeList {
116106
var top: VerticalEdge { get }
@@ -119,22 +109,34 @@ public protocol EdgeList {
119109
var right: HorizontalEdge { get }
120110
}
121111

122-
123112
/// PinLayout interface
124113
public protocol PinLayout {
125114
//
126115
// Layout using distances from superview’s edges
127116
//
128117
@discardableResult func top() -> PinLayout
129118
@discardableResult func top(_ value: CGFloat) -> PinLayout
119+
@discardableResult func top(_ percent: Percent) -> PinLayout
120+
130121
@discardableResult func left() -> PinLayout
131122
@discardableResult func left(_ value: CGFloat) -> PinLayout
123+
@discardableResult func left(_ percent: Percent) -> PinLayout
124+
132125
@discardableResult func bottom() -> PinLayout
133126
@discardableResult func bottom(_ value: CGFloat) -> PinLayout
127+
@discardableResult func bottom(_ percent: Percent) -> PinLayout
128+
134129
@discardableResult func right() -> PinLayout
135130
@discardableResult func right(_ value: CGFloat) -> PinLayout
131+
@discardableResult func right(_ percent: Percent) -> PinLayout
132+
133+
@discardableResult func hCenter() -> PinLayout
136134
@discardableResult func hCenter(_ value: CGFloat) -> PinLayout
135+
@discardableResult func hCenter(_ percent: Percent) -> PinLayout
136+
137+
@discardableResult func vCenter() -> PinLayout
137138
@discardableResult func vCenter(_ value: CGFloat) -> PinLayout
139+
@discardableResult func vCenter(_ percent: Percent) -> PinLayout
138140

139141
//
140142
// Layout using edges
@@ -190,13 +192,18 @@ public protocol PinLayout {
190192
// Width, height and size
191193
//
192194
@discardableResult func width(_ width: CGFloat) -> PinLayout
193-
@discardableResult func width(percent: CGFloat) -> PinLayout
195+
@discardableResult func width(_ percent: Percent) -> PinLayout
194196
@discardableResult func width(of view: UIView) -> PinLayout
197+
195198
@discardableResult func height(_ height: CGFloat) -> PinLayout
196-
@discardableResult func height(percent: CGFloat) -> PinLayout
199+
@discardableResult func height(_ percent: Percent) -> PinLayout
197200
@discardableResult func height(of view: UIView) -> PinLayout
201+
198202
@discardableResult func size(_ size: CGSize) -> PinLayout
203+
@discardableResult func size(_ sideLength: CGFloat) -> PinLayout
204+
@discardableResult func size(_ percent: Percent) -> PinLayout
199205
@discardableResult func size(of view: UIView) -> PinLayout
206+
200207
@discardableResult func sizeToFit() -> PinLayout
201208

202209
//
@@ -270,3 +277,32 @@ public enum VerticalAlignment: String {
270277
case center
271278
case bottom
272279
}
280+
281+
/// UIView's horizontal edges (left/right) definition
282+
public protocol HorizontalEdge {
283+
var x: CGFloat { get }
284+
}
285+
286+
/// UIView's vertical edges (top/bottom) definition
287+
public protocol VerticalEdge {
288+
var y: CGFloat { get }
289+
}
290+
291+
/// Percent
292+
public struct Percent {
293+
let value: CGFloat
294+
}
295+
296+
postfix operator %
297+
public postfix func % (v: CGFloat) -> Percent {
298+
return Percent(value: v)
299+
}
300+
301+
public postfix func % (v: Int) -> Percent {
302+
return Percent(value: CGFloat(v))
303+
}
304+
305+
prefix operator -
306+
public prefix func - (p: Percent) -> Percent {
307+
return Percent(value: -p.value)
308+
}

0 commit comments

Comments
 (0)