UIView+Setter.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import "UIView+Setter.h"
  2. @implementation UIView(Setter)
  3. -(void)boardWid:(CGFloat)wid Color:(UIColor*)col
  4. {
  5. self.layer.borderColor = col.CGColor;
  6. self.layer.borderWidth = wid;
  7. }
  8. -(void)corner:(CGFloat)cor
  9. {
  10. self.layer.cornerRadius = cor;
  11. self.layer.masksToBounds = YES;
  12. }
  13. -(void)scale:(CGFloat)rate
  14. {//CGAffineTransformIdentity
  15. // self.transform = CGAffineTransformScale(self.transform, rate,rate);
  16. self.transform = CGAffineTransformScale(CGAffineTransformIdentity, rate,rate);
  17. }
  18. -(void)addViewWithRect:(CGRect)re Color:(UIColor*)color
  19. {
  20. UIView *v = [[UIView alloc] initWithFrame:re];
  21. [v setBackgroundColor:color];
  22. [self.superview addSubview:v];
  23. }
  24. -(void)addViewWithRect:(CGRect)re
  25. {
  26. [self addViewWithRect:re Color:lineColor];
  27. }
  28. -(void)addSelfViewWithRect:(CGRect)re
  29. {
  30. UIView *v = [[UIView alloc] initWithFrame:re];
  31. [v setBackgroundColor:lineColor];
  32. [self addSubview:v];
  33. }
  34. @end
  35. @implementation UIViewController(Setter)
  36. -(void)configNavBar
  37. {
  38. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
  39. [backBbi setTintColor:defGreen];
  40. [self.navigationItem setLeftBarButtonItem:backBbi];
  41. }
  42. -(void)goBack{
  43. [self.navigationController popViewControllerAnimated:YES];
  44. }
  45. -(void)configNavigationBarDismissNav
  46. {
  47. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNav)];
  48. [backBbi setTintColor:defGreen];
  49. self.navigationController.navigationBar.translucent = NO;
  50. [self.navigationItem setLeftBarButtonItem:backBbi];
  51. }
  52. -(void)dismissNav
  53. {
  54. [self.view endEditing:YES];
  55. [self.navigationController dismissViewControllerAnimated:NO completion:nil];
  56. }
  57. -(void)addV:(UIView*)aView
  58. {
  59. [self.view addSubview:aView];
  60. }
  61. @end
  62. @implementation UIImageView(Setter)
  63. -(void)imageName:(NSString*)name
  64. {
  65. [self setImage:[UIImage imageNamed:name]];
  66. }
  67. @end
  68. @implementation UIButton(Setter)
  69. - (void) setPersonalImage:(UIImage *)img Tit:(NSString *)tit Font:(CGFloat)font State:(UIControlState)state
  70. {
  71. [self.titleLabel setContentMode:UIViewContentModeCenter];
  72. [self setTitleColor:[UIColor whiteColor] forState:state];
  73. [self setTitle:tit forState:state];
  74. [self.titleLabel setFont:[UIFont scaleSize:font]];
  75. [self setImage:img forState:state];
  76. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  77. //文字图片位置调整
  78. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  79. // self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  80. //适应字体及内容,否则titleLabel的尺寸为(0,0)
  81. [self.titleLabel sizeToFit];
  82. //设置图标四周边界
  83. [self setImageEdgeInsets:UIEdgeInsetsMake(10, 0, 10, 20)];
  84. //设置标题四周边界
  85. [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
  86. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  87. }
  88. - (void) setImage:(UIImage *)img Tit:(NSString *)tit Font:(CGFloat)font State:(UIControlState)state
  89. {
  90. [self.titleLabel setContentMode:UIViewContentModeCenter];
  91. [self setTitleColor:titleColor forState:state];
  92. [self setTitle:tit forState:state];
  93. [self.titleLabel setFont:[UIFont scaleSize:font]];
  94. [self setImage:img forState:state];
  95. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  96. }
  97. -(void) setTitle:(NSString*)title textColor:(UIColor*)color font:(CGFloat)font fotState:(UIControlState)stateType
  98. {
  99. [self setTitle:title forState:stateType];
  100. [self setTitleColor:color forState:stateType];
  101. [self.titleLabel setFont:[UIFont scaleSize:font]];
  102. //scaleSize
  103. }
  104. -(void)target:(id)obj
  105. {
  106. [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside];
  107. }
  108. -(void)target:(id)obj Tag:(NSInteger)tag
  109. {
  110. [self target:obj];
  111. [self setTag:tag];
  112. }
  113. @end
  114. @implementation UILabel(Setter)
  115. -(void)setFont:(CGFloat)font TextColor:(UIColor*)color
  116. {
  117. [self setFont:[UIFont scaleSize:font]];
  118. [self setTextColor:color];
  119. }
  120. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color
  121. {
  122. [self setText:text];
  123. [self setFont:[UIFont scaleSize:font]];
  124. [self setTextColor:color];
  125. }
  126. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color Alignment:(NSTextAlignment)align
  127. {
  128. [self setText:text Font:font TextColor:color];
  129. [self setTextAlignment:align];
  130. }
  131. -(void)boardWid:(CGFloat)wid Color:(UIColor*)col
  132. {
  133. self.layer.borderColor = col.CGColor;
  134. self.layer.borderWidth = wid;
  135. }
  136. -(void)corner:(CGFloat)cor
  137. {
  138. self.layer.cornerRadius = cor;
  139. self.layer.masksToBounds = YES;
  140. }
  141. @end
  142. void SetRandomColor(UIView* vi)
  143. {
  144. UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
  145. [vi setBackgroundColor:color];
  146. }
  147. void SetRandomColorS(UIView* vi)
  148. {
  149. for (UIView* v in vi.subviews) {
  150. SetRandomColor(v);
  151. }
  152. }