-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathPinLayout+Size.swift
266 lines (225 loc) · 10.8 KB
/
PinLayout+Size.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 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
enum AdjustSizeType {
case fitTypeWidth
case fitTypeHeight
case fitTypeWidthFlexible
case fitTypeHeightFlexible
case fitTypeContent
case aspectRatio(CGFloat)
var isFlexible: Bool {
if case .fitTypeWidthFlexible = self {
return true
} else if case .fitTypeHeightFlexible = self {
return true
} else {
return false
}
}
internal var requiresSizeCalculable: Bool {
switch self {
case .fitTypeWidth, .fitTypeHeight,
.fitTypeWidthFlexible, .fitTypeHeightFlexible,
.fitTypeContent:
return true
case .aspectRatio(_):
return false
}
}
func toFitType() -> FitType? {
switch self {
case .fitTypeWidth: return .width
case .fitTypeHeight: return .height
case .fitTypeWidthFlexible: return .widthFlexible
case .fitTypeHeightFlexible: return .heightFlexible
default: return nil
}
}
}
extension FitType {
func toAdjustSizeType() -> AdjustSizeType {
switch self {
case .width: return .fitTypeWidth
case .height: return .fitTypeHeight
case .widthFlexible: return .fitTypeWidthFlexible
case .heightFlexible: return .fitTypeHeightFlexible
case .content: return .fitTypeContent
}
}
}
extension PinLayout {
@discardableResult
public func size(_ size: CGSize) -> PinLayout {
return setSize(size, { return "size(CGSize(width: \(size.width), height: \(size.height)))" })
}
@discardableResult
public func size(_ sideLength: CGFloat) -> PinLayout {
return setSize(CGSize(width: sideLength, height: sideLength), { return "size(sideLength: \(sideLength))" })
}
@discardableResult
public func size(_ percent: Percent) -> PinLayout {
func context() -> String { return "size(\(percent.description))" }
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
let size = CGSize(width: percent.of(layoutSuperviewRect.width), height: percent.of(layoutSuperviewRect.height))
return setSize(size, context)
}
@discardableResult
public func size(of view: PinView) -> PinLayout {
func context() -> String { return "size(of \(viewDescription(view)))" }
return setSize(view.getRect(keepTransform: keepTransform).size, context)
}
/**
Set the view aspect ratio.
AspectRatio is applied only if a single dimension (either width or height) can be determined,
in that case the aspect ratio will be used to compute the other dimension.
* AspectRatio is defined as the ratio between the width and the height (width / height).
* An aspect ratio of 2 means the width is twice the size of the height.
* AspectRatio respects the min (minWidth/minHeight) and the max (maxWidth/maxHeight)
dimensions of an item.
*/
@discardableResult
public func aspectRatio(_ ratio: CGFloat) -> PinLayout {
return setAdjustSizeType(.aspectRatio(ratio), { "aspectRatio(\(ratio))" })
}
/**
Set the view aspect ratio using another UIView's aspect ratio.
AspectRatio is applied only if a single dimension (either width or height) can be determined,
in that case the aspect ratio will be used to compute the other dimension.
* AspectRatio is defined as the ratio between the width and the height (width / height).
* AspectRatio respects the min (minWidth/minHeight) and the max (maxWidth/maxHeight)
dimensions of an item.
*/
@discardableResult
public func aspectRatio(of view: PinView) -> PinLayout {
let rect = view.getRect(keepTransform: keepTransform)
return setAdjustSizeType(.aspectRatio(rect.width / rect.height), { "aspectRatio(of: \(viewDescription(view)))" })
}
/**
If the layouted view is an UIImageView, this method will set the aspectRatio using
the UIImageView's image dimension.
For other types of views, this method as no impact.
*/
#if os(iOS) || os(tvOS)
@discardableResult
public func aspectRatio() -> PinLayout {
func context() -> String { return "aspectRatio()" }
guard let imageView = view as? UIImageView else {
warnWontBeApplied("the layouted view must be an UIImageView() to use the aspectRatio() method without parameter.", context)
return self
}
guard let imageSize = imageView.image?.size else {
guard Pin.logWarnings && Pin.activeWarnings.aspectRatioImageNotSet else { return self }
warnWontBeApplied("the layouted UIImageView's image hasn't been set (aspectRatioImageNotSet)", context)
return self
}
return setAdjustSizeType(.aspectRatio(imageSize.width / imageSize.height), context)
}
#endif
/**
The method adjust the view's size based on the view's `sizeThatFits()` method result.
PinLayout will adjust either the view's width or height based on the `fitType` parameter value.
Notes:
* If margin rules apply, margins will be applied when determining the reference dimension (width/height).
* The resulting size will always respect `minWidth` / `maxWidth` / `minHeight` / `maxHeight`.
- Parameter fitType: Identify the reference dimension (width / height) that will be used
to adjust the view's size:
.width: The method adjust the view's size based on the **reference width**.
* If properties related to the width have been pinned (e.g: width, left & right, margins, ...),
the **reference width will be determined by these properties**, if not the **current view's width**
will be used.
* The resulting width will always **match the reference width**.
.height: The method adjust the view's size based on the **reference height**.
* If properties related to the height have been pinned (e.g: height, top & bottom, margins, ...),
the **reference height will be determined by these properties**, if not the **current view's height**
will be used.
* The resulting height will always **match the reference height**.
.widthFlexible: Similar to `.width`, except that PinLayout won't constrain the resulting width to
match the reference width. The resulting width may be smaller or bigger depending on the view's
sizeThatFits(..) method result. For example a single line UILabel may returns a smaller width if its
string is smaller than the reference width.
.heightFlexible: Similar to `.height`, except that PinLayout won't constrain the resulting height to
match the reference height. The resulting height may be smaller or bigger depending on the view's
sizeThatFits(..) method result.
Examples:
```
// Adjust the view's size based on a width of 100 pixels.
// The resulting width will always match the pinned property `width(100)`.
view.pin.width(100).sizeToFit(.width)
// Adjust the view's size based on view's current width.
// The resulting width will always match the view's original width.
// The resulting height will never be bigger than the specified `maxHeight`.
view.pin.sizeToFit(.width).maxHeight(100)
// Adjust the view's size based on 100% of the superview's height.
// The resulting height will always match the pinned property `height(100%)`.
view.pin.height(100%).sizeToFit(.height)
// Adjust the view's size based on view's current height.
// The resulting width will always match the view's original height.
view.pin.sizeToFit(.height)
// Since `.widthFlexible` has been specified, its possible that the resulting
// width will be smaller or bigger than 100 pixels, based of the label's sizeThatFits()
// method result.
label.pin.width(100).sizeToFit(.widthFlexible)
```
*/
@discardableResult
public func sizeToFit(_ fitType: FitType = .content) -> PinLayout {
return setAdjustSizeType(fitType.toAdjustSizeType(), { return "sizeToFit(\(fitType.description))" })
}}
//
// MARK: Private methods
extension PinLayout {
internal func setSize(_ size: CGSize, _ context: Context) -> PinLayout {
setWidth(size.width, { return "\(context())'s width" })
setHeight(size.height, { return "\(context())'s height" })
return self
}
internal func setAdjustSizeType(_ type: AdjustSizeType?, _ context: Context) -> PinLayout {
guard adjustSizeType == nil else {
warnAdjustSizeConflict(context: context)
return self
}
if let type = type {
if case let AdjustSizeType.aspectRatio(ratio) = type, ratio <= 0 {
warnWontBeApplied("the aspectRatio (\(ratio)) must be greater than zero.", context)
return self
} else if type.requiresSizeCalculable, !(view is SizeCalculable) {
warnWontBeApplied("PinLayout cannot comptute this view's size. This type of views doesn't conform to the protocol SizeCalculable.", context)
return self
}
}
adjustSizeType = type
return self
}
private func warnAdjustSizeConflict(context: Context) {
guard let adjustSizeType = adjustSizeType else { return }
let conflict: String
switch adjustSizeType {
case .fitTypeWidth, .fitTypeHeight, .fitTypeWidthFlexible, .fitTypeHeightFlexible, .fitTypeContent:
conflict = "sizeToFit(\(adjustSizeType.toFitType()!.description))."
case .aspectRatio(let ratio):
conflict = "aspectRatio(\(ratio))"
}
warn("PinLayout Conflict: \(context()) won't be applied since it conflicts with \(conflict).")
}
}