美文网首页
一句代码创建常用UI控件

一句代码创建常用UI控件

作者: 指尖书法 | 来源:发表于2017-03-30 18:17 被阅读22次

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

    • Label
    //添加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;
    }
    
    • ImageView
    //添加一张图片
    +(instancetype)xhh_ImageViewWithView:(UIView *)superView imageName:(NSString *)imageName {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.userInteractionEnabled = YES;
        [superView addSubview:imageView];
        return imageView;
    }
    
    • Button
      这个用继承来写,是因为想把button的点击事件集成在创建的代码段中,保证代码的连贯性.因为功能比较多,这个方法写的臭长臭长的....
    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];
        }];
    

    相关文章

      网友评论

          本文标题:一句代码创建常用UI控件

          本文链接:https://www.haomeiwen.com/subject/spepottx.html