ConstraintDSL.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 protocol ConstraintDSL {
  29. var target: AnyObject? { get }
  30. func setLabel(_ value: String?)
  31. func label() -> String?
  32. }
  33. extension ConstraintDSL {
  34. public func setLabel(_ value: String?) {
  35. objc_setAssociatedObject(self.target as Any, &labelKey, value, .OBJC_ASSOCIATION_COPY_NONATOMIC)
  36. }
  37. public func label() -> String? {
  38. return objc_getAssociatedObject(self.target as Any, &labelKey) as? String
  39. }
  40. }
  41. private var labelKey: UInt8 = 0
  42. public protocol ConstraintBasicAttributesDSL : ConstraintDSL {
  43. }
  44. extension ConstraintBasicAttributesDSL {
  45. // MARK: Basics
  46. public var left: ConstraintItem {
  47. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.left)
  48. }
  49. public var top: ConstraintItem {
  50. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.top)
  51. }
  52. public var right: ConstraintItem {
  53. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.right)
  54. }
  55. public var bottom: ConstraintItem {
  56. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.bottom)
  57. }
  58. public var leading: ConstraintItem {
  59. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leading)
  60. }
  61. public var trailing: ConstraintItem {
  62. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.trailing)
  63. }
  64. public var width: ConstraintItem {
  65. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.width)
  66. }
  67. public var height: ConstraintItem {
  68. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.height)
  69. }
  70. public var centerX: ConstraintItem {
  71. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerX)
  72. }
  73. public var centerY: ConstraintItem {
  74. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerY)
  75. }
  76. public var edges: ConstraintItem {
  77. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.edges)
  78. }
  79. public var directionalEdges: ConstraintItem {
  80. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalEdges)
  81. }
  82. public var horizontalEdges: ConstraintItem {
  83. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.horizontalEdges)
  84. }
  85. public var verticalEdges: ConstraintItem {
  86. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.verticalEdges)
  87. }
  88. public var directionalHorizontalEdges: ConstraintItem {
  89. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalHorizontalEdges)
  90. }
  91. public var directionalVerticalEdges: ConstraintItem {
  92. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalVerticalEdges)
  93. }
  94. public var size: ConstraintItem {
  95. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.size)
  96. }
  97. public var center: ConstraintItem {
  98. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.center)
  99. }
  100. }
  101. public protocol ConstraintAttributesDSL : ConstraintBasicAttributesDSL {
  102. }
  103. extension ConstraintAttributesDSL {
  104. // MARK: Baselines
  105. @available(*, deprecated, renamed:"lastBaseline")
  106. public var baseline: ConstraintItem {
  107. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.lastBaseline)
  108. }
  109. @available(iOS 8.0, OSX 10.11, *)
  110. public var lastBaseline: ConstraintItem {
  111. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.lastBaseline)
  112. }
  113. @available(iOS 8.0, OSX 10.11, *)
  114. public var firstBaseline: ConstraintItem {
  115. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.firstBaseline)
  116. }
  117. // MARK: Margins
  118. @available(iOS 8.0, *)
  119. public var leftMargin: ConstraintItem {
  120. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leftMargin)
  121. }
  122. @available(iOS 8.0, *)
  123. public var topMargin: ConstraintItem {
  124. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.topMargin)
  125. }
  126. @available(iOS 8.0, *)
  127. public var rightMargin: ConstraintItem {
  128. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.rightMargin)
  129. }
  130. @available(iOS 8.0, *)
  131. public var bottomMargin: ConstraintItem {
  132. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.bottomMargin)
  133. }
  134. @available(iOS 8.0, *)
  135. public var leadingMargin: ConstraintItem {
  136. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.leadingMargin)
  137. }
  138. @available(iOS 8.0, *)
  139. public var trailingMargin: ConstraintItem {
  140. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.trailingMargin)
  141. }
  142. @available(iOS 8.0, *)
  143. public var centerXWithinMargins: ConstraintItem {
  144. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerXWithinMargins)
  145. }
  146. @available(iOS 8.0, *)
  147. public var centerYWithinMargins: ConstraintItem {
  148. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerYWithinMargins)
  149. }
  150. @available(iOS 8.0, *)
  151. public var margins: ConstraintItem {
  152. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.margins)
  153. }
  154. @available(iOS 8.0, *)
  155. public var directionalMargins: ConstraintItem {
  156. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalMargins)
  157. }
  158. @available(iOS 8.0, *)
  159. public var centerWithinMargins: ConstraintItem {
  160. return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerWithinMargins)
  161. }
  162. }