-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGRect+Layout.swift
More file actions
372 lines (348 loc) · 10.6 KB
/
CGRect+Layout.swift
File metadata and controls
372 lines (348 loc) · 10.6 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// CGRect+Extension.swift
// KL IoT
//
// Created by 陈旭珂 on 2019/4/7.
// Copyright © 2019 陈旭珂. All rights reserved.
//
// https://github.com/vcxk/CGRectLayout
#if os(iOS)
import UIKit
#elseif os(macOS)
import Foundation
#endif
public extension CGRect {
enum CalculateType {
typealias FloatLiteralType = CGFloat
init(floatLiteral value: CalculateType.FloatLiteralType) {
if value > 1 || value < -1 {
self = .length(CGFloat(value))
} else {
self = .ratio(CGFloat(value))
}
}
case length(CGFloat)
case ratio(CGFloat)
}
}
fileprivate extension CGFloat {
func calculate(_ cal: CGRect.CalculateType) -> CGFloat {
switch cal {
case .length(let len):
return len
case .ratio(let rat):
return self * rat
}
}
var calculateType: CGRect.CalculateType {
return CGRect.CalculateType(floatLiteral: self)
}
}
public extension CGRect {
// property x,y,width,height
var x: CGFloat {
get{ return self.origin.x }
set { self.origin.x = newValue }
}
var y: CGFloat {
get { return self.origin.y }
set { self.origin.y = newValue }
}
var width: CGFloat {
get { return self.size.width }
set { self.size.width = newValue }
}
var height: CGFloat {
get { return self.size.height }
set { self.size.height = newValue }
}
// property left,right,top,bottom
var top:CGFloat {
set{
#if os(iOS)
self.origin.y = newValue
#elseif os(macOS)
self.origin.y = newValue - self.height
#endif
}
get {
#if os(iOS)
return self.origin.y
#elseif os(macOS)
return self.origin.y + self.height
#endif
}
}
var bottom:CGFloat {
get {
#if os(iOS)
return self.origin.y + self.height
#elseif os(macOS)
return self.origin.y
#endif
}
set {
#if os(iOS)
self.origin.y = newValue - self.height
#elseif os(macOS)
self.origin.y = newValue
#endif
}
}
var left:CGFloat {
set { self.x = newValue }
get { return self.x }
}
var right:CGFloat {
get { return self.x + self.width }
set { self.x = newValue - self.width }
}
// property center
var midX : CGFloat {
get { return self.x + self.width/2 }
set { self.x = newValue - self.width/2 }
}
var midY : CGFloat {
get { return self.y + self.height/2 }
set { self.y = newValue - self.height/2 }
}
var center:CGPoint {
get { return CGPoint(x: self.midX, y: self.midY) }
set {
self.midY = newValue.y
self.midX = newValue.x
}
}
mutating func set(top:CGFloat,bottom:CGFloat) {
self.top = top
#if os(iOS)
self.height = bottom - top
#elseif os(macOS)
self.height = top - bottom
#endif
}
mutating func set(left:CGFloat,right:CGFloat) {
self.x = left
self.width = right - left
}
}
public extension CGRect {
func width(_ w:CGFloat) -> CGRect {
var new = self
new.width = w
return new
}
func height(_ h:CGFloat) -> CGRect {
var new = self
new.height = h
return new
}
func x(_ x:CGFloat) -> CGRect {
var new = self
new.x = x
return new
}
func y(_ y:CGFloat) -> CGRect {
var new = self
new.y = y
return new
}
}
//Rect inside
public extension CGRect {
#if os(iOS)
func top(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self
let h = new.height.calculate(ratio)
if h >= 0 {
new.height = h
} else {
let height = self.height + h
new.height = height >= 0 ? height : 0
}
return new
}
func top(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) ->CGRect {
var new = self.top(ratio)
new.y += self.height.calculate(offset)
return new
}
func bottom(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self.top(ratio)
new.bottom = self.bottom
return new
}
func bottom(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) -> CGRect {
var new = self.top(ratio)
new.bottom = self.bottom - self.height.calculate(offset)
return new
}
#elseif os(macOS)
func top(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self.bottom(ratio)
new.top = self.top
return new
}
func top(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) ->CGRect {
var new = self.top(ratio)
new.top = self.top - self.height.calculate(offset)
return new
}
func bottom(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self
let h = new.height.calculate(ratio)
if h >= 0 {
new.height = h
} else {
let height = self.height + h
new.height = height >= 0 ? height : 0
}
return new
}
func bottom(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) -> CGRect {
var new = self.bottom(ratio)
new.bottom = self.bottom + self.height.calculate(offset)
return new
}
#endif
func left(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self
let w = self.width.calculate(ratio)
if w >= 0 {
new.width = w
} else {
let width = self.width + w
new.width = width >= 0 ? width : 0
}
return new
}
func left(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) -> CGRect {
var new = self.left(ratio)
new.x += self.width.calculate(offset)
return new
}
func right(_ ratio:CGRect.CalculateType) -> CGRect {
var new = self.left(ratio)
new.right = self.right
return new
}
func right(_ ratio:CGRect.CalculateType,offset:CGRect.CalculateType) -> CGRect {
var new = self.left(ratio)
new.right = self.right - self.width.calculate(offset)
return new
}
func center(xs:CGRect.CalculateType,ys:CGRect.CalculateType) -> CGRect {
var new = self.left(xs).top(ys)
new.center = self.center
return new
}
}
//Rect margin outside
public extension CGRect {
func mLeft(_ v:CalculateType, offset: CalculateType) -> CGRect {
var new = self.left(v)
let o = self.width.calculate(offset)
new.right = self.left - o
return new
}
func mTop(_ v:CalculateType, offset: CalculateType) -> CGRect {
var new = self.top(v)
#if os(iOS)
new.bottom = self.top - self.height.calculate(offset)
#elseif os(macOS)
new.bottom = self.top + self.height.calculate(offset)
#endif
return new
}
func mRight(_ v:CalculateType, offset: CalculateType) -> CGRect {
var new = self.left(v)
new.left = self.right + self.width.calculate(offset)
return new
}
func mBottom(_ v:CalculateType, offset: CalculateType) -> CGRect {
var new = self.top(v)
#if os(iOS)
new.top = self.bottom + self.height.calculate(offset)
#elseif os(macOS)
new.top = self.bottom - self.height.calculate(offset)
#endif
return new
}
func mLeft(_ v:CalculateType) -> CGRect {
var new = self.left(v)
new.right = self.left
return new
}
func mTop(_ v:CalculateType) -> CGRect {
var new = self.top(v)
new.bottom = self.top
return new
}
func mRight(_ v:CalculateType) -> CGRect {
var new = self.left(v)
new.left = self.right
return new
}
func mBottom(_ v:CalculateType) -> CGRect {
var new = self.top(v)
new.top = self.bottom
return new
}
}
//Rect inside
public extension CGRect {
func top(_ v:CGFloat = 0.5) -> CGRect {
return self.top(CalculateType(floatLiteral: v))
}
func top(_ v:CGFloat = 0.5, offset:CGFloat) ->CGRect {
return self.top(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func bottom(_ v:CGFloat = 0.5) -> CGRect {
return self.bottom(CalculateType(floatLiteral: v))
}
func bottom(_ v:CGFloat = 0.5, offset:CGFloat) -> CGRect {
return self.bottom(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func left(_ v:CGFloat = 0.5) -> CGRect {
return self.left(CalculateType(floatLiteral: v))
}
func left(_ v:CGFloat = 0.5, offset:CGFloat) -> CGRect {
return self.left(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func right(_ v:CGFloat = 0.5) -> CGRect {
return self.right(CalculateType(floatLiteral: v))
}
func right(_ v:CGFloat = 0.5, offset:CGFloat) -> CGRect {
return self.right(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func center(xs:CGFloat = 0.5, ys:CGFloat = 0.5) -> CGRect {
return self.center(xs: CalculateType(floatLiteral: xs), ys: CalculateType(floatLiteral: ys))
}
}
//Rect margin outside
public extension CGRect {
func mTop(_ v:CGFloat = 1.0) -> CGRect {
return self.mTop(CalculateType(floatLiteral: v))
}
func mTop(_ v:CGFloat = 1.0,offset:CGFloat) ->CGRect {
return self.mTop(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func mBottom(_ v:CGFloat = 1.0) -> CGRect {
return self.mBottom(CalculateType(floatLiteral: v))
}
func mBottom(_ v:CGFloat = 1.0,offset:CGFloat) -> CGRect {
return self.mBottom(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func mLeft(_ v:CGFloat = 1.0) -> CGRect {
return self.mLeft(CalculateType(floatLiteral: v))
}
func mLeft(_ v:CGFloat = 1.0,offset:CGFloat) -> CGRect {
return self.mLeft(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
func mRight(_ v:CGFloat = 1.0) -> CGRect {
return self.mRight(CalculateType(floatLiteral: v))
}
func mRight(_ v:CGFloat = 1.0,offset:CGFloat) -> CGRect {
return self.mRight(CalculateType(floatLiteral: v), offset: CalculateType(floatLiteral: offset))
}
}