Debugging.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Snap
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  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)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. * View extension that exposes snp_label debugging api
  30. */
  31. public extension View {
  32. public var snp_label: String? {
  33. get {
  34. return objc_getAssociatedObject(self, &labelKey) as? String
  35. }
  36. set {
  37. objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_COPY_NONATOMIC))
  38. }
  39. }
  40. }
  41. /**
  42. * LayoutConstraint extension that exposes snp_label debugging api
  43. */
  44. public extension LayoutConstraint {
  45. public var snp_label: String? {
  46. get {
  47. return objc_getAssociatedObject(self, &labelKey) as? String
  48. }
  49. set {
  50. objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_COPY_NONATOMIC))
  51. }
  52. }
  53. override public var description: String {
  54. var description = "<"
  55. description += descriptionForObject(self)
  56. description += " \(descriptionForObject(self.firstItem))"
  57. if self.firstAttribute != .NotAnAttribute {
  58. description += ".\(self.firstAttribute.snp_description)"
  59. }
  60. description += " \(self.relation.snp_description)"
  61. if let secondItem: AnyObject = self.secondItem {
  62. description += " \(descriptionForObject(secondItem))"
  63. }
  64. if self.secondAttribute != .NotAnAttribute {
  65. description += ".\(self.secondAttribute.snp_description)"
  66. }
  67. if self.multiplier != 1.0 {
  68. description += " * \(self.multiplier)"
  69. }
  70. if self.secondAttribute == .NotAnAttribute {
  71. description += " \(self.constant)"
  72. } else {
  73. if self.constant > 0.0 {
  74. description += " + \(self.constant)"
  75. } else if self.constant < 0.0 {
  76. description += " - \(CGFloat.abs(self.constant))"
  77. }
  78. }
  79. if self.priority != 1000.0 {
  80. description += " ^\(self.priority)"
  81. }
  82. description += ">"
  83. return description
  84. }
  85. }
  86. private var labelKey = ""
  87. private func descriptionForObject(object: AnyObject) -> String {
  88. let pointerDescription = NSString(format: "%p", [object])
  89. if let object = object as? UIView {
  90. return "<\(object.dynamicType):\(object.snp_label ?? pointerDescription)>"
  91. } else if let object = object as? LayoutConstraint {
  92. return "<\(object.dynamicType):\(object.snp_label ?? pointerDescription)>"
  93. }
  94. return "<\(object.dynamicType):\(pointerDescription)>"
  95. }
  96. private extension NSLayoutRelation {
  97. private var snp_description: String {
  98. switch self {
  99. case .Equal: return "=="
  100. case .GreaterThanOrEqual: return ">="
  101. case .LessThanOrEqual: return "<="
  102. }
  103. }
  104. }
  105. private extension NSLayoutAttribute {
  106. private var snp_description: String {
  107. #if os(iOS)
  108. switch self {
  109. case .NotAnAttribute: return "notAnAttribute"
  110. case .Top: return "top"
  111. case .Left: return "left"
  112. case .Bottom: return "bottom"
  113. case .Right: return "right"
  114. case .Leading: return "leading"
  115. case .Trailing: return "trailing"
  116. case .Width: return "width"
  117. case .Height: return "height"
  118. case .CenterX: return "centerX"
  119. case .CenterY: return "centerY"
  120. case .Baseline: return "baseline"
  121. case .FirstBaseline: return "firstBaseline"
  122. case .TopMargin: return "topMargin"
  123. case .LeftMargin: return "leftMargin"
  124. case .BottomMargin: return "bottomMargin"
  125. case .RightMargin: return "rightMargin"
  126. case .LeadingMargin: return "leadingMargin"
  127. case .TrailingMargin: return "trailingMargin"
  128. case .CenterXWithinMargins: return "centerXWithinMargins"
  129. case .CenterYWithinMargins: return "centerYWithinMargins"
  130. }
  131. #else
  132. switch self {
  133. case .NotAnAttribute: return "notAnAttribute"
  134. case .Top: return "top"
  135. case .Left: return "left"
  136. case .Bottom: return "bottom"
  137. case .Right: return "right"
  138. case .Leading: return "leading"
  139. case .Trailing: return "trailing"
  140. case .Width: return "width"
  141. case .Height: return "height"
  142. case .CenterX: return "centerX"
  143. case .CenterY: return "centerY"
  144. case .Baseline: return "baseline"
  145. }
  146. #endif
  147. }
  148. }