UIView+Extension.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // UIView+Extension.m
  3. // JWlive
  4. //
  5. // Created by pican zhang on 16/3/17.
  6. // Copyright © 2016年 pican zhang. All rights reserved.
  7. //
  8. #import "UIView+Extension.h"
  9. @implementation UIView (Extension)
  10. - (void)setSize:(CGSize)size
  11. {
  12. CGRect frame = self.frame;
  13. frame.size = size;
  14. self.frame = frame;
  15. }
  16. - (CGSize)size
  17. {
  18. return self.frame.size;
  19. }
  20. - (void)setX:(CGFloat)x
  21. {
  22. CGRect frame = self.frame;
  23. frame.origin.x = x;
  24. self.frame = frame;
  25. }
  26. - (CGFloat)x
  27. {
  28. return self.frame.origin.x;
  29. }
  30. - (void)setY:(CGFloat)y
  31. {
  32. CGRect frame = self.frame;
  33. frame.origin.y = y;
  34. self.frame = frame;
  35. }
  36. - (CGFloat)y
  37. {
  38. return self.frame.origin.y;
  39. }
  40. - (void)setWidth:(CGFloat)width
  41. {
  42. CGRect frame = self.frame;
  43. frame.size.width = width;
  44. self.frame = frame;
  45. }
  46. - (CGFloat)width
  47. {
  48. return self.frame.size.width;
  49. }
  50. - (void)setHeight:(CGFloat)height
  51. {
  52. CGRect frame = self.frame;
  53. frame.size.height = height;
  54. self.frame = frame;
  55. }
  56. - (CGFloat)height
  57. {
  58. return self.frame.size.height;
  59. }
  60. - (void)setCenterX:(CGFloat)centerX
  61. {
  62. CGPoint center = self.center;
  63. center.x = centerX;
  64. self.center = center;
  65. }
  66. - (CGFloat)centerX
  67. {
  68. return self.center.x;
  69. }
  70. - (void)setCenterY:(CGFloat)centerY
  71. {
  72. CGPoint center = self.center;
  73. center.y = centerY;
  74. self.center = center;
  75. }
  76. - (CGFloat)centerY
  77. {
  78. return self.center.y;
  79. }
  80. - (void)setOrigin:(CGPoint)origin
  81. {
  82. CGRect frame = self.frame;
  83. frame.origin = origin;
  84. self.frame = frame;
  85. }
  86. - (CGPoint)origin
  87. {
  88. return self.frame.origin;
  89. }
  90. - (void)HorizonLinePaddingToTop:(CGFloat)top{
  91. UIView* line = [[UIView alloc]init];
  92. line.frame = CGRectMake(0, top, self.frame.size.width, 0.5);
  93. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  94. [self addSubview:line];
  95. }
  96. - (void)VerticalLinePaddingToLeft:(CGFloat)left{
  97. UIView* line = [[UIView alloc]init];
  98. line.frame = CGRectMake(left, 0, 0.5, self.frame.size.height);
  99. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  100. [self addSubview:line];
  101. }
  102. - (void)RightLinePaddingTop:(CGFloat)top{
  103. UIView* line = [[UIView alloc]init];
  104. line.frame = CGRectMake(self.frame.size.width-0.5, top, 0.5, self.frame.size.height-2*top);
  105. line.backgroundColor = [UIColor whiteColor];
  106. [self addSubview:line];
  107. }
  108. - (void)LeftLinePaddingTop:(CGFloat)top{
  109. UIView* line = [[UIView alloc]init];
  110. line.frame = CGRectMake(0, top, 0.5, self.frame.size.height-2*top);
  111. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  112. [self addSubview:line];
  113. }
  114. - (void)topLineforView{
  115. UIView* line = [[UIView alloc]init];
  116. line.frame = CGRectMake(0, 0, KScreenWidth, 0.5);
  117. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  118. [self addSubview:line];
  119. }
  120. - (void)topLineforViewWithColor:(UIColor*)color{
  121. UIView* line = [[UIView alloc]init];
  122. line.frame = CGRectMake(0, 0, KScreenWidth, 0.5);
  123. line.backgroundColor = color;
  124. [self addSubview:line];
  125. }
  126. - (void)topLinepaddingLeft:(CGFloat)left{
  127. UIView* line = [[UIView alloc]init];
  128. line.frame = CGRectMake(left, 0, KScreenWidth-left, 0.5);
  129. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  130. [self addSubview:line];
  131. }
  132. - (void)bottomLineforViewHeight:(CGFloat)height{
  133. UIView* line = [[UIView alloc]init];
  134. line.frame = CGRectMake(0, height-0.5, KScreenWidth, 0.5);
  135. line.backgroundColor = LZF5F4F7Color;
  136. [self addSubview:line];
  137. }
  138. - (void)bottomLineforView{
  139. // UIView* line = [[UIView alloc]init];
  140. // line.frame = CGRectMake(0, self.height-1.0, KScreenWidth, 1.0);
  141. // line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  142. // [self addSubview:line];
  143. }
  144. - (void)bottomLineforViewWithColor:(UIColor*)color{
  145. UIView* line = [[UIView alloc]init];
  146. line.frame = CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width, 0.5);
  147. NSLog(@"%@",NSStringFromCGRect(line.frame));
  148. line.backgroundColor =color;
  149. [self addSubview:line];
  150. }
  151. - (void)bottomWideLineforView:(CGFloat)height{
  152. UIView* line = [[UIView alloc]init];
  153. line.frame = CGRectMake(0, self.frame.size.height-height, self.frame.size.width, height);
  154. line.backgroundColor = HexColorFromRGB(0xe6e6e6);
  155. [self addSubview:line];
  156. }
  157. - (void)addLineWithColor:(UIColor *)color lineRect:(CGRect)rect{
  158. UIView* line = [[UIView alloc]init];
  159. line.frame = rect;
  160. line.backgroundColor = color;
  161. [self addSubview:line];
  162. }
  163. - (UIViewController *)viewController{
  164. for (UIView* next = self; next; next = next.superview) {
  165. UIResponder* nextResponder = [next nextResponder];
  166. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  167. return (UIViewController*)nextResponder;
  168. }
  169. }
  170. return nil;
  171. }
  172. - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(UIView *)view {
  173. if (!view) {
  174. if ([self isKindOfClass:[UIWindow class]]) {
  175. return [((UIWindow *)self) convertPoint:point toWindow:nil];
  176. } else {
  177. return [self convertPoint:point toView:nil];
  178. }
  179. }
  180. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  181. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  182. if ((!from || !to) || (from == to)) return [self convertPoint:point toView:view];
  183. point = [self convertPoint:point toView:from];
  184. point = [to convertPoint:point fromWindow:from];
  185. point = [view convertPoint:point fromView:to];
  186. return point;
  187. }
  188. - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(UIView *)view {
  189. if (!view) {
  190. if ([self isKindOfClass:[UIWindow class]]) {
  191. return [((UIWindow *)self) convertPoint:point fromWindow:nil];
  192. } else {
  193. return [self convertPoint:point fromView:nil];
  194. }
  195. }
  196. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  197. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  198. if ((!from || !to) || (from == to)) return [self convertPoint:point fromView:view];
  199. point = [from convertPoint:point fromView:view];
  200. point = [to convertPoint:point fromWindow:from];
  201. point = [self convertPoint:point fromView:to];
  202. return point;
  203. }
  204. - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(UIView *)view {
  205. if (!view) {
  206. if ([self isKindOfClass:[UIWindow class]]) {
  207. return [((UIWindow *)self) convertRect:rect toWindow:nil];
  208. } else {
  209. return [self convertRect:rect toView:nil];
  210. }
  211. }
  212. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  213. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  214. if (!from || !to) return [self convertRect:rect toView:view];
  215. if (from == to) return [self convertRect:rect toView:view];
  216. rect = [self convertRect:rect toView:from];
  217. rect = [to convertRect:rect fromWindow:from];
  218. rect = [view convertRect:rect fromView:to];
  219. return rect;
  220. }
  221. - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(UIView *)view {
  222. if (!view) {
  223. if ([self isKindOfClass:[UIWindow class]]) {
  224. return [((UIWindow *)self) convertRect:rect fromWindow:nil];
  225. } else {
  226. return [self convertRect:rect fromView:nil];
  227. }
  228. }
  229. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  230. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  231. if ((!from || !to) || (from == to)) return [self convertRect:rect fromView:view];
  232. rect = [from convertRect:rect fromView:view];
  233. rect = [to convertRect:rect fromWindow:from];
  234. rect = [self convertRect:rect fromView:to];
  235. return rect;
  236. }
  237. @end