NSLayoutConstraint+MASDebugAdditions.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. @(NSLayoutAttributeFirstBaseline) : @"firstBaseline",
  42. @(NSLayoutAttributeLastBaseline) : @"lastBaseline",
  43. #if TARGET_OS_IPHONE || TARGET_OS_TV
  44. @(NSLayoutAttributeLeftMargin) : @"leftMargin",
  45. @(NSLayoutAttributeRightMargin) : @"rightMargin",
  46. @(NSLayoutAttributeTopMargin) : @"topMargin",
  47. @(NSLayoutAttributeBottomMargin) : @"bottomMargin",
  48. @(NSLayoutAttributeLeadingMargin) : @"leadingMargin",
  49. @(NSLayoutAttributeTrailingMargin) : @"trailingMargin",
  50. @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins",
  51. @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins",
  52. #endif
  53. };
  54. });
  55. return descriptionMap;
  56. }
  57. + (NSDictionary *)layoutPriorityDescriptionsByValue {
  58. static dispatch_once_t once;
  59. static NSDictionary *descriptionMap;
  60. dispatch_once(&once, ^{
  61. #if TARGET_OS_IPHONE || TARGET_OS_TV
  62. descriptionMap = @{
  63. @(MASLayoutPriorityDefaultHigh) : @"high",
  64. @(MASLayoutPriorityDefaultLow) : @"low",
  65. @(MASLayoutPriorityDefaultMedium) : @"medium",
  66. @(MASLayoutPriorityRequired) : @"required",
  67. @(MASLayoutPriorityFittingSizeLevel) : @"fitting size",
  68. };
  69. #elif TARGET_OS_MAC
  70. descriptionMap = @{
  71. @(MASLayoutPriorityDefaultHigh) : @"high",
  72. @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window",
  73. @(MASLayoutPriorityDefaultMedium) : @"medium",
  74. @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put",
  75. @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window",
  76. @(MASLayoutPriorityDefaultLow) : @"low",
  77. @(MASLayoutPriorityFittingSizeCompression) : @"fitting size",
  78. @(MASLayoutPriorityRequired) : @"required",
  79. };
  80. #endif
  81. });
  82. return descriptionMap;
  83. }
  84. #pragma mark - description override
  85. + (NSString *)descriptionForObject:(id)obj {
  86. if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) {
  87. return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]];
  88. }
  89. return [NSString stringWithFormat:@"%@:%p", [obj class], obj];
  90. }
  91. - (NSString *)description {
  92. NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"];
  93. [description appendString:[self.class descriptionForObject:self]];
  94. [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]];
  95. if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) {
  96. [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]];
  97. }
  98. [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]];
  99. if (self.secondItem) {
  100. [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]];
  101. }
  102. if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) {
  103. [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]];
  104. }
  105. if (self.multiplier != 1) {
  106. [description appendFormat:@" * %g", self.multiplier];
  107. }
  108. if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) {
  109. [description appendFormat:@" %g", self.constant];
  110. } else {
  111. if (self.constant) {
  112. [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)];
  113. }
  114. }
  115. if (self.priority != MASLayoutPriorityRequired) {
  116. [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]];
  117. }
  118. [description appendString:@">"];
  119. return description;
  120. }
  121. @end