口袋优选

UIView+SDAutoLayout.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // UIView+SDAutoLayout.h
  3. //
  4. // Created by gsd on 15/10/6.
  5. // Copyright (c) 2015年 gsd. All rights reserved.
  6. //
  7. /*
  8. *************************************************************************
  9. --------- INTRODUCTION ---------
  10. USAGE:
  11. MODE 1. >>>>>>>>>>>>>>> You can use it in this way:
  12. Demo.sd_layout
  13. .topSpaceToView(v1, 100)
  14. .bottomSpaceToView(v3, 100)
  15. .leftSpaceToView(v0, 150)
  16. .rightSpaceToView(v2, 150);
  17. MODE 2. >>>>>>>>>>>>>>> You can also use it in this way that is more brevity:
  18. Demo.sd_layout.topSpaceToView(v1, 100).bottomSpaceToView(v3, 100).leftSpaceToView(v0, 150).rightSpaceToView(v2, 150);
  19. *************************************************************************
  20. */
  21. /*
  22. *********************************************************************************
  23. *
  24. * 在您使用此自动布局库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并
  25. * 帮您解决问题。
  26. * QQ : 2689718696(gsdios)
  27. * Email : gsdios@126.com
  28. * GitHub: https://github.com/gsdios
  29. * 新浪微博:GSD_iOS
  30. *
  31. * 视频教程:http://www.letv.com/ptv/vplay/24038772.html
  32. * 用法示例:https://github.com/gsdios/SDAutoLayout/blob/master/README.md
  33. *
  34. *********************************************************************************
  35. */
  36. // 如果需要用“断言”调试程序请打开此宏
  37. //#define SDDebugWithAssert
  38. #import <UIKit/UIKit.h>
  39. @class SDAutoLayoutModel, SDUIViewCategoryManager;
  40. typedef SDAutoLayoutModel *(^MarginToView)(UIView *toView, CGFloat value);
  41. typedef SDAutoLayoutModel *(^Margin)(CGFloat value);
  42. typedef SDAutoLayoutModel *(^MarginEqualToView)(UIView *toView);
  43. typedef SDAutoLayoutModel *(^WidthHeight)(CGFloat value);
  44. typedef SDAutoLayoutModel *(^WidthHeightEqualToView)(UIView *toView, CGFloat ratioValue);
  45. typedef SDAutoLayoutModel *(^AutoHeight)(CGFloat ratioValue);
  46. typedef SDAutoLayoutModel *(^SameWidthHeight)();
  47. typedef void (^SpaceToSuperView)(UIEdgeInsets insets);
  48. @interface SDAutoLayoutModel : NSObject
  49. /*
  50. *************************说明************************
  51. 方法名中带有“SpaceToView”的需要传递2个参数:(UIView)参照view 和 (CGFloat)间距数值
  52. 方法名中带有“RatioToView”的需要传递2个参数:(UIView)参照view 和 (CGFloat)倍数
  53. 方法名中带有“EqualToView”的需要传递1个参数:(UIView)参照view
  54. 方法名中带有“Is”的需要传递1个参数:(CGFloat)数值
  55. *****************************************************
  56. */
  57. /* 设置距离其它view的间距 */
  58. /** 左边到其参照view之间的间距,参数为“(View, CGFloat)” */
  59. @property (nonatomic, copy, readonly) MarginToView leftSpaceToView;
  60. /** 右边到其参照view之间的间距,参数为“(View, CGFloat)” */
  61. @property (nonatomic, copy, readonly) MarginToView rightSpaceToView;
  62. /** 顶部到其参照view之间的间距,参数为“(View, CGFloat)” */
  63. @property (nonatomic, copy, readonly) MarginToView topSpaceToView;
  64. /** 底部到其参照view之间的间距,参数为“(View, CGFloat)” */
  65. @property (nonatomic, copy, readonly) MarginToView bottomSpaceToView;
  66. /* 设置x、y、width、height、centerX、centerY 值 */
  67. /** x值,参数为“(CGFloat)” */
  68. @property (nonatomic, copy, readonly) Margin xIs;
  69. /** y值,参数为“(CGFloat)” */
  70. @property (nonatomic, copy, readonly) Margin yIs;
  71. /** centerX值,参数为“(CGFloat)” */
  72. @property (nonatomic, copy, readonly) Margin centerXIs;
  73. /** centerY值,参数为“(CGFloat)” */
  74. @property (nonatomic, copy, readonly) Margin centerYIs;
  75. /** 宽度值,参数为“(CGFloat)” */
  76. @property (nonatomic, copy, readonly) WidthHeight widthIs;
  77. /** 高度值,参数为“(CGFloat)” */
  78. @property (nonatomic, copy, readonly) WidthHeight heightIs;
  79. /* 设置最大宽度和高度、最小宽度和高度 */
  80. /** 最大宽度值,参数为“(CGFloat)” */
  81. @property (nonatomic, copy, readonly) WidthHeight maxWidthIs;
  82. /** 最大高度值,参数为“(CGFloat)” */
  83. @property (nonatomic, copy, readonly) WidthHeight maxHeightIs;
  84. /** 最小宽度值,参数为“(CGFloat)” */
  85. @property (nonatomic, copy, readonly) WidthHeight minWidthIs;
  86. /** 最小高度值,参数为“(CGFloat)” */
  87. @property (nonatomic, copy, readonly) WidthHeight minHeightIs;
  88. /* 设置和某个参照view的边距相同 */
  89. /** 左间距与参照view相同,参数为“(View)” */
  90. @property (nonatomic, copy, readonly) MarginEqualToView leftEqualToView;
  91. /** 右间距与参照view相同,参数为“(View)” */
  92. @property (nonatomic, copy, readonly) MarginEqualToView rightEqualToView;
  93. /** 顶部间距与参照view相同,参数为“(View)” */
  94. @property (nonatomic, copy, readonly) MarginEqualToView topEqualToView;
  95. /** 底部间距与参照view相同,参数为“(View)” */
  96. @property (nonatomic, copy, readonly) MarginEqualToView bottomEqualToView;
  97. /** centerX与参照view相同,参数为“(View)” */
  98. @property (nonatomic, copy, readonly) MarginEqualToView centerXEqualToView;
  99. /** centerY与参照view相同,参数为“(View)” */
  100. @property (nonatomic, copy, readonly) MarginEqualToView centerYEqualToView;
  101. /* 设置宽度或者高度等于参照view的多少倍 */
  102. /** 宽度是参照view宽度的多少倍,参数为“(View, CGFloat)” */
  103. @property (nonatomic, copy, readonly) WidthHeightEqualToView widthRatioToView;
  104. /** 高度是参照view高度的多少倍,参数为“(View, CGFloat)” */
  105. @property (nonatomic, copy, readonly) WidthHeightEqualToView heightRatioToView;
  106. /** 设置一个view的宽度和它的高度相同,参数为空“()” */
  107. @property (nonatomic, copy, readonly) SameWidthHeight widthEqualToHeight;
  108. /** 设置一个view的高度和它的宽度相同,参数为空“()” */
  109. @property (nonatomic, copy, readonly) SameWidthHeight heightEqualToWidth;
  110. /** 自适应高度,传入高宽比值,label可以传0实现文字高度自适应 */
  111. @property (nonatomic, copy, readonly) AutoHeight autoHeightRatio;
  112. /* 填充父view(快捷方法) */
  113. /** 传入UIEdgeInsetsMake(top, left, bottom, right),可以快捷设置view到其父view上左下右的间距 */
  114. @property (nonatomic, copy, readonly) SpaceToSuperView spaceToSuperView;
  115. @property (nonatomic, weak) UIView *needsAutoResizeView;
  116. @end
  117. @interface UIView (SDAutoHeightWidth)
  118. /** 设置Cell的高度自适应,也可用于设置普通view内容高度自适应 */
  119. - (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin;
  120. /** 用于设置普通view内容宽度自适应 */
  121. - (void)setupAutoWidthWithRightView:(UIView *)rightView rightMargin:(CGFloat)rightMargin;
  122. /** 设置Cell的高度自适应,也可用于设置普通view内容自适应(应用于当你不确定哪个view在自动布局之后会排布在最下方最为bottomView的时候可以调用次方法将所有可能在最下方的view都传过去) */
  123. - (void)setupAutoHeightWithBottomViewsArray:(NSArray *)bottomViewsArray bottomMargin:(CGFloat)bottomMargin;
  124. /** 主动刷新布局(如果你需要设置完布局代码就获得view的frame请调用此方法) */
  125. - (void)updateLayout;
  126. - (void)clearAutoHeigtSettings;
  127. - (void)clearAutoWidthSettings;
  128. @property (nonatomic) CGFloat autoHeight;
  129. @property (nonatomic, readonly) SDUIViewCategoryManager *sd_categoryManager;
  130. @property (nonatomic, readonly) NSMutableArray *sd_bottomViewsArray;
  131. @property (nonatomic) CGFloat sd_bottomViewBottomMargin;
  132. @property (nonatomic) NSArray *sd_rightViewsArray;
  133. @property (nonatomic) CGFloat sd_rightViewRightMargin;
  134. @end
  135. @interface UIView (SDLayoutExtention)
  136. /** 自动布局完成后的回调block,可以在这里获取到view的真实frame */
  137. @property (nonatomic) void (^didFinishAutoLayoutBlock)(CGRect frame);
  138. /** 添加一组子view */
  139. - (void)sd_addSubviews:(NSArray *)subviews;
  140. /* 设置圆角 */
  141. /** 设置圆角半径值 */
  142. @property (nonatomic, strong) NSNumber *sd_cornerRadius;
  143. /** 设置圆角半径值为view宽度的多少倍 */
  144. @property (nonatomic, strong) NSNumber *sd_cornerRadiusFromWidthRatio;
  145. /** 设置圆角半径值为view高度的多少倍 */
  146. @property (nonatomic, strong) NSNumber *sd_cornerRadiusFromHeightRatio;
  147. /** 设置等宽子view(子view需要在同一水平方向) */
  148. @property (nonatomic, strong) NSArray *sd_equalWidthSubviews;
  149. @end
  150. @interface UIScrollView (SDAutoContentSize)
  151. /** 设置scrollview内容竖向自适应 */
  152. - (void)setupAutoContentSizeWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin;
  153. /** 设置scrollview内容横向自适应 */
  154. - (void)setupAutoContentSizeWithRightView:(UIView *)rightView rightMargin:(CGFloat)rightMargin;
  155. @end
  156. @interface UILabel (SDLabelAutoResize)
  157. /** 是否是attributedString */
  158. @property (nonatomic) BOOL isAttributedContent;
  159. /** 设置单行文本label宽度自适应 */
  160. - (void)setSingleLineAutoResizeWithMaxWidth:(CGFloat)maxWidth;
  161. @end
  162. // ----------------- 以下为此库内部需要用到的类和方法(可以不用看)----------------
  163. @interface SDAutoLayoutModelItem : NSObject
  164. @property (nonatomic, strong) NSNumber *value;
  165. @property (nonatomic, weak) UIView *refView;
  166. @end
  167. @interface UIView (SDAutoLayout)
  168. /** 开始自动布局 */
  169. - (SDAutoLayoutModel *)sd_layout;
  170. /** 清空之前的自动布局设置,重新开始自动布局 */
  171. - (SDAutoLayoutModel *)sd_resetLayout;
  172. /** 清空之前的自动布局设置 */
  173. - (void)sd_clearAutoLayoutSettings;
  174. - (NSMutableArray *)autoLayoutModelsArray;
  175. - (void)addAutoLayoutModel:(SDAutoLayoutModel *)model;
  176. @property (nonatomic) SDAutoLayoutModel *ownLayoutModel;
  177. @property (nonatomic, strong) NSNumber *fixedWith;
  178. @property (nonatomic, strong) NSNumber *fixedHeight;
  179. @property (nonatomic, strong) NSNumber *sd_maxWidth;
  180. @property (nonatomic, strong) NSNumber *autoHeightRatioValue;
  181. @end
  182. @interface UIView (SDChangeFrame)
  183. @property (nonatomic) CGFloat left;
  184. @property (nonatomic) CGFloat top;
  185. @property (nonatomic) CGFloat right;
  186. @property (nonatomic) CGFloat bottom;
  187. @property (nonatomic) CGFloat centerX;
  188. @property (nonatomic) CGFloat centerY;
  189. @property (nonatomic) CGFloat width;
  190. @property (nonatomic) CGFloat height;
  191. @property (nonatomic) CGPoint origin;
  192. @property (nonatomic) CGSize size;
  193. @end
  194. @interface SDUIViewCategoryManager : NSObject
  195. @property (nonatomic, strong) NSArray *rightViewsArray;
  196. @property (nonatomic, assign) CGFloat rightViewRightMargin;
  197. @end
  198. // 版权属于原作者
  199. // http://code4app.com (cn) http://code4app.net (en)
  200. // 发布代码于最专业的源码分享网站: Code4App.com