-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathPinLayout+WrapContent.swift
143 lines (122 loc) · 5.84 KB
/
PinLayout+WrapContent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2018 Luc Dion
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if os(iOS) || os(tvOS)
import UIKit
#else
import AppKit
#endif
extension PinLayout {
/**
Adjust the view's width & height to wrap all its subviews that are included in the size calculation. The method also adjust subviews position to create a tight wrap.
*/
@discardableResult
public func wrapContent() -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), { return "wrapContent()" })
}
/**
Adjust the view's width & height to wrap all its subviews that are included in the size calculation. The method also adds a padding around all subviews.
- Parameters:
- padding: Specify a padding value.
*/
@discardableResult
public func wrapContent(padding: CGFloat) -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(padding: \(padding)" })
}
/**
The method...
- Adjust the view's width and height to wrap all its subviews that are included in the size calculation.
- Adjust subviews's position to create a tight wrap.
- Apply padding around all subviews.
- Parameters:
- padding: Specify a padding using an UIEdgeInsets.
*/
@discardableResult
public func wrapContent(padding: PEdgeInsets) -> PinLayout {
return wrapContent(.all, padding: padding, { return "wrapContent(padding: \(insetsDescription(padding))" })
}
/**
The method...
- Adjust the view's width and height to wrap all its subviews that are included in the size calculation.
- Adjust subviews's position to create a tight wrap.
- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
*/
@discardableResult
public func wrapContent(_ type: WrapType) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), { return "wrapContent(\(type.description)" })
}
/**
The method...
- Adjust the view's width and height to wrap all its subviews that are included in the size calculation.
- Adjust subviews's position to create a tight wrap.
- Apply padding around all subviews.
- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
- padding: Specify a padding value.
*/
@discardableResult
public func wrapContent(_ type: WrapType, padding: CGFloat) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(\(type.description), padding: \(padding)" })
}
/**
The method...
- Adjust the view's width and height to wrap all its subviews that are included in the size calculation.
- Adjust subviews's position to create a tight wrap.
- Apply padding around all subviews.
- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
- padding: Specify a padding using an UIEdgeInsets.
*/
@discardableResult
public func wrapContent(_ type: WrapType, padding: PEdgeInsets) -> PinLayout {
return wrapContent(type, padding: padding, { return "wrapContent(\(type.description), padding: \(insetsDescription(padding))" })
}
private func wrapContent(_ type: WrapType, padding: PEdgeInsets, _ context: Context) -> PinLayout {
let includedSubviews = view.subviewsIncludedInSizeCalculation
guard !includedSubviews.isEmpty else { return self }
let firstViewRect = includedSubviews[0].getRect(keepTransform: keepTransform)
let boundingRect = includedSubviews.reduce(firstViewRect, { (result, view) in
result.union(view.getRect(keepTransform: keepTransform))
})
var offsetDx: CGFloat = 0
var offsetDy: CGFloat = 0
if type == .all || type == .horizontally {
let contentWidth = boundingRect.width + padding.left + padding.right
if contentWidth >= 0 {
setWidth(contentWidth, context)
}
offsetDx = -boundingRect.minX + padding.left
}
if type == .all || type == .vertically {
let contentHeight = boundingRect.height + padding.top + padding.bottom
if contentHeight >= 0 {
setHeight(contentHeight, context)
}
offsetDy = -boundingRect.minY + padding.top
}
if offsetDx != 0 || offsetDy != 0 {
includedSubviews.forEach { (view) in
let viewRect = view.getRect(keepTransform: keepTransform)
let newRect = viewRect.offsetBy(dx: offsetDx, dy: offsetDy)
view.setRect(newRect, keepTransform: keepTransform)
}
}
return self
}
}