MASConstraint.m 7.9 KB

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