-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutBuilder.swift
More file actions
529 lines (439 loc) · 20.7 KB
/
LayoutBuilder.swift
File metadata and controls
529 lines (439 loc) · 20.7 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/** Helper class for creating layouts */
import UIKit
class LayoutBuilder : NSObject {
/** Use this identifier to refer to parent view in extended constraints (you can change it) */
static var parentViewKey = "parent"
/**
This identifier is used in some methods like fillWithView
TODO: deprecate and force to use a key or generate sequential keys like LinearBuilder
Note: LinearBuilder may then use the key generation in LayoutBuilder (is it a good idea?)
*/
static var defaultViewKey = "view"
static let XtConstraintPrefix = "X"
static let DefaultPriority: UILayoutPriority = UILayoutPriority(rawValue: 0)
private static let TopGuideKey = "TOP_GUIDE"
private static let BottomGuideKey = "BOTTOM_GUIDE"
let view: UIView
private var viewOfController = false // view is the UIView of a UIViewController
private var subviews = [String:AnyObject]() // may contain UILayoutGuide objects
var metrics = [String:Float]()
var displayRandomColors = false
/** The subviews and constraints will be added to the given `view` */
public init(view: UIView) {
self.view = view
}
/**
* Inits object with a the view of a controller.
* It handles the top and bottom guides of controller view.
*/
public convenience init(controller: UIViewController) {
self.init(view: controller.view)
viewOfController = true
// TODO: we use these guides for constraints but they are deprecated; now we should use safeAreaLayoutGuide
subviews[LayoutBuilder.TopGuideKey] = controller.topLayoutGuide
subviews[LayoutBuilder.BottomGuideKey] = controller.bottomLayoutGuide
}
func viewKey(_ view: UIView) -> String? {
// Looks for entries with the given view, takes the key of first one
// See: https://stackoverflow.com/a/27218964/1121497
return subviews.filter { $1 as? UIView == view }.first?.0
}
func view(key: String) -> UIView? {
let view = subviews[key]
return view as? UIView
}
/** Inits object with a new UIView as main view */
public convenience override init() {
self.init(view: UIView())
}
// Add views
@discardableResult
func fillWithView(_ view: UIView, key: String = LayoutBuilder.defaultViewKey, margin: Float = 0) -> LayoutBuilder {
let m = CGFloat(margin)
return fillWithView(view, key: key, margins: UIEdgeInsetsMake(m, m, m, m))
}
@discardableResult
func fillWithView(_ view: UIView, key: String = LayoutBuilder.defaultViewKey, margins:UIEdgeInsets) -> LayoutBuilder
{
// In the view of a controller, we use layout guides as vertical anchors
let topKey = viewOfController ? "[\(LayoutBuilder.TopGuideKey)]" : "|"
let bottomKey = "|" // always "|" so in iPhone X we reach the bottom
return addView(view, key: key)
.withMetrics([
"left":Float(margins.left), "right":Float(margins.right),
"top":Float(margins.top), "bottom":Float(margins.bottom)])
.addConstraints([
"H:|-(left)-[\(key)]-(right)-|",
"V:\(topKey)-(top)-[\(key)]-(bottom)-\(bottomKey)"])
}
/**
* Avoid this method. Margins are not fixed so the view may to grow too much if container size is not properly constrained.
* Use the method where the center axis is indicated, so you only center on the axis that is constrained (usually horizontal).
*/
@available(*, deprecated)
@discardableResult
func addViewCentered(_ view: UIView, key: String = LayoutBuilder.defaultViewKey) -> LayoutBuilder
{
let parentKey = LayoutBuilder.parentViewKey
return addView(view, key: key).addConstraints([
"X:\(key).centerX == \(parentKey).centerX",
"H:|-(>=0)-[\(key)]-(>=0)-|",
"X:\(key).centerY == \(parentKey).centerY",
"V:|-(>=0)-[\(key)]-(>=0)-|"
])
}
@discardableResult
func addViewCentered(_ view: UIView, axis: UILayoutConstraintAxis, key: String = LayoutBuilder.defaultViewKey) -> LayoutBuilder
{
let parentKey = LayoutBuilder.parentViewKey
let centerProp = axis == .horizontal ? "centerX" : "centerY"
let centerAxisLetter = axis == .horizontal ? "H" : "V"
let crossAxisLetter = axis == .horizontal ? "V" : "H"
return addView(view, key: key).addConstraints([
"X:\(key).\(centerProp) == \(parentKey).\(centerProp)",
"\(centerAxisLetter):|-(>=0)-[\(key)]-(>=0)-|",
"\(crossAxisLetter):|[\(key)]|"
])
}
// NOTE: this method could be avoided by declaring a default addToParent: Bool = false
// but we leave this method so from ObjC we don't have to pass `addToParent` all the time.
@discardableResult
func addViews(_ views: [String:UIView]) -> LayoutBuilder {
return addViews(views, addToParent: true)
}
/**
* Adds views to the main view.
* Use `addToParent: false` if you just want to use the views in the constraints but not add them to main view.
*/
@discardableResult
func addViews(_ views: [String:UIView], addToParent:Bool) -> LayoutBuilder {
for (key,view) in views {
addView(view, key:key, addToParent:addToParent)
}
return self
}
@discardableResult
func addView(_ view:UIView, key:String, addToParent:Bool = true) -> LayoutBuilder {
subviews[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
if displayRandomColors {
view.backgroundColor = LayoutBuilder.getRandomColorWithAlpha(0.4)
}
if addToParent {
self.view.addSubview(view)
}
return self
}
// Remove views
@discardableResult
func removeAllViews() -> LayoutBuilder
{
return removeViewsWithKeys(Array(subviews.keys))
}
@discardableResult
func removeViewsWithKeys(_ keysToRemove: [String]) -> LayoutBuilder
{
for key in keysToRemove {
if let view = subviews[key] as? UIView {
view.removeFromSuperview()
subviews.removeValue(forKey: key)
}
}
return self
}
// Scroll view
/**
* Configures a UIScrollView you have added before (with given key) to scroll in the specified axis (direction).
* The contentView is configured to fill the scroll view and to be as wide as the layout main view.
*/
@discardableResult
func configureScrollView(_ scrollViewKey: String, axis: UILayoutConstraintAxis, contentView: UIView) -> LayoutBuilder
{
let scrollView = subviews[scrollViewKey] as! UIScrollView // you should have added it
LayoutBuilder(view: scrollView).fillWithView(contentView)
let crossAxisLetter: String = axis == .vertical ? "H" : "V"
// Make the scrollView's contentView match cross axis size with main view (main view contains scrollView)
// For example, in a vertical scrollView, the contentView has the same width (horizontal size) as the main view
let constraint = "\(crossAxisLetter):|[content(==main)]|"
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraint,
options: [], metrics: nil, views: ["content":contentView, "main":self.view]))
return self
}
/**
* Convenience method when you want a UIScrollView to fill the main view and scroll in the given axis.
* The contentView is the content of the scroll view.
*/
@discardableResult
func fillWithScrollView(_ axis: UILayoutConstraintAxis, contentView: UIView) -> LayoutBuilder
{
return fillWithScrollView(axis, contentView: contentView, scrollView: UIScrollView())
}
/**
* Convenience method when you want a UIScrollView to fill the main view and scroll in the given axis.
* The contentView is the content of the scroll view.
*/
@discardableResult
func fillWithScrollView(_ axis: UILayoutConstraintAxis, contentView: UIView, scrollView: UIScrollView) -> LayoutBuilder
{
fillWithView(scrollView) // scrollView is added with defaultViewKey
return configureScrollView(LayoutBuilder.defaultViewKey, axis: axis, contentView:contentView)
}
// Various
@discardableResult
func setupView(_ action: (UIView) -> ()) -> LayoutBuilder {
action(view)
return self
}
@discardableResult
func withRandomColors(_ displayRandomColors: Bool = true) -> LayoutBuilder {
self.displayRandomColors = displayRandomColors
return self
}
@discardableResult
func withMetrics(_ metrics: [String:Float]) -> LayoutBuilder {
self.metrics = metrics
return self
}
// Hugging and compression - http://stackoverflow.com/questions/33842797
/** Tries to simulate Android's wrap_content by setting hugging and compression to view and children (recursively) */
@discardableResult
func setWrapContent(_ viewKey:String, axis: UILayoutConstraintAxis) -> LayoutBuilder {
let view = findViewFromKey(viewKey)
return setWrapContent(view: view, axis: axis)
}
/** Tries to simulate Android's wrap_content by setting hugging and compression to view and children (recursively) */
@discardableResult
func setWrapContent(view:UIView, axis: UILayoutConstraintAxis) -> LayoutBuilder {
setHugging(view: view, priority: .defaultHigh, axis: axis)
setResistance(view: view, priority: .required, axis: axis)
return self
}
/** Sets hugging priority to view with given key and children (recursively) */
@discardableResult
func setHugging(_ viewKey:String, priority:UILayoutPriority, axis: UILayoutConstraintAxis) -> LayoutBuilder {
let view = findViewFromKey(viewKey)
return setHugging(view: view, priority: priority, axis: axis)
}
/** Sets hugging priority to view and children (recursively) */
@discardableResult
func setHugging(view:UIView, priority:UILayoutPriority, axis: UILayoutConstraintAxis) -> LayoutBuilder {
view.setContentHuggingPriority(priority, for: axis)
for v in view.subviews {
setHugging(view: v, priority: priority, axis: axis) // recursive
}
return self
}
/** Sets compression resistance priority to view with given key and children (recursively) */
@discardableResult
func setResistance(_ viewKey:String, priority:UILayoutPriority, axis: UILayoutConstraintAxis) -> LayoutBuilder {
let view = findViewFromKey(viewKey)
return setResistance(view: view, priority: priority, axis: axis)
}
/** Sets compression resistance priority to view and children (recursively) */
@discardableResult
func setResistance(view:UIView, priority:UILayoutPriority, axis: UILayoutConstraintAxis) -> LayoutBuilder {
view.setContentCompressionResistancePriority(priority, for: axis)
for v in view.subviews {
setResistance(view: v, priority: priority, axis: axis) // recursive
}
return self
}
// Constraints
// NOTE: this method could be avoided by declaring a default priority:UILayoutPriority = LayoutBuilder.DefaultPriority
// but we leave this method so from ObjC we don't have to pass `priority` all the time.
@discardableResult
func addConstraints(_ cs:[String]) -> LayoutBuilder {
return addConstraints(cs, priority: LayoutBuilder.DefaultPriority)
}
@discardableResult
func addConstraints(_ cs:[String], priority:UILayoutPriority) -> LayoutBuilder {
_ = addAndGetConstraints(cs, priority: priority)
return self
}
@discardableResult
func addConstraint(_ c:String, priority:UILayoutPriority = LayoutBuilder.DefaultPriority) -> LayoutBuilder {
_ = addAndGetConstraint(c, priority: priority)
return self
}
/** Adds a constraint (with given priority) and returns the generated NSLayoutConstraint objects */
func addAndGetConstraints(_ cs:[String], priority:UILayoutPriority = LayoutBuilder.DefaultPriority) -> [NSLayoutConstraint]
{
return cs.map { addAndGetConstraint($0, priority: priority) }.reduce([], { (current, next) in current + next })
}
/** Adds a constraint (with given priority) and returns the generated NSLayoutConstraint objects */
func addAndGetConstraint(_ c:String, priority:UILayoutPriority = LayoutBuilder.DefaultPriority) -> [NSLayoutConstraint]
{
let realConstraints = parseConstraint(c)
if priority != LayoutBuilder.DefaultPriority {
for realConstraint in realConstraints {
realConstraint.priority = priority;
}
}
self.view.addConstraints(realConstraints)
return realConstraints
}
/** Parses a constraint, either a normal Visual Format constraint (H,V) or an extended (X) constraint */
func parseConstraint(_ c:String) -> [NSLayoutConstraint] {
if c.hasPrefix(LayoutBuilder.XtConstraintPrefix) {
return parseXtConstraint(c)
}
else {
return NSLayoutConstraint.constraints(withVisualFormat: c,
options: NSLayoutFormatOptions(), metrics: metrics, views: subviews)
}
}
/** Parses an extended (X) constraint */
private func parseXtConstraint(_ constraint: String) -> [NSLayoutConstraint]
{
let results = LayoutBuilder.xtConstraintRegex.matches(in: constraint,
options: NSRegularExpression.MatchingOptions(), range: NSMakeRange(0, constraint.count))
if results.count != 1 {
fatalError("Invalid constraint: \(constraint)")
}
let match: NSTextCheckingResult = results[0]
if match.numberOfRanges != 10 {
dumpMatch(match, forString: constraint)
fatalError("Invalid constraint: \(constraint)")
}
let item1Key: String = constraint.substring(match.range(at: 1))
let attr1Str: String = constraint.substring(match.range(at: 2))
let relationStr: String = constraint.substring(match.range(at: 3))
let item2Key: String = constraint.substring(match.range(at: 4))
let attr2Str: String = constraint.substring(match.range(at: 5))
let item1: AnyObject = findViewFromKey(item1Key)
let item2: AnyObject = findViewFromKey(item2Key)
let attr1: NSLayoutAttribute = parseAttribute(attr1Str)
let attr2: NSLayoutAttribute = parseAttribute(attr2Str)
let relation: NSLayoutRelation = parseRelation(relationStr)
var multiplier: Float = 1
if match.range(at: 6).location != NSNotFound {
let operation: String = constraint.substring(match.range(at: 6))
let multiplierValue: String = constraint.substring(match.range(at: 7))
multiplier = getFloat(multiplierValue)
if (operation == "/") { // TODO: deprecate this, I think it leads to weird behaviour sometimes
multiplier = 1 / multiplier
}
}
var constant: Float = 0
if match.range(at: 8).location != NSNotFound {
let operation: String = constraint.substring(match.range(at: 8))
let constantValue: String = constraint.substring(match.range(at: 9))
constant = getFloat(constantValue)
if (operation == "-") {
constant = -constant
}
}
let c: NSLayoutConstraint = NSLayoutConstraint(
item: item1, attribute: attr1, relatedBy: relation,
toItem: item2, attribute: attr2, multiplier: CGFloat(multiplier), constant: CGFloat(constant))
return [c]
}
/** `value` may be the name of a metric, or a literal float value */
private func getFloat(_ value: String) -> Float
{
if stringIsIdentifier(value) {
if let metric = metrics[value] {
return metric
}
else {
let reason = "Metric `\(value)` was not provided"
fatalError(reason)
}
}
else {
return (value as NSString).floatValue
}
}
/** Returns true if `value` starts with a valid identifier character */
private func stringIsIdentifier(_ value: String) -> Bool {
let c = value[value.startIndex] // gets first char of string
return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || c == "_"
}
private func findViewFromKey(_ key: String) -> UIView
{
if (key == LayoutBuilder.parentViewKey) {
return self.view
}
else {
if let view = subviews[key] as? UIView {
return view
}
else {
let reason = "No view was added with key `\(key)`"
fatalError(reason)
}
}
}
private static let attributes : [String:NSLayoutAttribute] = [
"left": .left, "right": .right, "top": .top, "bottom": .bottom,
"leading": .leading, "trailing": .trailing,
"width": .width, "height": .height,
"centerX": .centerX, "centerY": .centerY,
"baseline": .lastBaseline, // for one-line texts first/last are the same
"lastBaseline": .lastBaseline, "firstBaseline": .firstBaseline,
"leftMargin": .leftMargin, "rightMargin": .rightMargin,
"topMargin": .topMargin, "bottomMargin": .bottomMargin,
"leadingMargin": .leadingMargin, "trailingMargin": .trailingMargin,
"centerXWithinMargins": .centerXWithinMargins, "centerYWithinMargins": .centerYWithinMargins]
private func parseAttribute(_ attrStr: String) -> NSLayoutAttribute
{
if let value = LayoutBuilder.attributes[attrStr] {
return value
}
else {
let reason = "Attribute `\(attrStr)` is not valid. Use one of: \(LayoutBuilder.attributes.keys)"
fatalError(reason)
}
}
private static let relations : [String:NSLayoutRelation] = [
"==": .equal, ">=": .greaterThanOrEqual, "<=": .lessThanOrEqual]
private func parseRelation(_ relationStr: String) -> NSLayoutRelation
{
if let value = LayoutBuilder.relations[relationStr] {
return value
}
else {
let reason = "Relation `\(relationStr)` is not valid. Use one of: \(LayoutBuilder.relations.keys)"
fatalError(reason)
}
}
private static var xtConstraintRegex = LayoutBuilder.prepareRegex()
private static func prepareRegex() -> NSRegularExpression {
// C identifier
let identifier: String = "[_a-zA-Z][_a-zA-Z0-9]{0,30}"
// VIEW_KEY.ATTR or (use LayoutBuilder.parentViewKey as VIEW_KEY to refer to parent view)
let attr: String = "(\(identifier))\\.(\(identifier))"
// Relations taken from NSLayoutRelation
let relation: String = "([=><]+)"
// float number e.g. "12", "12.", "2.56"
let number: String = "\\d+\\.?\\d*"
// Value (indentifier or number)
let value: String = "(?:(?:\(identifier))|(?:\(number)))"
// e.g. "*5" or "/ 27.3" or "* 200"
let multiplier: String = "([*/]) *(\(value))"
// e.g. "+ 2." or "- 56" or "-7.5"
let constant: String = "([+-]) *(\(value))"
let pattern: String = "^\(XtConstraintPrefix): *\(attr) *\(relation) *\(attr) *(?:\(multiplier))? *(?:\(constant))?$"
return try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
}
static func getRandomColorWithAlpha(_ alpha: CGFloat) -> UIColor
{
let red = arc4random_uniform(256)
let green = arc4random_uniform(256)
let blue = arc4random_uniform(256)
return UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
}
private func dumpMatch(_ match: NSTextCheckingResult, forString str: String)
{
for i in 0 ..< match.numberOfRanges {
let range = match.range(at: i)
if range.location != NSNotFound {
let part = str.substring(range)
print("Range \(i): \(part)")
}
else {
print("Range \(i) NOT FOUND")
}
}
}
}