ConstraintDescription.swift 29 KB

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