-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVisualFormat.swift
423 lines (363 loc) · 14 KB
/
VisualFormat.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
// VisualFormat.swift
// SwiftVisualFormat
//
// Created by Bridger Maxwell on 8/1/14.
// Copyright (c) 2014 Bridger Maxwell. All rights reserved.
//
#if os(OSX)
import AppKit
public typealias ALVFView = NSView
#elseif os(iOS)
import UIKit
public typealias ALVFView = UIView
#endif
extension ALVFView {
public func addVerticalConstraints(constraintAble: [ConstraintAble]) {
self.addConstraints(verticalConstraints(constraintAble))
}
public func addHorizontalConstraints(constraintAble: [ConstraintAble]) {
self.addConstraints(horizontalConstraints(constraintAble))
}
}
@objc public protocol ConstraintAble {
func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint];
}
public func constraints(axis: UILayoutConstraintAxis, constraintAble: [ConstraintAble]) -> [NSLayoutConstraint] {
return constraintAble[0].toConstraints(axis)
}
public func horizontalConstraints(constraintAble: [ConstraintAble]) -> [NSLayoutConstraint] {
return constraints(.Horizontal, constraintAble: constraintAble)
}
public func verticalConstraints(constraintAble: [ConstraintAble]) -> [NSLayoutConstraint] {
return constraints(.Vertical, constraintAble: constraintAble)
}
@objc public protocol ViewContainingToken : ConstraintAble {
var firstView: ALVFView? { get }
var lastView: ALVFView? { get }
}
protocol ConstantToken {
var ALConstant: CGFloat { get }
}
// This is half of a space constraint, [view]-space
class ViewAndSpaceToken : NSObject {
let view: ViewContainingToken
let space: ConstantToken
let relation: NSLayoutRelation
init(view: ViewContainingToken, space: ConstantToken, relation: NSLayoutRelation) {
self.view = view
self.space = space
self.relation = relation
}
}
// This is half of a space constraint, |-5
class LeadingSuperviewAndSpaceToken : NSObject {
let space: ConstantToken
let relation: NSLayoutRelation
init(space: ConstantToken, relation: NSLayoutRelation) {
self.space = space
self.relation = relation
}
}
// This is half of a space constraint, 5-|
class TrailingSuperviewAndSpaceToken : NSObject {
let space: ConstantToken
init(space: ConstantToken) {
self.space = space
}
}
// [view]-5-[view2]
class SpacedViewsConstraintToken: NSObject, ConstraintAble, ViewContainingToken {
let leadingView: ViewContainingToken
let trailingView: ViewContainingToken
let space: ConstantToken
init(leadingView: ViewContainingToken, trailingView: ViewContainingToken, space: ConstantToken) {
self.leadingView = leadingView
self.trailingView = trailingView
self.space = space
}
var firstView: UIView? {
get {
return self.leadingView.firstView
}
}
var lastView: UIView? {
get {
return self.trailingView.lastView
}
}
func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
if let leadingView = self.leadingView.lastView {
if let trailingView = self.trailingView.firstView {
let space = self.space.ALConstant
var leadingAttribute: NSLayoutAttribute!
var trailingAttribute: NSLayoutAttribute!
if (axis == .Horizontal) {
leadingAttribute = .Leading
trailingAttribute = .Trailing
} else {
leadingAttribute = .Top
trailingAttribute = .Bottom
}
var constraints = [NSLayoutConstraint(
item: trailingView, attribute: leadingAttribute,
relatedBy: .Equal,
toItem: leadingView, attribute: trailingAttribute,
multiplier: 1.0, constant: space)]
constraints += self.leadingView.toConstraints(axis)
constraints += self.trailingView.toConstraints(axis)
return constraints
}
}
NSException(name: NSInvalidArgumentException, reason: "This space constraint was between two view items that couldn't fit together. Weird?", userInfo: nil).raise()
return [] // To appease the compiler, which doesn't realize this branch dies
}
}
// [view == 50]
class SizeConstantConstraintToken: NSObject, ConstraintAble, ViewContainingToken {
let view: ALVFView
let size: ConstantToken
let relation: NSLayoutRelation
init(view: ALVFView, size: ConstantToken, relation: NSLayoutRelation) {
self.view = view
self.size = size
self.relation = relation
}
var firstView: ALVFView? {
get {
return self.view.firstView
}
}
var lastView: ALVFView? {
get {
return self.view.lastView
}
}
func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
let constant = self.size.ALConstant
var attribute: NSLayoutAttribute!
if (axis == .Horizontal) {
attribute = .Width
} else {
attribute = .Height
}
let constraint = NSLayoutConstraint(
item: self.view, attribute: attribute,
relatedBy: self.relation,
toItem: nil, attribute: .NotAnAttribute,
multiplier: 1.0, constant: constant)
return [constraint]
}
}
// [view == view2]
class SizeRelationConstraintToken: NSObject, ConstraintAble, ViewContainingToken {
let view: ALVFView
let relatedView: ALVFView
let relation: NSLayoutRelation
init(view: ALVFView, relatedView: ALVFView, relation: NSLayoutRelation) {
self.view = view
self.relatedView = relatedView
self.relation = relation
}
var firstView: ALVFView? {
get {
return self.view.firstView
}
}
var lastView: ALVFView? {
get {
return self.view.lastView
}
}
func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
var attribute: NSLayoutAttribute!
if (axis == .Horizontal) {
attribute = .Width
} else {
attribute = .Height
}
return [ NSLayoutConstraint(
item: self.view, attribute: attribute,
relatedBy: self.relation,
toItem: self.relatedView, attribute: attribute,
multiplier: 1.0, constant: 0) ]
}
}
// |-5-[view]
public class LeadingSuperviewConstraintToken: NSObject, ConstraintAble, ViewContainingToken {
let viewContainer: ViewContainingToken
let space: ConstantToken
init(viewContainer: ViewContainingToken, space: ConstantToken) {
self.viewContainer = viewContainer
self.space = space
}
public var firstView: UIView? {
get {
return nil // No one can bind to our first view, is the superview
}
}
public var lastView: UIView? {
get {
return self.viewContainer.lastView
}
}
public func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
if let view = self.viewContainer.firstView {
let constant = self.space.ALConstant
if let superview = view.superview {
var constraint: NSLayoutConstraint!
if (axis == .Horizontal) {
constraint = NSLayoutConstraint(
item: view, attribute: .Leading,
relatedBy: .Equal,
toItem: superview, attribute: .Leading,
multiplier: 1.0, constant: constant)
} else {
constraint = NSLayoutConstraint(
item: view, attribute: .Top,
relatedBy: .Equal,
toItem: superview, attribute: .Top,
multiplier: 1.0, constant: constant)
}
return viewContainer.toConstraints(axis) + [constraint]
}
NSException(name: NSInvalidArgumentException, reason: "You tried to create a constraint to \(view)'s superview, but it has no superview yet!", userInfo: nil).raise()
}
NSException(name: NSInvalidArgumentException, reason: "This superview bar | was before something that doesn't have a view. Weird?", userInfo: nil).raise()
return [] // To appease the compiler, which doesn't realize this branch dies
}
}
// [view]-5-|
public class TrailingSuperviewConstraintToken: NSObject, ConstraintAble, ViewContainingToken {
let viewContainer: ViewContainingToken
let space: ConstantToken
init(viewContainer: ViewContainingToken, space: ConstantToken) {
self.viewContainer = viewContainer
self.space = space
}
public var firstView: UIView? {
get {
return self.viewContainer.firstView
}
}
public var lastView: UIView? {
get {
return nil // No one can bind to our last view, is the superview
}
}
public func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
if let view = self.viewContainer.lastView {
let constant = self.space.ALConstant
if let superview = view.superview {
var constraint: NSLayoutConstraint!
if (axis == .Horizontal) {
constraint = NSLayoutConstraint(
item: superview, attribute: .Trailing,
relatedBy: .Equal,
toItem: view, attribute: .Trailing,
multiplier: 1.0, constant: constant)
} else {
constraint = NSLayoutConstraint(
item: superview, attribute: .Bottom,
relatedBy: .Equal,
toItem: view, attribute: .Bottom,
multiplier: 1.0, constant: constant)
}
return viewContainer.toConstraints(axis) + [constraint]
}
NSException(name: NSInvalidArgumentException, reason: "You tried to create a constraint to \(view)'s superview, but it has no superview yet!", userInfo: nil).raise()
}
NSException(name: NSInvalidArgumentException, reason: "This superview bar | was after something that doesn't have a view. Weird?", userInfo: nil).raise()
return [] // To appease the compiler, which doesn't realize this branch dies
}
}
let RequiredPriority: Float = 1000 // For some reason, the linker can't find UILayoutPriorityRequired. Not sure what I am doing wrong
prefix operator | {}
prefix public func | (tokenArray: [ViewContainingToken]) -> [LeadingSuperviewConstraintToken] {
// |[view]
return [LeadingSuperviewConstraintToken(viewContainer: tokenArray[0], space: 0)]
}
postfix operator | {}
postfix public func | (tokenArray: [ViewContainingToken]) -> [TrailingSuperviewConstraintToken] {
// [view]|
return [TrailingSuperviewConstraintToken(viewContainer: tokenArray[0], space: 0)]
}
func >= (left: ALVFView, right: ConstantToken) -> SizeConstantConstraintToken {
// [view >= 50]
return SizeConstantConstraintToken(view: left, size: right, relation: .GreaterThanOrEqual)
}
func >= (left: ALVFView, right: ALVFView) -> SizeRelationConstraintToken {
// [view >= view2]
return SizeRelationConstraintToken(view: left, relatedView: right, relation: .GreaterThanOrEqual)
}
func <= (left: ALVFView, right: ConstantToken) -> SizeConstantConstraintToken {
// [view <= 50]
return SizeConstantConstraintToken(view: left, size: right, relation: .LessThanOrEqual)
}
func <= (left: ALVFView, right: ALVFView) -> SizeRelationConstraintToken {
// [view <= view2]
return SizeRelationConstraintToken(view: left, relatedView: right, relation: .LessThanOrEqual)
}
func == (left: ALVFView, right: ConstantToken) -> SizeConstantConstraintToken {
// [view == 50]
return SizeConstantConstraintToken(view: left, size: right, relation: .Equal)
}
func == (left: ALVFView, right: ALVFView) -> SizeRelationConstraintToken {
// [view == view2]
return SizeRelationConstraintToken(view: left, relatedView: right, relation: .Equal)
}
func - (left: [ViewContainingToken], right: ConstantToken) -> ViewAndSpaceToken {
// [view]-5
return ViewAndSpaceToken(view: left[0], space: right, relation: .Equal)
}
func - (left: ViewAndSpaceToken, right: [ViewContainingToken]) -> [SpacedViewsConstraintToken] {
// [view]-5-[view2]
return [SpacedViewsConstraintToken(leadingView: left.view, trailingView: right[0], space: left.space)]
}
func - (left: [ViewContainingToken], right: TrailingSuperviewAndSpaceToken) -> [TrailingSuperviewConstraintToken] {
// [view]-5-|
return [TrailingSuperviewConstraintToken(viewContainer: left[0], space: right.space)]
}
func - (left: LeadingSuperviewAndSpaceToken, right: [ViewContainingToken]) -> [LeadingSuperviewConstraintToken] {
// |-5-[view]
return [LeadingSuperviewConstraintToken(viewContainer: right[0], space: left.space)]
}
postfix operator -| {}
postfix func -| (constant: ConstantToken) -> TrailingSuperviewAndSpaceToken {
// 5-|
return TrailingSuperviewAndSpaceToken(space: constant)
}
prefix operator |- {}
prefix func |- (constant: ConstantToken) -> LeadingSuperviewAndSpaceToken {
// |-5
return LeadingSuperviewAndSpaceToken(space: constant, relation: .Equal)
}
extension ALVFView: ViewContainingToken {
public var firstView: ALVFView? {
get {
return self
}
}
public var lastView: ALVFView? {
get {
return self
}
}
public func toConstraints(axis: UILayoutConstraintAxis) -> [NSLayoutConstraint] {
return []
}
}
extension CGFloat: ConstantToken {
var ALConstant: CGFloat {
get {
return self
}
}
}
extension NSInteger: ConstantToken {
var ALConstant: CGFloat {
get {
return CGFloat(self)
}
}
}