Skip to content

Commit 2de2708

Browse files
author
Luc Dion
committed
Initial commit
1 parent 666ba1a commit 2de2708

File tree

8 files changed

+294
-101
lines changed

8 files changed

+294
-101
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ TestProjects/cocoapods/macos/PinLayout-macOS.xcworkspace/
1919
TestProjects/cocoapods/macos/Podfile.lock
2020
TestProjects/cocoapods/tvos/PinLayout-tvOS.xcworkspace/
2121
TestProjects/cocoapods/tvos/Podfile.lock
22+
2223
TestProjects/carthage/ios/Cartfile.resolved
2324
TestProjects/carthage/ios/Carthage/
2425

2526
TestProjects/swift-package-manager
2627

27-
Example/PinLayoutExampleMacOS
28+
Example/PinLayoutExampleMacOS

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Extremely Fast views layouting without auto layout. No magic, pure code, full co
3737
* Swift 3.2+ / Swift 4.0 / Objective-C
3838

3939
### Recent features
40+
* :star: Add methods `wrapConten(...)` that adjust the view's width & height to wrap all its subviews. See [wrapContent](#wrapCcontent) for more information.
41+
4042
* :star: PinLayout now support macOS. See [macOS Support](#macos_support) for more information.
4143

4244
* :star: PinLayout expose the `safeAreaInsets` through [`UIView.pin.safeArea`](#safeAreaInsets), this property support not only iOS 11, but is also backward compatible for earlier iOS releases (7/8/9/10). See [safeAreaInsets support](#safeAreaInsets) for more information.
@@ -61,6 +63,7 @@ Extremely Fast views layouting without auto layout. No magic, pure code, full co
6163
* [Aspect Ratio](#aspect_ratio)
6264
* [Margins](#margins)
6365
* [safeAreaInsets support](#safeAreaInsets)
66+
* [WrapContent](#wrapCcontent)
6467
* [justify, align](#justify_align)
6568
* [UIView's transforms](#uiview_transform)
6669
* [Warnings](#warnings)
@@ -1134,8 +1137,51 @@ This example runs perfectly on a iPhone X (iOS 11), but it also runs on any devi
11341137

11351138
<br/>
11361139

1140+
<a name="wrapCcontent"></a>
1141+
## WrapContent
11371142

1138-
<a name="aspect_ratio"></a>
1143+
The following methods are useful to adjust the view's width and/or height to wrap all its subviews. These method also adjust subviews position to create a tight wrap.
1144+
1145+
**Methods:**
1146+
1147+
* **`wrapContent()`**
1148+
Bla.
1149+
* **`wrapContent(padding: CGFloat)`**
1150+
Bla.
1151+
* **`wrapContent(padding: UIEdgeInsets)`**
1152+
Bla.
1153+
* **`wrapContent(:WrapType)`**
1154+
Bla.
1155+
* **`wrapContent(:WrapType, padding: CGFloat)`**
1156+
Bla.
1157+
* **`wrapContent(:WrapType, padding: UIEdgeInsets)`**
1158+
Bla.
1159+
1160+
TABLE SHOWING DIFFERENCES!!!
1161+
1162+
###### Usage examples:
1163+
```swift
1164+
view.pin.wrapContent().center() // wrap all subviews and centered the view inside its parent.
1165+
view.pin.wrapContent(padding: 20) // wrap all subviews with a padding of 20 pixels all around
1166+
```
1167+
1168+
1169+
###### Example:
1170+
...:
1171+
1172+
<img src="docs/pinlayout-example-justify-left.png" width="540"/>
1173+
1174+
1175+
```swift
1176+
viewA.pin.....
1177+
```
1178+
1179+
<br/>
1180+
1181+
1182+
1183+
1184+
<a name="justify_align"></a>
11391185
## justify() / align()
11401186

11411187
**Methods:**

Sources/Impl/PinLayoutImpl+WrapContent.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,27 @@ extension PinLayoutImpl {
2828
return wrapContent(.all, padding: .zero, { return "wrapContent()" })
2929
}
3030

31+
func wrapContent(padding: CGFloat) -> PinLayout {
32+
return wrapContent(.all, padding: UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(padding: \(padding)" })
33+
}
34+
35+
func wrapContent(padding: UIEdgeInsets) -> PinLayout {
36+
return wrapContent(.all, padding: padding, { return "wrapContent(padding: \(insetsDescription(padding))" })
37+
}
38+
3139
func wrapContent(_ type: WrapType) -> PinLayout {
3240
return wrapContent(type, padding: .zero, { return "wrapContent(\(type.description)" })
3341
}
3442

35-
@discardableResult
36-
private func wrapContent(_ type: WrapType, padding: PPadding, _ context: Context) -> PinLayout {
43+
func wrapContent(_ type: WrapType, padding: CGFloat) -> PinLayout {
44+
return wrapContent(type, padding: UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(\(type.description), padding: \(padding)" })
45+
}
46+
47+
func wrapContent(_ type: WrapType, padding: UIEdgeInsets) -> PinLayout {
48+
return wrapContent(type, padding: padding, { return "wrapContent(\(type.description), padding: \(insetsDescription(padding))" })
49+
}
50+
51+
private func wrapContent(_ type: WrapType, padding: UIEdgeInsets, _ context: Context) -> PinLayout {
3752
let subviews = view.subviews
3853
guard !subviews.isEmpty else { return self }
3954
let firstViewFrame = subviews[0].frame
@@ -61,28 +76,22 @@ extension PinLayoutImpl {
6176
var offsetDx: CGFloat = 0
6277
var offsetDy: CGFloat = 0
6378

64-
if type == .all || type == .width {
65-
let contentWidth = maxX - minX
79+
if type == .all || type == .horizontally {
80+
let contentWidth = maxX - minX + padding.left + padding.right
6681
if contentWidth >= 0 {
6782
setWidth(contentWidth, context)
6883
}
6984

70-
offsetDx = -minX
71-
// let contentWidth = view.subviews.max(by: { subview1, subview2 in
72-
// subview1.frame.maxX < subview2.frame.maxX
73-
// })?.frame.maxX ?? 0
85+
offsetDx = -minX + padding.left
7486
}
7587

76-
if type == .all || type == .height {
77-
let contentHeight = maxY - minY
88+
if type == .all || type == .vertically {
89+
let contentHeight = maxY - minY + padding.top + padding.bottom
7890
if contentHeight >= 0 {
7991
setHeight(contentHeight, context)
8092
}
8193

82-
offsetDy = -minY
83-
// let contentHeight = view.subviews.max(by: { subview1, subview2 in
84-
// subview1.frame.maxY < subview2.frame.maxY
85-
// })?.frame.maxY ?? 0
94+
offsetDy = -minY + padding.top
8695
}
8796

8897
if offsetDx != 0 || offsetDy != 0 {

Sources/PinLayout+Filters.swift

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

Sources/PinLayout.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,48 @@ public protocol PinLayout {
308308
@discardableResult func size(of view: NSView) -> PinLayout
309309
#endif
310310

311+
/**
312+
Adjust the view's width & height to wrap all its subviews. The method also adjust subviews position to create a tight wrap.
313+
*/
311314
@discardableResult func wrapContent() -> PinLayout
315+
/**
316+
Adjust the view's width & height to wrap all its subviews. The method also adds a padding around all subviews.
317+
318+
- Parameters:
319+
- padding: Specify a padding value.
320+
*/
321+
@discardableResult func wrapContent(padding: CGFloat) -> PinLayout
322+
/**
323+
Adjust the view's width & height to wrap all its subviews. The method also adds a padding around all subviews.
324+
325+
- Parameters:
326+
- padding: Specify a padding using an UIEdgeInsets.
327+
*/
328+
@discardableResult func wrapContent(padding: UIEdgeInsets) -> PinLayout
329+
330+
/**
331+
Adjust the view's width AND/OR height to wrap all its subviews.
332+
333+
- Parameters:
334+
- type: Specify the wrap type (.all, .horizontally, .vertically)
335+
*/
312336
@discardableResult func wrapContent(_ type: WrapType) -> PinLayout
313-
@discardableResult func wrapContent(padding: PPadding) -> PinLayout
314-
@discardableResult func wrapContent(_ type: WrapType, padding: PPadding) -> PinLayout
337+
/**
338+
Adjust the view's width AND/OR height to wrap all its subviews. The method also adds a padding around all subviews.
339+
340+
- Parameters:
341+
- type: Specify the wrap type (.all, .horizontally, .vertically)
342+
- padding: Specify a padding value.
343+
*/
344+
@discardableResult func wrapContent(_ type: WrapType, padding: CGFloat) -> PinLayout
345+
/**
346+
Adjust the view's width AND/OR height to wrap all its subviews. The method also adds a padding around all subviews.
347+
348+
- Parameters:
349+
- type: Specify the wrap type (.all, .horizontally, .vertically)
350+
- padding: Specify a padding using an UIEdgeInsets.
351+
*/
352+
@discardableResult func wrapContent(_ type: WrapType, padding: UIEdgeInsets) -> PinLayout
315353

316354
/**
317355
Set the view aspect ratio.

Sources/Types+Description.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ extension WrapType {
5555
var description: String {
5656
switch self {
5757
case .all: return ".all"
58-
case .width: return ".width"
59-
case .height: return ".height"
58+
case .horizontally: return ".horizontally"
59+
case .vertically: return ".vertically"
6060
}
6161
}
6262
}

Sources/Types.swift

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -167,47 +167,12 @@ public enum FitType {
167167
}
168168

169169
public enum WrapType {
170-
/**
171-
*/
170+
/// Adjust the view's width AND height to wrap all its subviews.
172171
case all
173-
/**
174-
*/
175-
case width
176-
/**
177-
*/
178-
case height
179-
}
180-
181-
public struct PPadding {
182-
let top: CGFloat
183-
let left: CGFloat
184-
let bottom: CGFloat
185-
let right: CGFloat
186-
187-
static var zero: PPadding {
188-
return PPadding(0)
189-
}
190-
191-
init(_ all: CGFloat) {
192-
top = all
193-
left = all
194-
bottom = all
195-
right = all
196-
}
197-
198-
init(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) {
199-
self.top = top
200-
self.left = left
201-
self.bottom = bottom
202-
self.right = right
203-
}
204-
205-
init(h: CGFloat, v: CGFloat) {
206-
top = v
207-
left = h
208-
bottom = v
209-
right = h
210-
}
172+
/// Adjust only the view's width to wrap all its subviews. The view's height won't be modified.
173+
case horizontally
174+
/// Adjust only the view's height to wrap all its subviews. The view's width won't be modified.
175+
case vertically
211176
}
212177

213178
@objc public enum LayoutDirection: Int {

0 commit comments

Comments
 (0)