OC常用控件封装

时间:2022-08-10 21:55:17
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CreateUI : NSObject
#pragma mark viewRadius

+(void)radiusWithView:(UIView *)radiusView withRadius:(CGFloat)radius;
+(void)radiuCornerWithColor:(UIView *)aView  radius:(int)radius colorStr:(NSString *)colorStr;

//添加下边框
+(void)addFrameWithBottomView:(UIView *)borderView borderHeight:(CGFloat)borderHiehgt withBorderColor:(NSString *)frameColorStr;
//添加右边框
+(void)addFrameWithRightView:(UIView *)borderView borderWidth:(CGFloat)borderWidth withBorderColor:(NSString *)frameColorStr;
//添加下划线,左右和父视图相等
+(void)addFrameWithBottomViewWithParentsWithView:(UIView *)view;

#pragma mark UIView
+(UIView *)createViewWithBGColorStr:(NSString *)bgColorStr;
+(UIView *)createViewWithBGColorStr:(NSString *)bgColorStr withRadius:(CGFloat)radius;
/*
 报表help的view
 */
+ (UIView *)createAlertViewWithTitle:(NSString *)title withContent:(NSString *)content;


#pragma mark UILabel
+(UILabel *)createLabelWithTextColorStr:(NSString *)textColorStr withFontSize:(float )fontSize;
+(UILabel *)createLabelWithTextColorStr:(NSString *)textColorStr withFontSize:(float )fontSize withText:(NSString *)textStr;

#pragma mark UIButton
+(UIButton *)createGraySearchBtn;
+(UIButton *)createBtnWithTile:(NSString *)titleStr withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize ;
+(UIButton *)createBtnWithNormalimg:(NSString *)normalImg withSelectedImg:(NSString *)selectImag ;
+(UIButton *)createBtnWithTile:(NSString *)titleStr withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize withNormalimg:(NSString *)normalImg withSelectedImg:(NSString *)selectImag;



#pragma mark UITableView
+(UITableView *)createTableViewWithNoSep;


#pragma mark UITextField
+(UITextField *)createTFWithPlaceholder:(NSString *)placeholder withFontsize:(CGFloat)fontsize withColorStr:(NSString *)textColor;
+(void)textfieldWith:(UITextField *)tf placeholder:(NSString *)placeholder withFontsize:(CGFloat)fontsize withColorStr:(NSString *)textColor;


#pragma mark UISwitch
+(UISwitch *)createSwithWithOnBGColorStr:(NSString *)bgColorStr;

@end

 

#import "CreateUI.h"
#import "Masonry.h"
#import "DefineStr.h"
@implementation CreateUI

+(void)radiusWithView:(UIView *)radiusView withRadius:(CGFloat)radius{
    radiusView.clipsToBounds = YES;
    radiusView.layer.cornerRadius = radius;
}
+(void)radiuCornerWithColor:(UIView *)aView radius:(int)radius colorStr:(NSString *)colorStr{
    aView.clipsToBounds = YES;
    aView.layer.borderWidth = 1.0f;
    aView.layer.borderColor = [DefineStr colorWithHexString:colorStr].CGColor;
    aView.layer.cornerRadius = radius;
}
+(void)addFrameWithBottomView:(UIView *)borderView borderHeight:(CGFloat)borderHiehgt withBorderColor:(NSString *)frameColorStr{
    UIView *line = [CreateUI createViewWithBGColorStr:frameColorStr];
    [borderView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(borderHiehgt);
    }];
}
+(void)addFrameWithRightView:(UIView *)borderView borderWidth:(CGFloat)borderWidth withBorderColor:(NSString *)frameColorStr{
    UIView *line = [CreateUI createViewWithBGColorStr:frameColorStr];
    [borderView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.right.offset(0);
        make.width.offset(1);
        make.bottom.offset(0);
        
    }];

}
+(void)addFrameWithBottomViewWithParentsWithView:(UIView *)view{
    UILabel *line = [UILabel new];
    line.backgroundColor =[DefineStr colorWithHexString:@"#d6d6d6"];
    
    [[view superview] addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(1);
        make.bottom.equalTo(view.mas_bottom);
    }];

}
+(UIView *)createViewWithBGColorStr:(NSString *)bgColorStr{
    UIView *view = [UIView new];
    view.backgroundColor = [DefineStr colorWithHexString:bgColorStr];
    return view;
}
+(UIView *)createViewWithBGColorStr:(NSString *)bgColorStr withRadius:(CGFloat)radius{
    UIView *view = [CreateUI createViewWithBGColorStr:bgColorStr];
    [CreateUI radiusWithView:view withRadius:radius];
    return view;
}
+ (UIView *)createAlertViewWithTitle:(NSString *)title withContent:(NSString *)content
{
    float textWidth = 260;
    
    float textMargin = 10;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    
    titleLabel.font = [UIFont systemFontOfSize:18];
    
    titleLabel.textColor = [UIColor blackColor];
    
    titleLabel.backgroundColor = [UIColor clearColor];
    
    titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleLabel.text = title;
    
    titleLabel.frame = CGRectMake(0, textMargin, textMargin * 2 + textWidth, 40);
    NSString *message =  content;
    UIFont *textFont = [UIFont systemFontOfSize:15];
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    
    attrs[NSFontAttributeName] = textFont;
    
    CGSize maxSize = CGSizeMake(textWidth-textMargin*2, MAXFLOAT);
    
    CGSize size = [message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textMargin,CGRectGetMaxY(titleLabel.frame) + textMargin,textWidth, size.height)];
    
    textLabel.font = textFont;
    
    textLabel.textColor = [UIColor blackColor];
    
    textLabel.backgroundColor = [UIColor clearColor];
    
    textLabel.lineBreakMode =NSLineBreakByWordWrapping;
    
    textLabel.numberOfLines =0;
    
    textLabel.textAlignment =NSTextAlignmentLeft;
    
    textLabel.text = message;
    
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textWidth + textMargin * 2,CGRectGetMaxY(textLabel.frame)+textMargin)];
    [demoView addSubview:titleLabel];
    [demoView addSubview:textLabel];
    return demoView;
}

