NSLayoutConstraint+MASDebugAdditions.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // NSLayoutConstraint+MASDebugAdditions.m
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 3/08/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "NSLayoutConstraint+MASDebugAdditions.h"
  9. #import "MASConstraint.h"
  10. #import "MASLayoutConstraint.h"
  11. @implementation NSLayoutConstraint (MASDebugAdditions)
  12. #pragma mark - description maps
  13. + (NSDictionary *)layoutRelationDescriptionsByValue {
  14. static dispatch_once_t once;
  15. static NSDictionary *descriptionMap;
  16. dispatch_once(&once, ^{
  17. descriptionMap = @{
  18. @(NSLayoutRelationEqual) : @"==",
  19. @(NSLayoutRelationGreaterThanOrEqual) : @">=",
  20. @(NSLayoutRelationLessThanOrEqual) : @"<=",
  21. };
  22. });
  23. return descriptionMap;
  24. }
  25. + (NSDictionary *)layoutAttributeDescriptionsByValue {
  26. static dispatch_once_t once;
  27. static NSDictionary *descriptionMap;
  28. dispatch_once(&once, ^{
  29. descriptionMap = @{
  30. @(NSLayoutAttributeTop) : @"top",
  31. @(NSLayoutAttributeLeft) : @"left",
  32. @(NSLayoutAttributeBottom) : @"bottom",
  33. @(NSLayoutAttributeRight) : @"right",
  34. @(NSLayoutAttributeLeading) : @"leading",
  35. @(NSLayoutAttributeTrailing) : @"trailing",
  36. @(NSLayoutAttributeWidth) : @"width",
  37. @(NSLayoutAttributeHeight) : @"height",
  38. @(NSLayoutAttributeCenterX) : @"centerX",
  39. @(NSLayoutAttributeCenterY) : @"centerY",
  40. @(NSLayoutAttributeBaseline) : @"baseline",
  41. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
  42. @(NSLayoutAttributeFirstBaseline) : @"firstBaseline",
  43. @(NSLayoutAttributeLastBaseline) : @"lastBaseline",
  44. #endif
  45. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000)
  46. @(NSLayoutAttributeLeftMargin) : @"leftMargin",
  47. @(NSLayoutAttributeRightMargin) : @"rightMargin",
  48. @(NSLayoutAttributeTopMargin) : @"topMargin",
  49. @(NSLayoutAttributeBottomMargin) : @"bottomMargin",
  50. @(NSLayoutAttributeLeadingMargin) : @"leadingMargin",
  51. @(NSLayoutAttributeTrailingMargin) : @"trailingMargin",
  52. @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins",
  53. @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins",
  54. #endif
  55. };
  56. });
  57. return descriptionMap;
  58. }
  59. + (NSDictionary *)layoutPriorityDescriptionsByValue {
  60. static dispatch_once_t once;
  61. static NSDictionary *descriptionMap;
  62. dispatch_once(&once, ^{
  63. #if TARGET_OS_IPHONE || TARGET_OS_TV
  64. descriptionMap = @{
  65. @(MASLayoutPriorityDefaultHigh) : @"high",
  66. @(MASLayoutPriorityDefaultLow) : @"low",
  67. @(MASLayoutPriorityDefaultMedium) : @"medium",
  68. @(MASLayoutPriorityRequired) : @"required",
  69. @(MASLayoutPriorityFittingSizeLevel) : @"fitting size",
  70. };
  71. #elif TARGET_OS_MAC
  72. descriptionMap = @{
  73. @(MASLayoutPriorityDefaultHigh) : @"high",
  74. @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window",
  75. @(MASLayoutPriorityDefaultMedium) : @"medium",
  76. @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put",
  77. @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window",
  78. @(MASLayoutPriorityDefaultLow) : @"low",
  79. @(MASLayoutPriorityFittingSizeCompression) : @"fitting size",
  80. @(MASLayoutPriorityRequired) : @"required",
  81. };
  82. #endif
  83. });
  84. return descriptionMap;
  85. }
  86. #pragma mark - description override
  87. + (NSString *)descriptionForObject:(id)obj {
  88. if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) {
  89. return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]];
  90. }
  91. return [NSString stringWithFormat:@"%@:%p", [obj class], obj];
  92. }
  93. - (NSString *)description {
  94. NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"];
  95. [description appendString:[self.class descriptionForObject:self]];
  96. [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]];
  97. if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) {
  98. [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]];
  99. }
  100. [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]];
  101. if (self.secondItem) {
  102. [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]];
  103. }
  104. if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) {
  105. [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]];
  106. }
  107. if (self.multiplier != 1) {
  108. [description appendFormat:@" * %g", self.multiplier];
  109. }
  110. if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) {
  111. [description appendFormat:@" %g", self.constant];
  112. } else {
  113. if (self.constant) {
  114. [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)];
  115. }
  116. }
  117. if (self.priority != MASLayoutPriorityRequired) {
  118. [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]];
  119. }
  120. [description appendString:@">"];
  121. return description;
  122. }
  123. @end