UIView+ST.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // UIView+ST.m
  3. // Copyright © 2016年 ST. All rights reserved.
  4. //
  5. #import "UIView+ST.h"
  6. @implementation UIView (ST)
  7. - (void)setX:(CGFloat)x
  8. {
  9. CGRect frame = self.frame;
  10. frame.origin.x = x;
  11. self.frame = frame;
  12. }
  13. - (void)setY:(CGFloat)y
  14. {
  15. CGRect frame = self.frame;
  16. frame.origin.y = y;
  17. self.frame = frame;
  18. }
  19. - (CGFloat)x
  20. {
  21. return self.frame.origin.x;
  22. }
  23. - (CGFloat)y
  24. {
  25. return self.frame.origin.y;
  26. }
  27. - (void)setWidth:(CGFloat)width
  28. {
  29. CGRect frame = self.frame;
  30. frame.size.width = width;
  31. self.frame = frame;
  32. }
  33. - (void)setHeight:(CGFloat)height
  34. {
  35. CGRect frame = self.frame;
  36. frame.size.height = height;
  37. self.frame = frame;
  38. }
  39. - (CGFloat)height
  40. {
  41. return self.frame.size.height;
  42. }
  43. - (CGFloat)width
  44. {
  45. return self.frame.size.width;
  46. }
  47. - (UIView * (^)(CGFloat x))setX
  48. {
  49. return ^(CGFloat x) {
  50. self.x = x;
  51. return self;
  52. };
  53. }
  54. - (void)setCenterX:(CGFloat)centerX
  55. {
  56. CGPoint center = self.center;
  57. center.x = centerX;
  58. self.center = center;
  59. }
  60. - (CGFloat)centerX
  61. {
  62. return self.center.x;
  63. }
  64. - (void)setCenterY:(CGFloat)centerY
  65. {
  66. CGPoint center = self.center;
  67. center.y = centerY;
  68. self.center = center;
  69. }
  70. - (CGFloat)centerY
  71. {
  72. return self.center.y;
  73. }
  74. - (void)setSize:(CGSize)size
  75. {
  76. CGRect frame = self.frame;
  77. frame.size = size;
  78. self.frame = frame;
  79. }
  80. - (CGSize)size
  81. {
  82. return self.frame.size;
  83. }
  84. - (void)setOrigin:(CGPoint)origin
  85. {
  86. CGRect frame = self.frame;
  87. frame.origin = origin;
  88. self.frame = frame;
  89. }
  90. - (CGPoint)origin
  91. {
  92. return self.frame.origin;
  93. }
  94. - (CGFloat)left {
  95. return self.frame.origin.x;
  96. }
  97. - (void)setLeft:(CGFloat)x {
  98. CGRect frame = self.frame;
  99. frame.origin.x = x;
  100. self.frame = frame;
  101. }
  102. - (CGFloat)top {
  103. return self.frame.origin.y;
  104. }
  105. - (void)setTop:(CGFloat)y {
  106. CGRect frame = self.frame;
  107. frame.origin.y = y;
  108. self.frame = frame;
  109. }
  110. - (CGFloat)right {
  111. return self.frame.origin.x + self.frame.size.width;
  112. }
  113. - (void)setRight:(CGFloat)right {
  114. CGRect frame = self.frame;
  115. frame.origin.x = right - frame.size.width;
  116. self.frame = frame;
  117. }
  118. - (CGFloat)bottom {
  119. return self.frame.origin.y + self.frame.size.height;
  120. }
  121. - (void)setBottom:(CGFloat)bottom {
  122. CGRect frame = self.frame;
  123. frame.origin.y = bottom - frame.size.height;
  124. self.frame = frame;
  125. }
  126. - (UIView *(^)(UIColor *color)) setColor
  127. {
  128. return ^ (UIColor *color) {
  129. self.backgroundColor = color;
  130. return self;
  131. };
  132. }
  133. - (UIView *(^)(CGRect frame)) setFrame
  134. {
  135. return ^ (CGRect frame) {
  136. self.frame = frame;
  137. return self;
  138. };
  139. }
  140. - (UIView *(^)(CGSize size)) setSize
  141. {
  142. return ^ (CGSize size) {
  143. self.bounds = CGRectMake(0, 0, size.width, size.height);
  144. return self;
  145. };
  146. }
  147. - (UIView *(^)(CGPoint point)) setCenter
  148. {
  149. return ^ (CGPoint point) {
  150. self.center = point;
  151. return self;
  152. };
  153. }
  154. - (UIView *(^)(NSInteger tag)) setTag
  155. {
  156. return ^ (NSInteger tag) {
  157. self.tag = tag;
  158. return self;
  159. };
  160. }
  161. - (void)addTarget:(id)target
  162. action:(SEL)action;
  163. {
  164. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
  165. action:action];
  166. self.userInteractionEnabled = YES;
  167. [self addGestureRecognizer:tap];
  168. }
  169. - (void)addBorderColor:(UIColor *)color{
  170. [self.layer setBorderColor:color.CGColor];
  171. [self.layer setBorderWidth:0.5];
  172. [self.layer setCornerRadius:4];
  173. }
  174. @end