一句代码创建常用UI控件

2017-03-30  本文已影响22人  指尖书法

最近为了锻炼自己,练习用纯代码而不用xib编程,在写一些控件的时候,当真是无聊至极,不断的重复,于是想办法讲这些常用小控件直接抽取出来,方便写的时候一句搞定,话不多说,开车了.

//添加label
+(instancetype)xhh_LabelWithView:(UIView *)superView Text:(NSString *)text TextColor:(UIColor *)color Font:(CGFloat)font TextAlignment:(NSTextAlignment)alignment{
    
    UILabel *label = [[UILabel alloc]init];
    label.text = text;
    label.textColor = color;
    label.textAlignment = alignment;
    label.font = [UIFont systemFontOfSize:font];
    [superView addSubview:label];
    return label;
}
//添加一张图片
+(instancetype)xhh_ImageViewWithView:(UIView *)superView imageName:(NSString *)imageName {
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    imageView.userInteractionEnabled = YES;
    [superView addSubview:imageView];
    return imageView;
}
  1. 写一个类,继承自UIButton

  2. 为了方便直接简化block的命名,定义一个含参无返回值得block
    typedef void (^buttonClickBlock)(UIButton *);

  3. 两个方法,一个对象方法,一个类方法,为了规范.
    这里面涉及到block传递参数的知识,比较绕.大概思路就是:
    a. 创建的时候提前写好一个有参代码段
    b. 进入到创建方法中,去添加点击事件,传参(button)给事件的具体实现中
    c. 在具体方法中去实现block.同时参数传给block代码段中.作为代码段中的实参.

//添加button
-(instancetype)initWithView:(UIView *)superView Title:(NSString *)title titleColor:(UIColor *)color titleFont:(CGFloat)font frame:(CGRect)frame BGimageNor:(NSString *)BGimageNor BGimageSel:(NSString *)BGimageSel cornerRadius:(CGFloat)cornerRadius  buttonClickBlock:(buttonClickBlock)buttonClickBlock{
    if (self = [super initWithFrame:frame]) {
        
        self.frame = frame;
        self.titleLabel.font = [UIFont systemFontOfSize:font];
        self.layer.cornerRadius = cornerRadius;
        self.clipsToBounds = YES;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:BGimageNor] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:BGimageSel] forState:UIControlStateSelected];

//在创建的时候将点击事件block传入到这个方法中
        self.Myblock = buttonClickBlock;

//紧接着自己这个类监听点击事件,而具体内容则是对象的属性block表示的代码段.
        [self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [superView addSubview:self];

    }
    
    
    return self;
}

-(void)click:(UIButton *)button {
   
//block是有参数的,参数就是点击的那个button
    self.Myblock(button);
}

+(instancetype)xhhButtonWithView:(UIView *)superView Title:(NSString *)title titleColor:(UIColor *)color titleFont:(CGFloat)font frame:(CGRect)frame BGimageNor:(NSString *)BGimageNor BGimageSel:(NSString *)BGimageSel cornerRadius:(CGFloat)cornerRadius buttonClickBlock:(buttonClickBlock)buttonClickBlock {
    
    return [[self alloc]initWithView:superView Title:title titleColor:color titleFont:font frame:frame BGimageNor:BGimageNor BGimageSel:BGimageSel cornerRadius:cornerRadius buttonClickBlock:buttonClickBlock];
}

这样一来实现的时候会非常方便,一行搞定:


//图片
    UIImageView *leftImg = [UIImageView xhh_ImageViewWithView:self.freeCardView imageName:@"free_icon"];
  
//标签
    UILabel *label = [UILabel xhh_LabelWithView:self.freeCardView Text:@"啦啦啦啦德玛西亚!" TextColor:XHHBlack Font:14 TextAlignment:0];
    
 //按钮
    UIButton *button =[XHHButton xhhButtonWithView:self.freeCardView Title:@"啦啦啦" titleColor:XHHWhite titleFont:14 frame:CGRectZero BGimageNor:@"but" BGimageSel:nil cornerRadius:3 buttonClickBlock:^(UIButton *button) {
        XHHFreeTakeViewController *vc = [[XHHFreeTakeViewController alloc]init];
        XHHMainNavController *mainNAV = (XHHMainNavController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        [mainNAV pushViewController:vc animated:YES];
    }];
上一篇下一篇

猜你喜欢

热点阅读