MASConstraint.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // MASConstraint.m
  3. // Masonry
  4. //
  5. // Created by Nick Tymchenko on 1/20/14.
  6. //
  7. #import "MASConstraint.h"
  8. #import "MASConstraint+Private.h"
  9. #define MASMethodNotImplemented() \
  10. @throw [NSException exceptionWithName:NSInternalInconsistencyException \
  11. reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \
  12. userInfo:nil]
  13. @implementation MASConstraint
  14. #pragma mark - Init
  15. - (id)init {
  16. NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly.");
  17. return [super init];
  18. }
  19. #pragma mark - NSLayoutRelation proxies
  20. - (MASConstraint * (^)(id))equalTo {
  21. return ^id(id attribute) {
  22. return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
  23. };
  24. }
  25. - (MASConstraint * (^)(id))mas_equalTo {
  26. return ^id(id attribute) {
  27. return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
  28. };
  29. }
  30. - (MASConstraint * (^)(id))greaterThanOrEqualTo {
  31. return ^id(id attribute) {
  32. return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);
  33. };
  34. }
  35. - (MASConstraint * (^)(id))mas_greaterThanOrEqualTo {
  36. return ^id(id attribute) {
  37. return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);
  38. };
  39. }
  40. - (MASConstraint * (^)(id))lessThanOrEqualTo {
  41. return ^id(id attribute) {
  42. return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);
  43. };
  44. }
  45. - (MASConstraint * (^)(id))mas_lessThanOrEqualTo {
  46. return ^id(id attribute) {
  47. return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);
  48. };
  49. }
  50. #pragma mark - MASLayoutPriority proxies
  51. - (MASConstraint * (^)())priorityLow {
  52. return ^id{
  53. self.priority(MASLayoutPriorityDefaultLow);
  54. return self;
  55. };
  56. }
  57. - (MASConstraint * (^)())priorityMedium {
  58. return ^id{
  59. self.priority(MASLayoutPriorityDefaultMedium);
  60. return self;
  61. };
  62. }
  63. - (MASConstraint * (^)())priorityHigh {
  64. return ^id{
  65. self.priority(MASLayoutPriorityDefaultHigh);
  66. return self;
  67. };
  68. }
  69. #pragma mark - NSLayoutConstraint constant proxies
  70. - (MASConstraint * (^)(MASEdgeInsets))insets {
  71. return ^id(MASEdgeInsets insets){
  72. self.insets = insets;
  73. return self;
  74. };
  75. }
  76. - (MASConstraint * (^)(CGSize))sizeOffset {
  77. return ^id(CGSize offset) {
  78. self.sizeOffset = offset;
  79. return self;
  80. };
  81. }
  82. - (MASConstraint * (^)(CGPoint))centerOffset {
  83. return ^id(CGPoint offset) {
  84. self.centerOffset = offset;
  85. return self;
  86. };
  87. }
  88. - (MASConstraint * (^)(CGFloat))offset {
  89. return ^id(CGFloat offset){
  90. self.offset = offset;
  91. return self;
  92. };
  93. }
  94. - (MASConstraint * (^)(NSValue *value))valueOffset {
  95. return ^id(NSValue *offset) {
  96. NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset);
  97. [self setLayoutConstantWithValue:offset];
  98. return self;
  99. };
  100. }
  101. - (MASConstraint * (^)(id offset))mas_offset {
  102. // Will never be called due to macro
  103. return nil;
  104. }
  105. #pragma mark - NSLayoutConstraint constant setter
  106. - (void)setLayoutConstantWithValue:(NSValue *)value {
  107. if ([value isKindOfClass:NSNumber.class]) {
  108. self.offset = [(NSNumber *)value doubleValue];
  109. } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) {
  110. CGPoint point;
  111. [value getValue:&point];
  112. self.centerOffset = point;
  113. } else if (strcmp(value.objCType, @encode(CGSize)) == 0) {
  114. CGSize size;
  115. [value getValue:&size];
  116. self.sizeOffset = size;
  117. } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) {
  118. MASEdgeInsets insets;
  119. [value getValue:&insets];
  120. self.insets = insets;
  121. } else {
  122. NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value);
  123. }
  124. }
  125. #pragma mark - Semantic properties
  126. - (MASConstraint *)with {
  127. return self;
  128. }
  129. - (MASConstraint *)and {
  130. return self;
  131. }
  132. #pragma mark - Chaining
  133. - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute {
  134. MASMethodNotImplemented();
  135. }
  136. - (MASConstraint *)left {
  137. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft];
  138. }
  139. - (MASConstraint *)top {
  140. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop];
  141. }
  142. - (MASConstraint *)right {
  143. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight];
  144. }
  145. - (MASConstraint *)bottom {
  146. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom];
  147. }
  148. - (MASConstraint *)leading {
  149. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading];
  150. }
  151. - (MASConstraint *)trailing {
  152. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing];
  153. }
  154. - (MASConstraint *)width {
  155. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth];
  156. }
  157. - (MASConstraint *)height {
  158. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight];
  159. }
  160. - (MASConstraint *)centerX {
  161. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX];
  162. }
  163. - (MASConstraint *)centerY {
  164. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY];
  165. }
  166. - (MASConstraint *)baseline {
  167. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline];
  168. }
  169. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
  170. - (MASConstraint *)firstBaseline {
  171. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline];
  172. }
  173. - (MASConstraint *)lastBaseline {
  174. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline];
  175. }
  176. #endif
  177. #if TARGET_OS_IPHONE || TARGET_OS_TV
  178. - (MASConstraint *)leftMargin {
  179. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin];
  180. }
  181. - (MASConstraint *)rightMargin {
  182. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin];
  183. }
  184. - (MASConstraint *)topMargin {
  185. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin];
  186. }
  187. - (MASConstraint *)bottomMargin {
  188. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin];
  189. }
  190. - (MASConstraint *)leadingMargin {
  191. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin];
  192. }
  193. - (MASConstraint *)trailingMargin {
  194. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin];
  195. }
  196. - (MASConstraint *)centerXWithinMargins {
  197. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins];
  198. }
  199. - (MASConstraint *)centerYWithinMargins {
  200. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins];
  201. }
  202. #endif
  203. #pragma mark - Abstract
  204. - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); }
  205. - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); }
  206. - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); }
  207. - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); }
  208. - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); }
  209. - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); }
  210. - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); }
  211. - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); }
  212. - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); }
  213. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  214. - (MASConstraint *)animator { MASMethodNotImplemented(); }
  215. #endif
  216. - (void)activate { MASMethodNotImplemented(); }
  217. - (void)deactivate { MASMethodNotImplemented(); }
  218. - (void)install { MASMethodNotImplemented(); }
  219. - (void)uninstall { MASMethodNotImplemented(); }
  220. @end