ConstraintViewDSL.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. @available(*, deprecated:0.40.0, message:"Please use migrated .lastBaseline")
  60. public var baseline: ConstraintItem {
  61. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Baseline)
  62. }
  63. @available(iOS 8.0, OSX 10.11, *)
  64. public var lastBaseline: ConstraintItem {
  65. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Baseline)
  66. }
  67. @available(iOS 8.0, OSX 10.11, *)
  68. public var firstBaseline: ConstraintItem {
  69. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.FirstBaseline)
  70. }
  71. @available(iOS 8.0, *)
  72. public var leftMargin: ConstraintItem {
  73. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.LeftMargin)
  74. }
  75. @available(iOS 8.0, *)
  76. public var topMargin: ConstraintItem {
  77. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.TopMargin)
  78. }
  79. @available(iOS 8.0, *)
  80. public var rightMargin: ConstraintItem {
  81. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.RightMargin)
  82. }
  83. @available(iOS 8.0, *)
  84. public var bottomMargin: ConstraintItem {
  85. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.BottomMargin)
  86. }
  87. @available(iOS 8.0, *)
  88. public var leadingMargin: ConstraintItem {
  89. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.LeadingMargin)
  90. }
  91. @available(iOS 8.0, *)
  92. public var trailingMargin: ConstraintItem {
  93. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.TrailingMargin)
  94. }
  95. @available(iOS 8.0, *)
  96. public var centerXWithinMargins: ConstraintItem {
  97. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterXWithinMargins)
  98. }
  99. @available(iOS 8.0, *)
  100. public var centerYWithinMargins: ConstraintItem {
  101. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterYWithinMargins)
  102. }
  103. public var edges: ConstraintItem {
  104. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Edges)
  105. }
  106. public var size: ConstraintItem {
  107. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Size)
  108. }
  109. public var center: ConstraintItem {
  110. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Center)
  111. }
  112. @available(iOS 8.0, *)
  113. public var margins: ConstraintItem {
  114. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.Margins)
  115. }
  116. @available(iOS 8.0, *)
  117. public var centerWithinMargins: ConstraintItem {
  118. return ConstraintItem(target: self.view, attributes: ConstraintAttributes.CenterWithinMargins)
  119. }
  120. public func prepareConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) -> [Constraint] {
  121. return ConstraintMaker.prepareConstraints(view: self.view, closure: closure)
  122. }
  123. public func makeConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  124. ConstraintMaker.makeConstraints(view: self.view, closure: closure)
  125. }
  126. public func remakeConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  127. ConstraintMaker.remakeConstraints(view: self.view, closure: closure)
  128. }
  129. public func updateConstraints(_ closure: @noescape (make: ConstraintMaker) -> Void) {
  130. ConstraintMaker.updateConstraints(view: self.view, closure: closure)
  131. }
  132. public func removeConstraints() {
  133. ConstraintMaker.removeConstraints(view: self.view)
  134. }
  135. public var label: String? {
  136. get {
  137. return objc_getAssociatedObject(self.view, &labelKey) as? String
  138. }
  139. set {
  140. objc_setAssociatedObject(self.view, &labelKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
  141. }
  142. }
  143. public var contentHuggingHorizontalPriority: Float {
  144. get {
  145. return self.view.contentHuggingPriority(for: .horizontal)
  146. }
  147. set {
  148. self.view.setContentHuggingPriority(newValue, for: .horizontal)
  149. }
  150. }
  151. public var contentHuggingVerticalPriority: Float {
  152. get {
  153. return self.view.contentHuggingPriority(for: .vertical)
  154. }
  155. set {
  156. self.view.setContentHuggingPriority(newValue, for: .vertical)
  157. }
  158. }
  159. public var contentCompressionResistanceHorizontalPriority: Float {
  160. get {
  161. return self.view.contentCompressionResistancePriority(for: .horizontal)
  162. }
  163. set {
  164. self.view.setContentHuggingPriority(newValue, for: .horizontal)
  165. }
  166. }
  167. public var contentCompressionResistanceVerticalPriority: Float {
  168. get {
  169. return self.view.contentCompressionResistancePriority(for: .vertical)
  170. }
  171. set {
  172. self.view.setContentCompressionResistancePriority(newValue, for: .vertical)
  173. }
  174. }
  175. internal let view: ConstraintView
  176. internal init(view: ConstraintView) {
  177. self.view = view
  178. }
  179. internal var installedLayoutConstraints: [LayoutConstraint] {
  180. return objc_getAssociatedObject(self.view, &installedLayoutConstraintsKey) as? [LayoutConstraint] ?? []
  181. }
  182. internal func appendInstalledLayoutConstraints(_ layoutConstraints: [LayoutConstraint]) {
  183. var newValue = self.installedLayoutConstraints
  184. newValue += layoutConstraints
  185. objc_setAssociatedObject(self.view, &installedLayoutConstraintsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  186. }
  187. internal func removeInstalledLayoutConstraints(_ layoutConstraints: [LayoutConstraint]) {
  188. let newValue = self.installedLayoutConstraints.filter { !layoutConstraints.contains($0) }
  189. objc_setAssociatedObject(self.view, &installedLayoutConstraintsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  190. }
  191. }
  192. private var labelKey: UInt8 = 0
  193. private var installedLayoutConstraintsKey: UInt8 = 0