UIViewExt.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Erica Sadun, http://ericasadun.com
  3. iPhone Developer's Cookbook, 3.0 Edition
  4. BSD License, Use at your own risk
  5. */
  6. #import "UIViewExt.h"
  7. CGPoint CGRectGetCenter(CGRect rect)
  8. {
  9. CGPoint pt;
  10. pt.x = CGRectGetMidX(rect);
  11. pt.y = CGRectGetMidY(rect);
  12. return pt;
  13. }
  14. CGRect CGRectMoveToCenter(CGRect rect, CGPoint center)
  15. {
  16. CGRect newrect = CGRectZero;
  17. newrect.origin.x = center.x-CGRectGetMidX(rect);
  18. newrect.origin.y = center.y-CGRectGetMidY(rect);
  19. newrect.size = rect.size;
  20. return newrect;
  21. }
  22. @implementation UIView (ViewGeometry)
  23. // Retrieve and set the origin
  24. - (CGPoint) origin
  25. {
  26. return self.frame.origin;
  27. }
  28. - (void) setOrigin: (CGPoint) aPoint
  29. {
  30. CGRect newframe = self.frame;
  31. newframe.origin = aPoint;
  32. self.frame = newframe;
  33. }
  34. // Retrieve and set the size
  35. - (CGSize) size
  36. {
  37. return self.frame.size;
  38. }
  39. - (void) setSize: (CGSize) aSize
  40. {
  41. CGRect newframe = self.frame;
  42. newframe.size = aSize;
  43. self.frame = newframe;
  44. }
  45. // Query other frame locations
  46. - (CGPoint) bottomRight
  47. {
  48. CGFloat x = self.frame.origin.x + self.frame.size.width;
  49. CGFloat y = self.frame.origin.y + self.frame.size.height;
  50. return CGPointMake(x, y);
  51. }
  52. - (CGPoint) bottomLeft
  53. {
  54. CGFloat x = self.frame.origin.x;
  55. CGFloat y = self.frame.origin.y + self.frame.size.height;
  56. return CGPointMake(x, y);
  57. }
  58. - (CGPoint) topRight
  59. {
  60. CGFloat x = self.frame.origin.x + self.frame.size.width;
  61. CGFloat y = self.frame.origin.y;
  62. return CGPointMake(x, y);
  63. }
  64. // Retrieve and set height, width, top, bottom, left, right
  65. - (CGFloat) height
  66. {
  67. return self.frame.size.height;
  68. }
  69. - (void) setHeight: (CGFloat) newheight
  70. {
  71. CGRect newframe = self.frame;
  72. newframe.size.height = newheight;
  73. self.frame = newframe;
  74. }
  75. - (CGFloat) width
  76. {
  77. return self.frame.size.width;
  78. }
  79. - (void) setWidth: (CGFloat) newwidth
  80. {
  81. CGRect newframe = self.frame;
  82. newframe.size.width = newwidth;
  83. self.frame = newframe;
  84. }
  85. - (CGFloat) top
  86. {
  87. return self.frame.origin.y;
  88. }
  89. - (void) setTop: (CGFloat) newtop
  90. {
  91. CGRect newframe = self.frame;
  92. newframe.origin.y = newtop;
  93. self.frame = newframe;
  94. }
  95. - (CGFloat) left
  96. {
  97. return self.frame.origin.x;
  98. }
  99. - (void) setLeft: (CGFloat) newleft
  100. {
  101. CGRect newframe = self.frame;
  102. newframe.origin.x = newleft;
  103. self.frame = newframe;
  104. }
  105. - (CGFloat) bottom
  106. {
  107. return self.frame.origin.y + self.frame.size.height;
  108. }
  109. - (void) setBottom: (CGFloat) newbottom
  110. {
  111. CGRect newframe = self.frame;
  112. newframe.origin.y = newbottom - self.frame.size.height;
  113. self.frame = newframe;
  114. }
  115. - (CGFloat) right
  116. {
  117. return self.frame.origin.x + self.frame.size.width;
  118. }
  119. - (void) setRight: (CGFloat) newright
  120. {
  121. CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
  122. CGRect newframe = self.frame;
  123. newframe.origin.x += delta ;
  124. self.frame = newframe;
  125. }
  126. // Move via offset
  127. - (void) moveBy: (CGPoint) delta
  128. {
  129. CGPoint newcenter = self.center;
  130. newcenter.x += delta.x;
  131. newcenter.y += delta.y;
  132. self.center = newcenter;
  133. }
  134. // Scaling
  135. - (void) scaleBy: (CGFloat) scaleFactor
  136. {
  137. CGRect newframe = self.frame;
  138. newframe.size.width *= scaleFactor;
  139. newframe.size.height *= scaleFactor;
  140. self.frame = newframe;
  141. }
  142. // Ensure that both dimensions fit within the given size by scaling down
  143. - (void) fitInSize: (CGSize) aSize
  144. {
  145. CGFloat scale;
  146. CGRect newframe = self.frame;
  147. if (newframe.size.height && (newframe.size.height > aSize.height))
  148. {
  149. scale = aSize.height / newframe.size.height;
  150. newframe.size.width *= scale;
  151. newframe.size.height *= scale;
  152. }
  153. if (newframe.size.width && (newframe.size.width >= aSize.width))
  154. {
  155. scale = aSize.width / newframe.size.width;
  156. newframe.size.width *= scale;
  157. newframe.size.height *= scale;
  158. }
  159. self.frame = newframe;
  160. }
  161. @end