#import "UIView+Setter.h" @implementation UIView(Setter) -(void)boardWid:(CGFloat)wid Color:(UIColor*)col { self.layer.borderColor = col.CGColor; self.layer.borderWidth = wid; } -(void)corner:(CGFloat)cor { self.layer.cornerRadius = cor; self.layer.masksToBounds = YES; } -(void)scale:(CGFloat)rate {//CGAffineTransformIdentity // self.transform = CGAffineTransformScale(self.transform, rate,rate); self.transform = CGAffineTransformScale(CGAffineTransformIdentity, rate,rate); } -(void)addViewWithRect:(CGRect)re Color:(UIColor*)color { UIView *v = [[UIView alloc] initWithFrame:re]; [v setBackgroundColor:color]; [self.superview addSubview:v]; } -(void)addViewWithRect:(CGRect)re { [self addViewWithRect:re Color:lineColor]; } -(void)addSelfViewWithRect:(CGRect)re { UIView *v = [[UIView alloc] initWithFrame:re]; [v setBackgroundColor:lineColor]; [self addSubview:v]; } @end @implementation UIViewController(Setter) -(void)configNavBar { UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)]; [backBbi setTintColor:defGreen]; [self.navigationItem setLeftBarButtonItem:backBbi]; } -(void)goBack{ [self.navigationController popViewControllerAnimated:YES]; } -(void)configNavigationBarDismissNav { UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNav)]; [backBbi setTintColor:defGreen]; self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBbi]; } -(void)dismissNav { [self.view endEditing:YES]; [self.navigationController dismissViewControllerAnimated:NO completion:nil]; } -(void)addV:(UIView*)aView { [self.view addSubview:aView]; } @end @implementation UIImageView(Setter) -(void)imageName:(NSString*)name { [self setImage:[UIImage imageNamed:name]]; } @end @implementation UIButton(Setter) - (void) setPersonalImage:(UIImage *)img Tit:(NSString *)tit Font:(CGFloat)font State:(UIControlState)state { [self.titleLabel setContentMode:UIViewContentModeCenter]; [self setTitleColor:[UIColor whiteColor] forState:state]; [self setTitle:tit forState:state]; [self.titleLabel setFont:[UIFont scaleSize:font]]; [self setImage:img forState:state]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; //文字图片位置调整 self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; // self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //适应字体及内容,否则titleLabel的尺寸为(0,0) [self.titleLabel sizeToFit]; //设置图标四周边界 [self setImageEdgeInsets:UIEdgeInsetsMake(10, 0, 10, 20)]; //设置标题四周边界 [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; } - (void) setImage:(UIImage *)img Tit:(NSString *)tit Font:(CGFloat)font State:(UIControlState)state { [self.titleLabel setContentMode:UIViewContentModeCenter]; [self setTitleColor:titleColor forState:state]; [self setTitle:tit forState:state]; [self.titleLabel setFont:[UIFont scaleSize:font]]; [self setImage:img forState:state]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; } -(void) setTitle:(NSString*)title textColor:(UIColor*)color font:(CGFloat)font fotState:(UIControlState)stateType { [self setTitle:title forState:stateType]; [self setTitleColor:color forState:stateType]; [self.titleLabel setFont:[UIFont scaleSize:font]]; //scaleSize } -(void)target:(id)obj { [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside]; } -(void)target:(id)obj Tag:(NSInteger)tag { [self target:obj]; [self setTag:tag]; } @end @implementation UILabel(Setter) -(void)setFont:(CGFloat)font TextColor:(UIColor*)color { [self setFont:[UIFont scaleSize:font]]; [self setTextColor:color]; } -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color { [self setText:text]; [self setFont:[UIFont scaleSize:font]]; [self setTextColor:color]; } -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color Alignment:(NSTextAlignment)align { [self setText:text Font:font TextColor:color]; [self setTextAlignment:align]; } -(void)boardWid:(CGFloat)wid Color:(UIColor*)col { self.layer.borderColor = col.CGColor; self.layer.borderWidth = wid; } -(void)corner:(CGFloat)cor { self.layer.cornerRadius = cor; self.layer.masksToBounds = YES; } @end void SetRandomColor(UIView* vi) { UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0]; [vi setBackgroundColor:color]; } void SetRandomColorS(UIView* vi) { for (UIView* v in vi.subviews) { SetRandomColor(v); } }