UIView+Additions.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // UIViewAdditions.m
  3. //
  4. #import "UIView+Additions.h"
  5. #import <QuartzCore/QuartzCore.h>
  6. @implementation UIView (Additions)
  7. - (id)initWithParent:(UIView *)parent {
  8. self = [self initWithFrame:CGRectZero];
  9. if (!self)
  10. return nil;
  11. [parent addSubview:self];
  12. return self;
  13. }
  14. + (id) viewWithParent:(UIView *)parent {
  15. return [[self alloc] initWithParent:parent];
  16. }
  17. - (void)setBackgroundImage:(UIImage *)image
  18. {
  19. UIGraphicsBeginImageContext(self.frame.size);
  20. [image drawInRect:self.bounds];
  21. UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext();
  22. UIGraphicsEndImageContext();
  23. self.backgroundColor = [UIColor colorWithPatternImage:bgImage];
  24. }
  25. - (UIImage*)toImage
  26. {
  27. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
  28. CGContextRef ctx = UIGraphicsGetCurrentContext();
  29. [self.layer renderInContext:ctx];
  30. UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
  31. UIGraphicsEndImageContext();
  32. return tImage;
  33. }
  34. - (CGPoint)position {
  35. return self.frame.origin;
  36. }
  37. - (void)setPosition:(CGPoint)position {
  38. CGRect rect = self.frame;
  39. rect.origin = position;
  40. [self setFrame:rect];
  41. }
  42. - (CGFloat)x {
  43. return self.frame.origin.x;
  44. }
  45. - (void)setX:(CGFloat)x {
  46. CGRect rect = self.frame;
  47. rect.origin.x = x;
  48. [self setFrame:rect];
  49. }
  50. - (CGFloat)y {
  51. return self.frame.origin.y;
  52. }
  53. - (void)setY:(CGFloat)y {
  54. CGRect rect = self.frame;
  55. rect.origin.y = y;
  56. [self setFrame:rect];
  57. }
  58. - (CGFloat)left {
  59. return self.frame.origin.x;
  60. }
  61. - (void)setLeft:(CGFloat)x {
  62. CGRect frame = self.frame;
  63. frame.origin.x = x;
  64. self.frame = frame;
  65. }
  66. - (CGFloat)top {
  67. return self.frame.origin.y;
  68. }
  69. - (void)setTop:(CGFloat)y {
  70. CGRect frame = self.frame;
  71. frame.origin.y = y;
  72. self.frame = frame;
  73. }
  74. - (CGFloat)right {
  75. return self.frame.origin.x + self.frame.size.width;
  76. }
  77. - (void)setRight:(CGFloat)right {
  78. CGRect frame = self.frame;
  79. frame.origin.x = right - frame.size.width;
  80. self.frame = frame;
  81. }
  82. - (CGFloat)bottom {
  83. return self.frame.origin.y + self.frame.size.height;
  84. }
  85. - (void)setBottom:(CGFloat)bottom {
  86. CGRect frame = self.frame;
  87. frame.origin.y = bottom - frame.size.height;
  88. self.frame = frame;
  89. }
  90. - (BOOL)visible {
  91. return !self.hidden;
  92. }
  93. - (void)setVisible:(BOOL)visible {
  94. self.hidden=!visible;
  95. }
  96. -(void)removeAllSubViews{
  97. for (UIView *subview in self.subviews){
  98. [subview removeFromSuperview];
  99. }
  100. }
  101. - (CGSize)size {
  102. return [self frame].size;
  103. }
  104. - (void)setSize:(CGSize)size {
  105. CGRect rect = self.frame;
  106. rect.size = size;
  107. [self setFrame:rect];
  108. }
  109. - (CGFloat)width {
  110. return self.frame.size.width;
  111. }
  112. - (void)setWidth:(CGFloat)width {
  113. CGRect rect = self.frame;
  114. rect.size.width = width;
  115. [self setFrame:rect];
  116. }
  117. - (CGFloat)height {
  118. return self.frame.size.height;
  119. }
  120. - (void)setHeight:(CGFloat)height {
  121. CGRect rect = self.frame;
  122. rect.size.height = height;
  123. [self setFrame:rect];
  124. }
  125. @end
  126. @implementation UIImageView (MFAdditions)
  127. - (void) setImageWithName:(NSString *)name {
  128. [self setImage:[UIImage imageNamed:name]];
  129. [self sizeToFit];
  130. }
  131. @end