MASConstraint.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. - (MASConstraint *)firstBaseline;
  116. - (MASConstraint *)lastBaseline;
  117. #if TARGET_OS_IPHONE || TARGET_OS_TV
  118. - (MASConstraint *)leftMargin;
  119. - (MASConstraint *)rightMargin;
  120. - (MASConstraint *)topMargin;
  121. - (MASConstraint *)bottomMargin;
  122. - (MASConstraint *)leadingMargin;
  123. - (MASConstraint *)trailingMargin;
  124. - (MASConstraint *)centerXWithinMargins;
  125. - (MASConstraint *)centerYWithinMargins;
  126. #endif
  127. /**
  128. * Sets the constraint debug name
  129. */
  130. - (MASConstraint * (^)(id key))key;
  131. // NSLayoutConstraint constant Setters
  132. // for use outside of mas_updateConstraints/mas_makeConstraints blocks
  133. /**
  134. * Modifies the NSLayoutConstraint constant,
  135. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  136. * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
  137. */
  138. - (void)setInsets:(MASEdgeInsets)insets;
  139. /**
  140. * Modifies the NSLayoutConstraint constant,
  141. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  142. * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
  143. */
  144. - (void)setInset:(CGFloat)inset;
  145. /**
  146. * Modifies the NSLayoutConstraint constant,
  147. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  148. * NSLayoutAttributeWidth, NSLayoutAttributeHeight
  149. */
  150. - (void)setSizeOffset:(CGSize)sizeOffset;
  151. /**
  152. * Modifies the NSLayoutConstraint constant,
  153. * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  154. * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
  155. */
  156. - (void)setCenterOffset:(CGPoint)centerOffset;
  157. /**
  158. * Modifies the NSLayoutConstraint constant
  159. */
  160. - (void)setOffset:(CGFloat)offset;
  161. // NSLayoutConstraint Installation support
  162. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  163. /**
  164. * Whether or not to go through the animator proxy when modifying the constraint
  165. */
  166. @property (nonatomic, copy, readonly) MASConstraint *animator;
  167. #endif
  168. /**
  169. * Activates an NSLayoutConstraint if it's supported by an OS.
  170. * Invokes install otherwise.
  171. */
  172. - (void)activate;
  173. /**
  174. * Deactivates previously installed/activated NSLayoutConstraint.
  175. */
  176. - (void)deactivate;
  177. /**
  178. * Creates a NSLayoutConstraint and adds it to the appropriate view.
  179. */
  180. - (void)install;
  181. /**
  182. * Removes previously installed NSLayoutConstraint
  183. */
  184. - (void)uninstall;
  185. @end
  186. /**
  187. * Convenience auto-boxing macros for MASConstraint methods.
  188. *
  189. * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax.
  190. * A potential drawback of this is that the unprefixed macros will appear in global scope.
  191. */
  192. #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
  193. #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
  194. #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
  195. #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__)))
  196. #ifdef MAS_SHORTHAND_GLOBALS
  197. #define equalTo(...) mas_equalTo(__VA_ARGS__)
  198. #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__)
  199. #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__)
  200. #define offset(...) mas_offset(__VA_ARGS__)
  201. #endif
  202. @interface MASConstraint (AutoboxingSupport)
  203. /**
  204. * Aliases to corresponding relation methods (for shorthand macros)
  205. * Also needed to aid autocompletion
  206. */
  207. - (MASConstraint * (^)(id attr))mas_equalTo;
  208. - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
  209. - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
  210. /**
  211. * A dummy method to aid autocompletion
  212. */
  213. - (MASConstraint * (^)(id offset))mas_offset;
  214. @end