ConstraintViewDSL.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS) || os(tvOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. public struct ConstraintViewDSL {
  29. public var left: ConstraintItem {
  30. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Left)
  31. }
  32. public var top: ConstraintItem {
  33. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Top)
  34. }
  35. public var right: ConstraintItem {
  36. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Right)
  37. }
  38. public var bottom: ConstraintItem {
  39. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Bottom)
  40. }
  41. public var leading: ConstraintItem {
  42. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Leading)
  43. }
  44. public var trailing: ConstraintItem {
  45. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Trailing)
  46. }
  47. public var width: ConstraintItem {
  48. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Width)
  49. }
  50. public var height: ConstraintItem {
  51. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Height)
  52. }
  53. public var centerX: ConstraintItem {
  54. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterX)
  55. }
  56. public var centerY: ConstraintItem {
  57. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterY)
  58. }
  59. public var baseline: ConstraintItem {
  60. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Baseline)
  61. }
  62. @available(iOS 8.0, *)
  63. public var firstBaseline: ConstraintItem {
  64. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.FirstBaseline)
  65. }
  66. @available(iOS 8.0, *)
  67. public var leftMargin: ConstraintItem {
  68. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.LeftMargin)
  69. }
  70. @available(iOS 8.0, *)
  71. public var topMargin: ConstraintItem {
  72. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.TopMargin)
  73. }
  74. @available(iOS 8.0, *)
  75. public var rightMargin: ConstraintItem {
  76. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.RightMargin)
  77. }
  78. @available(iOS 8.0, *)
  79. public var bottomMargin: ConstraintItem {
  80. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.BottomMargin)
  81. }
  82. @available(iOS 8.0, *)
  83. public var leadingMargin: ConstraintItem {
  84. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.LeadingMargin)
  85. }
  86. @available(iOS 8.0, *)
  87. public var trailingMargin: ConstraintItem {
  88. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.TrailingMargin)
  89. }
  90. @available(iOS 8.0, *)
  91. public var centerXWithinMargins: ConstraintItem {
  92. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterXWithinMargins)
  93. }
  94. @available(iOS 8.0, *)
  95. public var centerYWithinMargins: ConstraintItem {
  96. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterYWithinMargins)
  97. }
  98. public var edges: ConstraintItem {
  99. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Edges)
  100. }
  101. public var size: ConstraintItem {
  102. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Size)
  103. }
  104. public var center: ConstraintItem {
  105. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Center)
  106. }
  107. @available(iOS 8.0, *)
  108. public var margins: ConstraintItem {
  109. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Margins)
  110. }
  111. @available(iOS 8.0, *)
  112. public var centerWithinMargins: ConstraintItem {
  113. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterWithinMargins)
  114. }
  115. public func prepareConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) -> [Constraint] {
  116. return ConstraintMaker.prepareConstraints(view: self.view, closure: closure)
  117. }
  118. public func makeConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  119. ConstraintMaker.makeConstraints(view: self.view, closure: closure)
  120. }
  121. public func remakeConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  122. ConstraintMaker.remakeConstraints(view: self.view, closure: closure)
  123. }
  124. public func updateConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  125. ConstraintMaker.updateConstraints(view: self.view, closure: closure)
  126. }
  127. public func removeConstraints() {
  128. ConstraintMaker.removeConstraints(view: self.view)
  129. }
  130. public var label: String? {
  131. get {
  132. return objc_getAssociatedObject(self.view, &labelKey) as? String
  133. }
  134. set {
  135. objc_setAssociatedObject(self.view, &labelKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
  136. }
  137. }
  138. internal let view: ConstraintView
  139. internal init(view: ConstraintView) {
  140. self.view = view
  141. }
  142. internal var installedLayoutConstraints: [LayoutConstraint] {
  143. return objc_getAssociatedObject(self.view, &installedLayoutConstraintsKey) as? [LayoutConstraint] ?? []
  144. }
  145. internal func appendInstalledLayoutConstraints(_ layoutConstraints: [LayoutConstraint]) {
  146. var newValue = self.installedLayoutConstraints
  147. newValue += layoutConstraints
  148. objc_setAssociatedObject(self.view, &installedLayoutConstraintsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  149. }
  150. internal func removeInstalledLayoutConstraints(_ layoutConstraints: [LayoutConstraint]) {
  151. let newValue = self.installedLayoutConstraints.filter { !layoutConstraints.contains($0) }
  152. objc_setAssociatedObject(self.view, &installedLayoutConstraintsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  153. }
  154. }
  155. private var labelKey: UInt8 = 0
  156. private var installedLayoutConstraintsKey: UInt8 = 0