/
UIEdgeInsetsExtensions.swift
133 lines (117 loc) · 5.16 KB
/
UIEdgeInsetsExtensions.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
//
// UIEdgeInsetsExtensions.swift
// SwifterSwift
//
// Created by Luciano Almeida on 15/06/18.
// Copyright © 2018 SwifterSwift
//
#if canImport(UIKit)
import UIKit
// MARK: - Properties
public extension UIEdgeInsets {
/// SwifterSwift: Return the vertical insets. The vertical insets is composed by top + bottom.
///
var vertical: CGFloat {
// Source: https://github.com/MessageKit/MessageKit/blob/master/Sources/SwifterSwift/UIEdgeInsets%2BExtensions.swift
return top + bottom
}
/// SwifterSwift: Return the horizontal insets. The horizontal insets is composed by left + right.
///
var horizontal: CGFloat {
// Source: https://github.com/MessageKit/MessageKit/blob/master/Sources/SwifterSwift/UIEdgeInsets%2BExtensions.swift
return left + right
}
}
// MARK: - Methods
public extension UIEdgeInsets {
/// SwifterSwift: Creates an `UIEdgeInsets` with the inset value applied to all (top, bottom, right, left)
///
/// - Parameter inset: Inset to be applied in all the edges.
init(inset: CGFloat) {
self.init(top: inset, left: inset, bottom: inset, right: inset)
}
/// SwifterSwift: Creates an `UIEdgeInsets` with the horizontal value equally divided and applied to right and left.
/// And the vertical value equally divided and applied to top and bottom.
///
///
/// - Parameter horizontal: Inset to be applied to right and left.
/// - Parameter vertical: Inset to be applied to top and bottom.
init(horizontal: CGFloat, vertical: CGFloat) {
self.init(top: vertical/2, left: horizontal/2, bottom: vertical/2, right: horizontal/2)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and top offset.
///
/// - Parameters:
/// - top: Offset to be applied in to the top edge.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(top: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: self.top + top, left: left, bottom: bottom, right: right)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and left offset.
///
/// - Parameters:
/// - left: Offset to be applied in to the left edge.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(left: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: top, left: self.left + left, bottom: bottom, right: right)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and bottom offset.
///
/// - Parameters:
/// - bottom: Offset to be applied in to the bottom edge.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(bottom: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: top, left: left, bottom: self.bottom + bottom, right: right)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and right offset.
///
/// - Parameters:
/// - right: Offset to be applied in to the right edge.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(right: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: top, left: left, bottom: bottom, right: self.right + right)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and horizontal value equally divided and applied to right offset and left offset.
///
/// - Parameters:
/// - horizontal: Offset to be applied to right and left.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(horizontal: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: top, left: left + horizontal/2, bottom: bottom, right: right + horizontal/2)
}
/// SwifterSwift: Creates an `UIEdgeInsets` based on current value and vertical value equally divided and applied to top and bottom.
///
/// - Parameters:
/// - vertical: Offset to be applied to top and bottom.
/// - Returns: UIEdgeInsets offset with given offset.
func insetBy(vertical: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: top + vertical/2, left: left, bottom: bottom + vertical/2, right: right)
}
}
// MARK: - Operators
public extension UIEdgeInsets {
/// SwifterSwift: Add all the properties of two `UIEdgeInsets` to create their addition.
///
/// - Parameters:
/// - lhs: The left-hand expression
/// - rhs: The right-hand expression
/// - Returns: A new `UIEdgeInsets` instance where the values of `lhs` and `rhs` are added together.
static func + (_ lhs: UIEdgeInsets, _ rhs: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsets(top: lhs.top + rhs.top,
left: lhs.left + rhs.left,
bottom: lhs.bottom + rhs.bottom,
right: lhs.right + rhs.right)
}
/// SwifterSwift: Add all the properties of two `UIEdgeInsets` to the left-hand instance.
///
/// - Parameters:
/// - lhs: The left-hand expression to be mutated
/// - rhs: The right-hand expression
static func += (_ lhs: inout UIEdgeInsets, _ rhs: UIEdgeInsets) {
lhs.top += rhs.top
lhs.left += rhs.left
lhs.bottom += rhs.bottom
lhs.right += rhs.right
}
}
#endif