Skip to content

Instantly share code, notes, and snippets.

@erica
Last active December 12, 2022 05:05
Show Gist options
  • Save erica/161a4b992d8b947504ca67acef5e26be to your computer and use it in GitHub Desktop.
Save erica/161a4b992d8b947504ca67acef5e26be to your computer and use it in GitHub Desktop.
import Cocoa
public struct UIEdgeInsets {
public var top: CGFloat // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
public var left: CGFloat
public var bottom: CGFloat
public var right: CGFloat
public init() {
(top, left, bottom, right) = (0, 0, 0, 0)
}
public init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
(self.top, self.left, self.right, self.bottom) = (top, left, right, bottom)
}
}
extension UIEdgeInsets: ExpressibleByDictionaryLiteral {
public typealias Key = WritableKeyPath<UIEdgeInsets, CGFloat>
public typealias Value = CGFloat
public init(dictionaryLiteral elements: (WritableKeyPath<UIEdgeInsets, CGFloat>, CGFloat)...) {
self = UIEdgeInsets()
for (inset, value) in elements {
self[keyPath: inset] = value
}
}
public var vertical: CGFloat {
get { return 0 } // meaningless but not fatal
set { (top, bottom) = (newValue, newValue) }
}
public var horizontal: CGFloat {
get { return 0 } // meaningless but not fatal
set { (left, right) = (newValue, newValue) }
}
public var all: CGFloat {
get { return 0 } // meaningless but not fatal
set { (vertical, horizontal) = (newValue, newValue) }
}
}
let insets: UIEdgeInsets = [\.vertical: 8, \.horizontal: 20]
print(insets) // (l: 8.0 , r: 20.0, t: 8.0, b: 20.0)
let insets2: UIEdgeInsets = [\.all: 8]
print(insets2) // (l: 8.0 , r: 8.0, t: 8.0, b: 8.0)
let insets3: UIEdgeInsets = [\.left: 8]
print(insets3) // (l: 8.0, r: 0.0, t: 0.0, b: 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment