OC-各类控件封装(笔记)

UtilAutoView.h

#pragma mark ============>UIView
UIView * insertAutoView(id view, UIColor * color);

#pragma mark =============>UIButton
UIButton *insertAutoButton(id superView,UIImage *normalImage,UIImage *selectedImage,UIColor *bgColor,UIColor *titleNormalColor,UIColor *titleSelectedColor,UIFont *titleFont,NSString *titleStr,UIControlContentHorizontalAlignment slignment);

#pragma mark ==============> UIImageView
UIImageView *insertAutoImage(id superView, UIImage *image,UIColor *bgColor);

#pragma mark ==============> UILabel
UILabel *insertAutoMutiLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr, NSInteger lineNum,NSLineBreakMode lineBreakMode);
UILabel *insertAutoLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr);

#pragma mark ===============>UITextField
UITextField *insertAutoTextField(id superView,NSString*placeholderStr,UIFont *font,NSTextAlignment alignment,UIColor *textColor,UIColor *bgColor);

#pragma mark ===============>UITextView
UITextView *insertAutoTextView(id superView,NSString*placeholderStr,UIFont *font,NSTextAlignment alignment,UIColor *textColor,UIColor *bgColor);

#pragma mark - 创建循环内容显示 -
UILabel *  setContentLabel(id mainView, UIColor * textColor, UIColor * bgColor, UIFont *font,NSTextAlignment align,NSString *contentStr,NSInteger topTag, BOOL isButton, SEL action);

UtilAutoView.m

UIView *insertAutoView(id superView, UIColor *color){
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = color;
    [superView addSubview:view];
    
    return view;
    
}

UIButton *insertAutoButton(id superView,UIImage *normalImage,UIImage *selectedImage,UIColor *bgColor,UIColor *titleNormalColor,UIColor *titleSelectedColor,UIFont *titleFont,NSString *titleStr,UIControlContentHorizontalAlignment slignment){
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
    [btn setBackgroundImage:selectedImage forState:UIControlStateSelected];
    [btn setTitleColor:titleSelectedColor forState:UIControlStateSelected];
    [btn setTitleColor:titleNormalColor forState:UIControlStateNormal];
    [btn setBackgroundColor:bgColor];
    btn.titleLabel.font=titleFont;
    btn.contentHorizontalAlignment = slignment;
    [btn setTitle:titleStr forState:UIControlStateNormal];
    [superView addSubview:btn];
    return btn;
    
};
UIButton *changeAutoButton(id superView,UIImage *normalImage,UIImage *selectedImage,UIColor *bgColor,UIColor *titleNormalColor,UIColor *titleSelectedColor,UIFont *titleFont,NSString *titleStr){
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setTitleColor:titleSelectedColor forState:UIControlStateSelected];
    [btn setTitleColor:titleNormalColor forState:UIControlStateNormal];
    [btn setBackgroundColor:bgColor];
    btn.titleLabel.font=titleFont;
    [btn setTitle:titleStr forState:UIControlStateNormal];
    [superView addSubview:btn];
    return btn;
    
};
UIImageView *insertAutoImage(id superView, UIImage *image,UIColor *bgColor){
    
    UIImageView *img = [[UIImageView alloc]init];
    img.backgroundColor=bgColor;
    [img setImage:image];
    img.contentMode=UIViewContentModeScaleAspectFill;
    img.clipsToBounds=YES;
    [superView addSubview:img];
    return img;
}

UILabel *insertAutoMutiLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr, NSInteger lineNum,NSLineBreakMode lineBreakMode){
    
    UILabel *label=[[UILabel alloc]init];
    label.backgroundColor = bgColor;
    label.font = font;
    label.textAlignment = align;
    label.textColor=textColor;
    label.text =contentStr;
    label.lineBreakMode = lineBreakMode;
    label.numberOfLines = lineNum;
    [superView addSubview:label];
    return label;
}
UILabel *insertAutoLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr){
    
    UILabel *label=[[UILabel alloc]init];
    label.backgroundColor = bgColor;
    label.font = font;
    label.textAlignment = align;
    label.textColor=textColor;
    label.text =contentStr;
    
    [superView addSubview:label];
    return label;
}
#pragma mark - 创建循环内容显示 -
UILabel *  setContentLabel(id mainView, UIColor * textColor, UIColor * bgColor, UIFont *font,NSTextAlignment align,NSString *contentStr,NSInteger topTag, BOOL isButton, SEL action){
    UILabel *label=[[UILabel alloc]init];
    label.backgroundColor = bgColor;
    label.font = font;
    label.textAlignment = align;
    label.textColor=textColor;
    label.text =contentStr;
    [mainView addSubview:label];
    return label;
}

UITextField *insertAutoTextField(id superView,NSString*placeholderStr,UIFont *font,NSTextAlignment alignment,UIColor *textColor,UIColor *bgColor){
    
    UITextField *textField = [[UITextField alloc]init];
    textField.borderStyle = UITextBorderStyleNone;
    textField.placeholder=placeholderStr;
    textField.autocorrectionType = UITextAutocorrectionTypeYes;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeNever;
    textField.font = font;
    textField.textAlignment = alignment;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.textColor= textColor;
    [textField setBackgroundColor:bgColor];
    [superView addSubview:textField];
    return textField;
    
    
}
UITextView *insertAutoTextView(id superView,NSString*placeholderStr,UIFont *font,NSTextAlignment alignment,UIColor *textColor,UIColor *bgColor){

    UITextView *textView = [[UITextView alloc]init];
    textView.placeholder=placeholderStr;
    textView.font = font;
    textView.textAlignment = alignment;
    textView.textColor= textColor;
    [textView setBackgroundColor:bgColor];
    [superView addSubview:textView];
    return textView;
    
}

