ConstraintDescription.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-2015 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. /**
  29. Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
  30. */
  31. public protocol ConstraintDescriptionFinalizable: class {
  32. var constraint: Constraint { get }
  33. func labeled(label: String) -> ConstraintDescriptionFinalizable
  34. }
  35. /**
  36. Used to expose priority APIs
  37. */
  38. public protocol ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
  39. func priority(priority: Float) -> ConstraintDescriptionFinalizable
  40. func priority(priority: Double) -> ConstraintDescriptionFinalizable
  41. func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable
  42. func priority(priority: UInt) -> ConstraintDescriptionFinalizable
  43. func priority(priority: Int) -> ConstraintDescriptionFinalizable
  44. func priorityRequired() -> ConstraintDescriptionFinalizable
  45. func priorityHigh() -> ConstraintDescriptionFinalizable
  46. func priorityMedium() -> ConstraintDescriptionFinalizable
  47. func priorityLow() -> ConstraintDescriptionFinalizable
  48. }
  49. /**
  50. Used to expose multiplier & constant APIs
  51. */
  52. public protocol ConstraintDescriptionEditable: ConstraintDescriptionPriortizable {
  53. func multipliedBy(amount: Float) -> ConstraintDescriptionEditable
  54. func multipliedBy(amount: Double) -> ConstraintDescriptionEditable
  55. func multipliedBy(amount: CGFloat) -> ConstraintDescriptionEditable
  56. func multipliedBy(amount: Int) -> ConstraintDescriptionEditable
  57. func multipliedBy(amount: UInt) -> ConstraintDescriptionEditable
  58. func dividedBy(amount: Float) -> ConstraintDescriptionEditable
  59. func dividedBy(amount: Double) -> ConstraintDescriptionEditable
  60. func dividedBy(amount: CGFloat) -> ConstraintDescriptionEditable
  61. func dividedBy(amount: Int) -> ConstraintDescriptionEditable
  62. func dividedBy(amount: UInt) -> ConstraintDescriptionEditable
  63. func offset(amount: Float) -> ConstraintDescriptionEditable
  64. func offset(amount: Double) -> ConstraintDescriptionEditable
  65. func offset(amount: CGFloat) -> ConstraintDescriptionEditable
  66. func offset(amount: Int) -> ConstraintDescriptionEditable
  67. func offset(amount: UInt) -> ConstraintDescriptionEditable
  68. func offset(amount: CGPoint) -> ConstraintDescriptionEditable
  69. func offset(amount: CGSize) -> ConstraintDescriptionEditable
  70. func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable
  71. func inset(amount: Float) -> ConstraintDescriptionEditable
  72. func inset(amount: Double) -> ConstraintDescriptionEditable
  73. func inset(amount: CGFloat) -> ConstraintDescriptionEditable
  74. func inset(amount: Int) -> ConstraintDescriptionEditable
  75. func inset(amount: UInt) -> ConstraintDescriptionEditable
  76. func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable
  77. }
  78. /**
  79. Used to expose relation APIs
  80. */
  81. public protocol ConstraintDescriptionRelatable: class {
  82. func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  83. func equalTo(other: View) -> ConstraintDescriptionEditable
  84. func equalToSuperview() -> ConstraintDescriptionEditable
  85. @available(iOS 7.0, *)
  86. func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  87. @available(iOS 9.0, OSX 10.11, *)
  88. func equalTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  89. func equalTo(other: Float) -> ConstraintDescriptionEditable
  90. func equalTo(other: Double) -> ConstraintDescriptionEditable
  91. func equalTo(other: CGFloat) -> ConstraintDescriptionEditable
  92. func equalTo(other: Int) -> ConstraintDescriptionEditable
  93. func equalTo(other: UInt) -> ConstraintDescriptionEditable
  94. func equalTo(other: CGSize) -> ConstraintDescriptionEditable
  95. func equalTo(other: CGPoint) -> ConstraintDescriptionEditable
  96. func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  97. func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  98. func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  99. func lessThanOrEqualToSuperview() -> ConstraintDescriptionEditable
  100. @available(iOS 7.0, *)
  101. func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  102. @available(iOS 9.0, OSX 10.11, *)
  103. func lessThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  104. func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
  105. func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
  106. func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
  107. func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable
  108. func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable
  109. func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  110. func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  111. func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  112. func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  113. func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  114. func greaterThanOrEqualToSuperview() -> ConstraintDescriptionEditable
  115. @available(iOS 7.0, *)
  116. func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  117. @available(iOS 9.0, OSX 10.11, *)
  118. func greaterThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  119. func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
  120. func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
  121. func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
  122. func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable
  123. func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable
  124. func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  125. func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  126. func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  127. }
  128. /**
  129. Used to expose chaining APIs
  130. */
  131. public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
  132. var left: ConstraintDescriptionExtendable { get }
  133. var top: ConstraintDescriptionExtendable { get }
  134. var bottom: ConstraintDescriptionExtendable { get }
  135. var right: ConstraintDescriptionExtendable { get }
  136. var leading: ConstraintDescriptionExtendable { get }
  137. var trailing: ConstraintDescriptionExtendable { get }
  138. var width: ConstraintDescriptionExtendable { get }
  139. var height: ConstraintDescriptionExtendable { get }
  140. var centerX: ConstraintDescriptionExtendable { get }
  141. var centerY: ConstraintDescriptionExtendable { get }
  142. var baseline: ConstraintDescriptionExtendable { get }
  143. @available(iOS 8.0, *)
  144. var firstBaseline: ConstraintDescriptionExtendable { get }
  145. @available(iOS 8.0, *)
  146. var leftMargin: ConstraintDescriptionExtendable { get }
  147. @available(iOS 8.0, *)
  148. var rightMargin: ConstraintDescriptionExtendable { get }
  149. @available(iOS 8.0, *)
  150. var topMargin: ConstraintDescriptionExtendable { get }
  151. @available(iOS 8.0, *)
  152. var bottomMargin: ConstraintDescriptionExtendable { get }
  153. @available(iOS 8.0, *)
  154. var leadingMargin: ConstraintDescriptionExtendable { get }
  155. @available(iOS 8.0, *)
  156. var trailingMargin: ConstraintDescriptionExtendable { get }
  157. @available(iOS 8.0, *)
  158. var centerXWithinMargins: ConstraintDescriptionExtendable { get }
  159. @available(iOS 8.0, *)
  160. var centerYWithinMargins: ConstraintDescriptionExtendable { get }
  161. }
  162. /**
  163. Used to internally manage building constraint
  164. */
  165. internal class ConstraintDescription: ConstraintDescriptionExtendable, ConstraintDescriptionEditable, ConstraintDescriptionFinalizable {
  166. internal var left: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Left) }
  167. internal var top: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Top) }
  168. internal var right: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Right) }
  169. internal var bottom: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
  170. internal var leading: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
  171. internal var trailing: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
  172. internal var width: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Width) }
  173. internal var height: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Height) }
  174. internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
  175. internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
  176. internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
  177. internal var label: String?
  178. @available(iOS 8.0, *)
  179. internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
  180. @available(iOS 8.0, *)
  181. internal var leftMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
  182. @available(iOS 8.0, *)
  183. internal var rightMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
  184. @available(iOS 8.0, *)
  185. internal var topMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
  186. @available(iOS 8.0, *)
  187. internal var bottomMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
  188. @available(iOS 8.0, *)
  189. internal var leadingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
  190. @available(iOS 8.0, *)
  191. internal var trailingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
  192. @available(iOS 8.0, *)
  193. internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
  194. @available(iOS 8.0, *)
  195. internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
  196. // MARK: initializer
  197. init(fromItem: ConstraintItem) {
  198. self.fromItem = fromItem
  199. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  200. }
  201. // MARK: equalTo
  202. internal func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  203. return self.constrainTo(other, relation: .Equal)
  204. }
  205. internal func equalTo(other: View) -> ConstraintDescriptionEditable {
  206. return self.constrainTo(other, relation: .Equal)
  207. }
  208. internal func equalToSuperview() -> ConstraintDescriptionEditable {
  209. guard let superview = fromItem.view?.superview else {
  210. fatalError("equalToSuperview() requires the view have a superview before being set.")
  211. }
  212. return self.equalTo(superview)
  213. }
  214. @available(iOS 7.0, *)
  215. internal func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  216. return self.constrainTo(other, relation: .Equal)
  217. }
  218. @available(iOS 9.0, OSX 10.11, *)
  219. internal func equalTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  220. return self.constrainTo(other, relation: .Equal)
  221. }
  222. internal func equalTo(other: Float) -> ConstraintDescriptionEditable {
  223. return self.constrainTo(other, relation: .Equal)
  224. }
  225. internal func equalTo(other: Double) -> ConstraintDescriptionEditable {
  226. return self.constrainTo(Float(other), relation: .Equal)
  227. }
  228. internal func equalTo(other: CGFloat) -> ConstraintDescriptionEditable {
  229. return self.constrainTo(Float(other), relation: .Equal)
  230. }
  231. internal func equalTo(other: Int) -> ConstraintDescriptionEditable {
  232. return self.constrainTo(Float(other), relation: .Equal)
  233. }
  234. internal func equalTo(other: UInt) -> ConstraintDescriptionEditable {
  235. return self.constrainTo(Float(other), relation: .Equal)
  236. }
  237. internal func equalTo(other: CGSize) -> ConstraintDescriptionEditable {
  238. return self.constrainTo(other, relation: .Equal)
  239. }
  240. internal func equalTo(other: CGPoint) -> ConstraintDescriptionEditable {
  241. return self.constrainTo(other, relation: .Equal)
  242. }
  243. internal func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  244. return self.constrainTo(other, relation: .Equal)
  245. }
  246. // MARK: lessThanOrEqualTo
  247. internal func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  248. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  249. }
  250. internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  251. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  252. }
  253. internal func lessThanOrEqualToSuperview() -> ConstraintDescriptionEditable {
  254. guard let superview = fromItem.view?.superview else {
  255. fatalError("lessThanOrEqualToSuperview() requires the view have a superview before being set.")
  256. }
  257. return self.lessThanOrEqualTo(superview)
  258. }
  259. @available(iOS 7.0, *)
  260. internal func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  261. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  262. }
  263. @available(iOS 9.0, OSX 10.11, *)
  264. internal func lessThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  265. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  266. }
  267. internal func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
  268. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  269. }
  270. internal func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable {
  271. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  272. }
  273. internal func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable {
  274. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  275. }
  276. internal func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable {
  277. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  278. }
  279. internal func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable {
  280. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  281. }
  282. internal func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  283. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  284. }
  285. internal func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  286. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  287. }
  288. internal func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  289. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  290. }
  291. // MARK: greaterThanOrEqualTo
  292. internal func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  293. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  294. }
  295. internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  296. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  297. }
  298. internal func greaterThanOrEqualToSuperview() -> ConstraintDescriptionEditable {
  299. guard let superview = fromItem.view?.superview else {
  300. fatalError("greaterThanOrEqualToSuperview() requires the view have a superview before being set.")
  301. }
  302. return self.greaterThanOrEqualTo(superview)
  303. }
  304. @available(iOS 7.0, *)
  305. internal func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  306. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  307. }
  308. @available(iOS 9.0, OSX 10.11, *)
  309. internal func greaterThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  310. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  311. }
  312. internal func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
  313. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  314. }
  315. internal func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable {
  316. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  317. }
  318. internal func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable {
  319. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  320. }
  321. internal func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionEditable {
  322. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  323. }
  324. internal func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionEditable {
  325. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  326. }
  327. internal func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  328. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  329. }
  330. internal func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  331. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  332. }
  333. internal func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  334. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  335. }
  336. // MARK: multiplier
  337. internal func multipliedBy(amount: Float) -> ConstraintDescriptionEditable {
  338. self.multiplier = amount
  339. return self
  340. }
  341. internal func multipliedBy(amount: Double) -> ConstraintDescriptionEditable {
  342. return self.multipliedBy(Float(amount))
  343. }
  344. internal func multipliedBy(amount: CGFloat) -> ConstraintDescriptionEditable {
  345. return self.multipliedBy(Float(amount))
  346. }
  347. internal func multipliedBy(amount: Int) -> ConstraintDescriptionEditable {
  348. return self.multipliedBy(Float(amount))
  349. }
  350. internal func multipliedBy(amount: UInt) -> ConstraintDescriptionEditable {
  351. return self.multipliedBy(Float(amount))
  352. }
  353. internal func dividedBy(amount: Float) -> ConstraintDescriptionEditable {
  354. self.multiplier = 1.0 / amount;
  355. return self
  356. }
  357. internal func dividedBy(amount: Double) -> ConstraintDescriptionEditable {
  358. return self.dividedBy(Float(amount))
  359. }
  360. internal func dividedBy(amount: CGFloat) -> ConstraintDescriptionEditable {
  361. return self.dividedBy(Float(amount))
  362. }
  363. internal func dividedBy(amount: Int) -> ConstraintDescriptionEditable {
  364. return self.dividedBy(Float(amount))
  365. }
  366. internal func dividedBy(amount: UInt) -> ConstraintDescriptionEditable {
  367. return self.dividedBy(Float(amount))
  368. }
  369. // MARK: offset
  370. internal func offset(amount: Float) -> ConstraintDescriptionEditable {
  371. self.constant = amount
  372. return self
  373. }
  374. internal func offset(amount: Double) -> ConstraintDescriptionEditable {
  375. return self.offset(Float(amount))
  376. }
  377. internal func offset(amount: CGFloat) -> ConstraintDescriptionEditable {
  378. return self.offset(Float(amount))
  379. }
  380. internal func offset(amount: Int) -> ConstraintDescriptionEditable {
  381. return self.offset(Float(amount))
  382. }
  383. internal func offset(amount: UInt) -> ConstraintDescriptionEditable {
  384. return self.offset(Float(amount))
  385. }
  386. internal func offset(amount: CGPoint) -> ConstraintDescriptionEditable {
  387. self.constant = amount
  388. return self
  389. }
  390. internal func offset(amount: CGSize) -> ConstraintDescriptionEditable {
  391. self.constant = amount
  392. return self
  393. }
  394. internal func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  395. self.constant = amount
  396. return self
  397. }
  398. // MARK: inset
  399. internal func inset(amount: Float) -> ConstraintDescriptionEditable {
  400. let value = CGFloat(amount)
  401. self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
  402. return self
  403. }
  404. internal func inset(amount: Double) -> ConstraintDescriptionEditable {
  405. let value = CGFloat(amount)
  406. self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
  407. return self
  408. }
  409. internal func inset(amount: CGFloat) -> ConstraintDescriptionEditable {
  410. self.constant = EdgeInsets(top: amount, left: amount, bottom: -amount, right: -amount)
  411. return self
  412. }
  413. internal func inset(amount: Int) -> ConstraintDescriptionEditable {
  414. let value = CGFloat(amount)
  415. self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
  416. return self
  417. }
  418. internal func inset(amount: UInt) -> ConstraintDescriptionEditable {
  419. let value = CGFloat(amount)
  420. self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
  421. return self
  422. }
  423. internal func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  424. self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
  425. return self
  426. }
  427. // MARK: priority
  428. internal func priority(priority: Float) -> ConstraintDescriptionFinalizable {
  429. self.priority = priority
  430. return self
  431. }
  432. internal func priority(priority: Double) -> ConstraintDescriptionFinalizable {
  433. return self.priority(Float(priority))
  434. }
  435. internal func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable {
  436. return self.priority(Float(priority))
  437. }
  438. func priority(priority: UInt) -> ConstraintDescriptionFinalizable {
  439. return self.priority(Float(priority))
  440. }
  441. internal func priority(priority: Int) -> ConstraintDescriptionFinalizable {
  442. return self.priority(Float(priority))
  443. }
  444. internal func priorityRequired() -> ConstraintDescriptionFinalizable {
  445. return self.priority(1000.0)
  446. }
  447. internal func priorityHigh() -> ConstraintDescriptionFinalizable {
  448. return self.priority(750.0)
  449. }
  450. internal func priorityMedium() -> ConstraintDescriptionFinalizable {
  451. #if os(iOS) || os(tvOS)
  452. return self.priority(500.0)
  453. #else
  454. return self.priority(501.0)
  455. #endif
  456. }
  457. internal func priorityLow() -> ConstraintDescriptionFinalizable {
  458. return self.priority(250.0)
  459. }
  460. // MARK: Constraint
  461. internal var constraint: Constraint {
  462. if self.concreteConstraint == nil {
  463. if self.relation == nil {
  464. fatalError("Attempting to create a constraint from a ConstraintDescription before it has been fully chained.")
  465. }
  466. self.concreteConstraint = ConcreteConstraint(
  467. fromItem: self.fromItem,
  468. toItem: self.toItem,
  469. relation: self.relation!,
  470. constant: self.constant,
  471. multiplier: self.multiplier,
  472. priority: self.priority,
  473. label: self.label)
  474. }
  475. return self.concreteConstraint!
  476. }
  477. func labeled(label: String) -> ConstraintDescriptionFinalizable {
  478. self.label = label
  479. return self
  480. }
  481. // MARK: Private
  482. private let fromItem: ConstraintItem
  483. private var toItem: ConstraintItem {
  484. willSet {
  485. if self.concreteConstraint != nil {
  486. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  487. }
  488. }
  489. }
  490. private var relation: ConstraintRelation? {
  491. willSet {
  492. if self.concreteConstraint != nil {
  493. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  494. }
  495. }
  496. }
  497. private var constant: Any = Float(0.0) {
  498. willSet {
  499. if self.concreteConstraint != nil {
  500. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  501. }
  502. }
  503. }
  504. private var multiplier: Float = 1.0 {
  505. willSet {
  506. if self.concreteConstraint != nil {
  507. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  508. }
  509. }
  510. }
  511. private var priority: Float = 1000.0 {
  512. willSet {
  513. if self.concreteConstraint != nil {
  514. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  515. }
  516. }
  517. }
  518. private var concreteConstraint: ConcreteConstraint? = nil
  519. private func addConstraint(attributes: ConstraintAttributes) -> ConstraintDescription {
  520. if self.relation == nil {
  521. self.fromItem.attributes += attributes
  522. }
  523. return self
  524. }
  525. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintDescription {
  526. if other.attributes != ConstraintAttributes.None {
  527. let toLayoutAttributes = other.attributes.layoutAttributes
  528. if toLayoutAttributes.count > 1 {
  529. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  530. if toLayoutAttributes != fromLayoutAttributes {
  531. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  532. return self
  533. }
  534. other.attributes = ConstraintAttributes.None
  535. }
  536. }
  537. self.toItem = other
  538. self.relation = relation
  539. return self
  540. }
  541. private func constrainTo(other: View, relation: ConstraintRelation) -> ConstraintDescription {
  542. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  543. }
  544. @available(iOS 7.0, *)
  545. private func constrainTo(other: LayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
  546. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  547. }
  548. @available(iOS 9.0, OSX 10.11, *)
  549. private func constrainTo(other: NSLayoutAnchor, relation: ConstraintRelation) -> ConstraintDescription {
  550. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  551. }
  552. private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintDescription {
  553. self.constant = other
  554. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  555. }
  556. private func constrainTo(other: Double, relation: ConstraintRelation) -> ConstraintDescription {
  557. self.constant = other
  558. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  559. }
  560. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintDescription {
  561. self.constant = other
  562. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  563. }
  564. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintDescription {
  565. self.constant = other
  566. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  567. }
  568. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintDescription {
  569. self.constant = other
  570. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  571. }
  572. }