酷店

MASConstraint.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 * (^)(void))priorityLow {
  52. return ^id{
  53. self.priority(MASLayoutPriorityDefaultLow);
  54. return self;
  55. };
  56. }
  57. - (MASConstraint * (^)(void))priorityMedium {
  58. return ^id{
  59. self.priority(MASLayoutPriorityDefaultMedium);
  60. return self;
  61. };
  62. }
  63. - (MASConstraint * (^)(void))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 * (^)(CGFloat))inset {
  77. return ^id(CGFloat inset){
  78. self.inset = inset;
  79. return self;
  80. };
  81. }
  82. - (MASConstraint * (^)(CGSize))sizeOffset {
  83. return ^id(CGSize offset) {
  84. self.sizeOffset = offset;
  85. return self;
  86. };
  87. }
  88. - (MASConstraint * (^)(CGPoint))centerOffset {
  89. return ^id(CGPoint offset) {
  90. self.centerOffset = offset;
  91. return self;
  92. };
  93. }
  94. - (MASConstraint * (^)(CGFloat))offset {
  95. return ^id(CGFloat offset){
  96. self.offset = offset;
  97. return self;
  98. };
  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. };
  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. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
  176. - (MASConstraint *)firstBaseline {
  177. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline];
  178. }
  179. - (MASConstraint *)lastBaseline {
  180. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline];
  181. }
  182. #endif
  183. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000)
  184. - (MASConstraint *)leftMargin {
  185. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin];
  186. }
  187. - (MASConstraint *)rightMargin {
  188. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin];
  189. }
  190. - (MASConstraint *)topMargin {
  191. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin];
  192. }
  193. - (MASConstraint *)bottomMargin {
  194. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin];
  195. }
  196. - (MASConstraint *)leadingMargin {
  197. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin];
  198. }
  199. - (MASConstraint *)trailingMargin {
  200. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin];
  201. }
  202. - (MASConstraint *)centerXWithinMargins {
  203. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins];
  204. }
  205. - (MASConstraint *)centerYWithinMargins {
  206. return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins];
  207. }
  208. #endif
  209. #pragma mark - Abstract
  210. - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); }
  211. - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); }
  212. - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); }
  213. - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); }
  214. - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); }
  215. - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); }
  216. - (void)setInset:(CGFloat __unused)inset { MASMethodNotImplemented(); }
  217. - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); }
  218. - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); }
  219. - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); }
  220. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  221. - (MASConstraint *)animator { MASMethodNotImplemented(); }
  222. #endif
  223. - (void)activate { MASMethodNotImplemented(); }
  224. - (void)deactivate { MASMethodNotImplemented(); }
  225. - (void)install { MASMethodNotImplemented(); }
  226. - (void)uninstall { MASMethodNotImplemented(); }
  227. @end