UIView+Setter.m 5.2 KB

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