MASConstraint.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // MASConstraint.h
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 22/07/13.
  6. // Copyright (c) 2013 cloudling. All rights reserved.
  7. //
  8. #import "MASUtilities.h"
  9. /**
  10. * Enables Constraints to be created with chainable syntax
  11. * Constraint can represent single NSLayoutConstraint (MASViewConstraint)
  12. * or a group of NSLayoutConstraints (MASComposisteConstraint)
  13. */
  14. @interface MASConstraint : NSObject
  15. // Chaining Support
  16. /**
  17. * Modifies the NSLayoutConstraint constant,
  18. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  19. * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
  20. */
  21. - (MASConstraint * (^)(MASEdgeInsets insets))insets;
  22. /**
  23. * Modifies the NSLayoutConstraint constant,
  24. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  25. * NSLayoutAttributeWidth, NSLayoutAttributeHeight
  26. */
  27. - (MASConstraint * (^)(CGSize offset))sizeOffset;
  28. /**
  29. * Modifies the NSLayoutConstraint constant,
  30. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  31. * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
  32. */
  33. - (MASConstraint * (^)(CGPoint offset))centerOffset;
  34. /**
  35. * Modifies the NSLayoutConstraint constant
  36. */
  37. - (MASConstraint * (^)(CGFloat offset))offset;
  38. /**
  39. * Modifies the NSLayoutConstraint constant based on a value type
  40. */
  41. - (MASConstraint * (^)(NSValue *value))valueOffset;
  42. /**
  43. * Sets the NSLayoutConstraint multiplier property
  44. */
  45. - (MASConstraint * (^)(CGFloat multiplier))multipliedBy;
  46. /**
  47. * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy
  48. */
  49. - (MASConstraint * (^)(CGFloat divider))dividedBy;
  50. /**
  51. * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
  52. */
  53. - (MASConstraint * (^)(MASLayoutPriority priority))priority;
  54. /**
  55. * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
  56. */
  57. - (MASConstraint * (^)())priorityLow;
  58. /**
  59. * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
  60. */
  61. - (MASConstraint * (^)())priorityMedium;
  62. /**
  63. * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
  64. */
  65. - (MASConstraint * (^)())priorityHigh;
  66. /**
  67. * Sets the constraint relation to NSLayoutRelationEqual
  68. * returns a block which accepts one of the following:
  69. * MASViewAttribute, UIView, NSValue, NSArray
  70. * see readme for more details.
  71. */
  72. - (MASConstraint * (^)(id attr))equalTo;
  73. /**
  74. * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
  75. * returns a block which accepts one of the following:
  76. * MASViewAttribute, UIView, NSValue, NSArray
  77. * see readme for more details.
  78. */
  79. - (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
  80. /**
  81. * Sets the constraint relation to NSLayoutRelationLessThanOrEqual
  82. * returns a block which accepts one of the following:
  83. * MASViewAttribute, UIView, NSValue, NSArray
  84. * see readme for more details.
  85. */
  86. - (MASConstraint * (^)(id attr))lessThanOrEqualTo;
  87. /**
  88. * Optional semantic property which has no effect but improves the readability of constraint
  89. */
  90. - (MASConstraint *)with;
  91. /**
  92. * Optional semantic property which has no effect but improves the readability of constraint
  93. */
  94. - (MASConstraint *)and;
  95. /**
  96. * Creates a new MASCompositeConstraint with the called attribute and reciever
  97. */
  98. - (MASConstraint *)left;
  99. - (MASConstraint *)top;
  100. - (MASConstraint *)right;
  101. - (MASConstraint *)bottom;
  102. - (MASConstraint *)leading;
  103. - (MASConstraint *)trailing;
  104. - (MASConstraint *)width;
  105. - (MASConstraint *)height;
  106. - (MASConstraint *)centerX;
  107. - (MASConstraint *)centerY;
  108. - (MASConstraint *)baseline;
  109. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
  110. - (MASConstraint *)firstBaseline;
  111. - (MASConstraint *)lastBaseline;
  112. #endif
  113. #if TARGET_OS_IPHONE || TARGET_OS_TV
  114. - (MASConstraint *)leftMargin;
  115. - (MASConstraint *)rightMargin;
  116. - (MASConstraint *)topMargin;
  117. - (MASConstraint *)bottomMargin;
  118. - (MASConstraint *)leadingMargin;
  119. - (MASConstraint *)trailingMargin;
  120. - (MASConstraint *)centerXWithinMargins;
  121. - (MASConstraint *)centerYWithinMargins;
  122. #endif
  123. /**
  124. * Sets the constraint debug name
  125. */
  126. - (MASConstraint * (^)(id key))key;
  127. // NSLayoutConstraint constant Setters
  128. // for use outside of mas_updateConstraints/mas_makeConstraints blocks
  129. /**
  130. * Modifies the NSLayoutConstraint constant,
  131. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  132. * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
  133. */
  134. - (void)setInsets:(MASEdgeInsets)insets;
  135. /**
  136. * Modifies the NSLayoutConstraint constant,
  137. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  138. * NSLayoutAttributeWidth, NSLayoutAttributeHeight
  139. */
  140. - (void)setSizeOffset:(CGSize)sizeOffset;
  141. /**
  142. * Modifies the NSLayoutConstraint constant,
  143. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  144. * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
  145. */
  146. - (void)setCenterOffset:(CGPoint)centerOffset;
  147. /**
  148. * Modifies the NSLayoutConstraint constant
  149. */
  150. - (void)setOffset:(CGFloat)offset;
  151. // NSLayoutConstraint Installation support
  152. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  153. /**
  154. * Whether or not to go through the animator proxy when modifying the constraint
  155. */
  156. @property (nonatomic, copy, readonly) MASConstraint *animator;
  157. #endif
  158. /**
  159. * Activates an NSLayoutConstraint if it's supported by an OS.
  160. * Invokes install otherwise.
  161. */
  162. - (void)activate;
  163. /**
  164. * Deactivates previously installed/activated NSLayoutConstraint.
  165. */
  166. - (void)deactivate;
  167. /**
  168. * Creates a NSLayoutConstraint and adds it to the appropriate view.
  169. */
  170. - (void)install;
  171. /**
  172. * Removes previously installed NSLayoutConstraint
  173. */
  174. - (void)uninstall;
  175. @end
  176. /**
  177. * Convenience auto-boxing macros for MASConstraint methods.
  178. *
  179. * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax.
  180. * A potential drawback of this is that the unprefixed macros will appear in global scope.
  181. */
  182. #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
  183. #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
  184. #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
  185. #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__)))
  186. #ifdef MAS_SHORTHAND_GLOBALS
  187. #define equalTo(...) mas_equalTo(__VA_ARGS__)
  188. #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__)
  189. #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__)
  190. #define offset(...) mas_offset(__VA_ARGS__)
  191. #endif
  192. @interface MASConstraint (AutoboxingSupport)
  193. /**
  194. * Aliases to corresponding relation methods (for shorthand macros)
  195. * Also needed to aid autocompletion
  196. */
  197. - (MASConstraint * (^)(id attr))mas_equalTo;
  198. - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
  199. - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
  200. /**
  201. * A dummy method to aid autocompletion
  202. */
  203. - (MASConstraint * (^)(id offset))mas_offset;
  204. @end