MASConstraint.h 7.7 KB

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