+(UILabel *)createLabelWithTextColorStr:(NSString *)textColorStr withFontSize:(float )fontSize{
    UILabel *label = [UILabel new];
    label.textColor = [DefineStr colorWithHexString:textColorStr];
    label.font = [UIFont systemFontOfSize:fontSize];
    return label;
}
+(UILabel *)createLabelWithTextColorStr:(NSString *)textColorStr withFontSize:(float )fontSize withText:(NSString *)textStr{
    UILabel *label = [CreateUI createLabelWithTextColorStr:textColorStr withFontSize:fontSize];
    label.text = textStr;
    return label;
}

+(void)setLabelWithLabel:(UILabel *)label withStr:(NSString *)str withColor:(NSString *)colorStr withFontSize:(float)fontSize withImg:(NSString *)imgStr numberLines:(int)lines {
    if(!str){
        str = @"";
    }
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:str];
    // 修改富文本中的不同文字的样式
    [attri addAttribute:NSForegroundColorAttributeName value:[DefineStr colorWithHexString:colorStr ] range:NSMakeRange(0, str.length)];
    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(0, str.length)];
    // 2.添加表情图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    attch.image = [UIImage imageNamed:imgStr];
    // 设置图片大小
    attch.bounds = CGRectMake(0,0, fontSize, fontSize);
    
    // 创建带有图片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    [attri insertAttributedString:string atIndex:0];// 插入某个位置
    
    // 用label的attributedText属性来使用富文本
    label.attributedText = attri;
    label.numberOfLines = lines;
}

+(UIButton *)createGraySearchBtn{
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setImage:[UIImage imageNamed:@"searchGray"] forState:UIControlStateNormal];
    return searchBtn;
}


+(UIButton *)createBtnWithTile:(NSString *)titleStr withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize  {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:titleStr forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    [btn setTitleColor:[DefineStr colorWithHexString:titleColorStr] forState:UIControlStateNormal];
    return btn;
    
}

+(UIButton *)createBtnWithTile:(NSString *)titleStr radius:(float)radius withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:titleStr forState:UIControlStateNormal];
   
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = radius;
    btn.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    [btn setTitleColor:[DefineStr colorWithHexString:titleColorStr] forState:UIControlStateNormal];
    return btn;

}


+(UIButton *)createBtnWithTile:(NSString *)titleStr withBGColorStr:(NSString *)bgColor radius:(float)radius withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize{
    
    UIButton *btn = [CreateUI createBtnWithTile:titleStr radius:radius withTitleColorStr:titleColorStr withFontSize:fontsize];
    [btn setBackgroundImage:[DefineStr createImageWithColor:[DefineStr colorWithHexString:bgColor]] forState:UIControlStateNormal];
       return btn;
}
+(UIButton *)createBtnWithNormalimg:(NSString *)normalImg withSelectedImg:(NSString *)selectImag {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImag] forState:UIControlStateSelected];
    return btn;
}
+(UIButton *)createBtnWithTile:(NSString *)titleStr withTitleColorStr:(NSString *)titleColorStr withFontSize:(CGFloat)fontsize withNormalimg:(NSString *)normalImg withSelectedImg:(NSString *)selectImag {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:titleStr forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    [btn setTitleColor:[DefineStr colorWithHexString:titleColorStr] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImag] forState:UIControlStateSelected];
    return btn;
    
}

+(UITableView *)createTableViewWithNoSep{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    return tableView;
}
+(UISwitch *)createSwithWithOnBGColorStr:(NSString *)bgColorStr{
    UISwitch *swith = [UISwitch new];
    swith.onTintColor = [DefineStr colorWithHexString:bgColorStr];
    return swith;
}

+(UITextField *)createTFWithPlaceholder:(NSString *)placeholder withFontsize:(CGFloat)fontsize withColorStr:(NSString *)textColor{
    UITextField *field = [UITextField new];
    field.placeholder =  placeholder;
    field.font = [UIFont systemFontOfSize:fontsize];
    field.textColor = [DefineStr colorWithHexString:textColor];
    return field;
}
+(void)textfieldWith:(UITextField *)field placeholder:(NSString *)placeholder withFontsize:(CGFloat)fontsize withColorStr:(NSString *)textColor{
    field.placeholder =  placeholder;
    field.font = [UIFont systemFontOfSize:fontsize];
    field.textColor = [DefineStr colorWithHexString:textColor];
}




@end