UtilView.h

#pragma mark ============>UIView
//设置常见View
UIView *insertView(id view, UIColor *color,CGRect rect);

UIView * insertContentView(id mainView, CGFloat w, NSString * title,NSString * personNum,NSInteger tag, BOOL isLast, CGFloat h);

#pragma mark =============>UIButton
//设置标题Button
UIButton *insertTitleButton(id superView, UIColor *textColor, CGRect rect, id target,SEL action, NSInteger tag, NSString *text,UIFont *font,UIColor *h_Color);
//设置ImageButton
UIButton *insertImageButton(id superView, UIColor *textColor, CGRect rect, id target,SEL action, UIImage *image_n,UIImage *image_h,NSInteger tag, NSString *text,UIFont *font,UIColor *h_Color);

#pragma mark ==============> UIImageView
//设置常见ImageView
UIImageView *insertImage(id superView, UIImage *image, CGRect rect);

UILabel *insertMutiLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr, NSInteger lineNum,NSLineBreakMode lineBreakMode ,CGRect rect);

UILabel *insertLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr,CGRect rect);

UtilView.m

UIView *insertView(id superView, UIColor *color,CGRect rect){
    
    UIView *view = [[UIView alloc]initWithFrame:rect];
    view.backgroundColor = color;
    [superView addSubview:view];
    
    return view;
}

UIView * insertContentView(id mainView, CGFloat w, NSString * title,NSString * personNum,NSInteger tag, BOOL isLast, CGFloat h){
    UIView * selfView = insertView(mainView,[UIColor clearColor], CGRectMake(tag*w, 0, w, h));
    UILabel * titleLabel = insertAutoLabel(selfView, RGBA(254, 227, 225,1), [UIColor clearColor], Regular_28, NSTextAlignmentCenter, title);
    UILabel * numLabel = insertAutoLabel(selfView, [UIColor whiteColor], [UIColor clearColor], Medium_40, NSTextAlignmentCenter, personNum);
    UIView * lineView = insertAutoView(selfView, RGBA(255, 154, 153,1));
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfView.mas_top).offset(20);
        make.centerX.equalTo(selfView.mas_centerX);
        make.height.mas_offset(15);
    }];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(5);
        make.centerX.equalTo(selfView.mas_centerX);
        make.height.mas_offset(30);
    }];
    if (isLast==YES){
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(selfView.mas_right);
            make.centerY.equalTo(selfView.mas_centerY);
            make.height.mas_offset(40);
            make.width.mas_offset(1);
        }];
    }
    return selfView;
}

UIButton *insertTitleButton(id superView, UIColor *textColor, CGRect rect, id target,SEL action, NSInteger tag, NSString *text, UIFont *font,UIColor *h_Color){
    return insertImageButton(superView, textColor, rect, target, action, nil, nil, tag, text, font,h_Color);
}


UIButton *insertImageButton(id superView, UIColor *textColor, CGRect rect, id target,SEL action, UIImage *image_n,UIImage *image_h,NSInteger tag, NSString *text,UIFont *font,UIColor *h_Color){
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:rect];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    if (nil != image_n) {
        [button setBackgroundImage:image_n forState:UIControlStateNormal];
    }
    
    if (nil != image_h){
        [button setBackgroundImage:image_h forState:UIControlStateNormal];
    }
    
    button.tag = tag;
    
    if (text.length > 1) {
        [button setTitle:text forState:UIControlStateNormal];
        [button setTitleColor:textColor forState:UIControlStateNormal];
        [button setTitleColor:h_Color forState:UIControlStateHighlighted];
    }
    
    button.titleLabel.font = font;
    
    [superView addSubview:button];
    
    return button;
    
}

UIImageView *insertImage(id superView, UIImage *image, CGRect rect){
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:rect];
    img.contentMode=UIViewContentModeScaleAspectFill;
    img.clipsToBounds=YES;
    img.layer.masksToBounds=YES;

    [img setImage:image];
    
    [superView addSubview:img];
    
    return img;
}

UILabel *insertMutiLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr, NSInteger lineNum,NSLineBreakMode lineBreakMode ,CGRect rect){
    
    UILabel *label=[[UILabel alloc]initWithFrame: rect];
    label.backgroundColor = bgColor;
    label.font = font;
    label.textAlignment = align;
    label.textColor=textColor;
    label.text =contentStr;
    label.lineBreakMode = lineBreakMode;
    label.numberOfLines = lineNum;
    [superView addSubview:label];
    return label;
}
UILabel *insertLabel(id superView,UIColor *textColor,UIColor *bgColor,UIFont *font,NSTextAlignment align,NSString *contentStr,CGRect rect){
    
    UILabel *label=[[UILabel alloc]initWithFrame: rect];
    label.backgroundColor = bgColor;
    label.font = font;
    label.textAlignment = align;
    label.textColor=textColor;
    label.text =contentStr;
    
    [superView addSubview:label];
    return label;
}

转载于:https://www.cnblogs.com/Dordly/p/9995220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值