View.swift 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // View.swift
  3. // Snappy
  4. //
  5. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #if os(iOS)
  25. import UIKit
  26. typealias View = UIView
  27. #else
  28. import AppKit
  29. typealias View = NSView
  30. #endif
  31. extension View {
  32. #if SNP_SHORTHAND
  33. var left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) }
  34. var top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) }
  35. var right: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Right) }
  36. var bottom: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Bottom) }
  37. var leading: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Leading) }
  38. var trailing: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Trailing) }
  39. var width: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Width) }
  40. var height: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Height) }
  41. var centerX: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterX) }
  42. var centerY: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterY) }
  43. var baseline: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Baseline) }
  44. var edges: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Edges) }
  45. var size: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Size) }
  46. var center: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Center) }
  47. func makeConstraints(block: (maker: ConstraintMaker) -> ()) {
  48. ConstraintMaker.makeConstraints(self, block: block)
  49. }
  50. func remakeConstraints(block: (maker: ConstraintMaker) -> ()) {
  51. ConstraintMaker.remakeConstraints(self, block: block)
  52. }
  53. #else
  54. var snp_left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) }
  55. var snp_top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) }
  56. var snp_right: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Right) }
  57. var snp_bottom: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Bottom) }
  58. var snp_leading: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Leading) }
  59. var snp_trailing: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Trailing) }
  60. var snp_width: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Width) }
  61. var snp_height: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Height) }
  62. var snp_centerX: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterX) }
  63. var snp_centerY: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterY) }
  64. var snp_baseline: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Baseline) }
  65. var snp_edges: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Edges) }
  66. var snp_size: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Size) }
  67. var snp_center: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Center) }
  68. func snp_makeConstraints(block: (maker: ConstraintMaker) -> ()) {
  69. ConstraintMaker.makeConstraints(self, block: block)
  70. }
  71. func snp_remakeConstraints(block: (maker: ConstraintMaker) -> ()) {
  72. ConstraintMaker.remakeConstraints(self, block: block)
  73. }
  74. #endif
  75. }