类方法创建控件

来源:互联网 发布:linux执行器 编辑:程序博客网 时间:2024/06/07 02:41

#pragma mark - 获取一个字符串的Size

+ (CGSize )getSizeWithString:(NSString *)str WithSize:(CGSize )allowSize font:(NSInteger )font {

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init];

    paragraphStyle.lineBreakMode =NSLineBreakByWordWrapping;

    NSDictionary *attributes =@{NSFontAttributeName:[UIFontsystemFontOfSize:font],

                                 NSParagraphStyleAttributeName:paragraphStyle.copy};

    CGSize size = [strboundingRectWithSize:allowSize

                                    options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading

                                 attributes:attributescontext:nil].size;

    

    returnCGSizeMake(size.width,ceilf(size.height));

}



#pragma mark - 创建一个Button

+ (UIButton *)creatButonWithType:(UIButtonType )type

                           Frame:(CGRect )frame

                           title:(NSString *)title

                      titleColor:(UIColor *)titleColor

                           image:(UIImage *)image font:(NSInteger )font {

    

    UIButton *button = [UIButtonbuttonWithType:type];

    button.frame = frame;

    [button setTitle:titleforState:UIControlStateNormal];

    [button setTitleColor:titleColorforState:UIControlStateNormal];

    [button setImage:imageforState:UIControlStateNormal];

    

    button.titleLabel.font = [UIFontsystemFontOfSize:font];

    

    return button;

}



#pragma mark 创建一个Label

+ (UILabel *)creatLabelWithFrame:(CGRect )frame

                           title:(NSString *)title

                      titleColor:(UIColor *)titleColor

                            font:(NSInteger )font {

    UILabel *label = [[UILabelalloc] initWithFrame:frame];

    label.text = title;

    label.font = [UIFontsystemFontOfSize:font];

    label.textColor = titleColor;

    

    return label;

}



#pragma mark  textField

+(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder{

    UITextField *textField = [[UITextFieldalloc]initWithFrame:frame];

    

    textField.font = font;

    

    textField.textColor = [UIColorgrayColor];

    

    textField.borderStyle UITextBorderStyleNone;

    

    textField.placeholder = placeholder;

    

    return textField;

}


#pragma mark  imageView

+(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color{

    UIImageView *imageView=[[UIImageViewalloc]initWithFrame:frame];

    

    if (imageName){

        imageView.image=[UIImageimageNamed:imageName];

    }

    if (color){

        imageView.backgroundColor=color;

    }

    

    return imageView;

}



#pragma mark  button

+(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action{


    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame = frame;

    if (imageName){

        [btn setBackgroundImage:[UIImageimageNamed:imageName] forState:UIControlStateNormal];

    }

    

    if (font){

        btn.titleLabel.font = font;

    }

    

    if (title){

        [btn setTitle:titleforState:UIControlStateNormal];

    }


    if (color){

        [btn setTitleColor:colorforState:UIControlStateNormal];

    }


    if (target&&action){

        [btn addTarget:targetaction:action forControlEvents:UIControlEventTouchUpInside];

    }

    return btn;

}


0 0
原创粉丝点击