iOS 一行代码简化初始化button、label、imageView、textField等常用控件

新建对应控件的分类 定义以下方法 使用的时候 导入对应的分类头文件直接调用方法即可。

1、按钮(UIButton)初始化

//初始化button
+(UIButton *)buttonWithFrame:(CGRect)rect withTitle:(NSString *)title withTitleColor:(UIColor *)color withbackgroundColor:(UIColor *)backgroundColor withFont:(CGFloat)fontSize withImage:(NSString *)imageName withBackgroundImage:(NSString *)backgroundImageName withTarget:(id)target withSelector:(SEL)selector{
    //右侧按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = rect;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setBackgroundColor:backgroundColor];
    btn.titleLabel.font = FONT(fontSize);
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

调用方法

self.beginBtn = [UIButton buttonWithFrame:CGRectMake(100, 0, iPhoneWidth - 160, 40) withTitle:@"" withTitleColor:LightGrayColor withbackgroundColor:ClearColor withFont:15 withImage:@"" withBackgroundImage:@"" withTarget:self withSelector:@selector(timerBtnClick:)];

2、标签(UILabel)初始化

+(UILabel *)labelWithFrame:(CGRect)rect withTitle:(NSString *)title withTitleColor:(UIColor *)color withFont:(CGFloat)fontSize withTextAlignment:(ReturnType)textAlignment withBackgroundColor:(UIColor *)backgroundColor{
    UILabel *LB = [[UILabel alloc] initWithFrame:rect];
    LB.text = title;
    LB.font = FONT(fontSize);
    LB.textColor = color;
    LB.backgroundColor = backgroundColor;
    if (textAlignment == TextAlignmentLeft) {
        LB.textAlignment = NSTextAlignmentLeft;
    }else if(textAlignment == TextAlignmentCenter){
        LB.textAlignment = NSTextAlignmentCenter;
    }else if (textAlignment == TextAlignmentRight){
        LB.textAlignment = NSTextAlignmentRight;
    }
    return LB;
}

.h中定义的枚举

typedef NS_ENUM(NSInteger ,ReturnType){
    TextAlignmentLeft      = 0,
    TextAlignmentCenter    = 1,
    TextAlignmentRight     = 2
};

调用方法

UILabel *beginLB = [UILabel labelWithFrame:CGRectMake(20, 10, 80, 20) withTitle:@"开始时间:" withTitleColor:BlackColor withFont:15 withTextAlignment:TextAlignmentLeft withBackgroundColor:ClearColor];

3、图片(UIImageView)初始化

+(UIImageView *)imageViewWithFrame:(CGRect)rect withImage:(NSString *)imageName{
    UIImageView *IV = [[UIImageView alloc] initWithFrame:rect];
    IV.image = [UIImage imageNamed:imageName];
    return IV;
}

4、文本框(UIITextField)初始化

+(UITextField *)textFieldWithFrame:(CGRect)rect withTextColor:(UIColor *)textColor withPlaceholder:(NSString *)placeholder withPlaceholderColor:(UIColor *)placeholderColor withPlaceholderFont:(CGFloat)placeholderFont{
    //账号输入框
    UITextField *TF = [[UITextField alloc] initWithFrame:rect];
    TF.clearButtonMode = UITextFieldViewModeWhileEditing;
    TF.textColor = textColor;
    //更改占位符字体颜色和大小
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 设置富文本对象的颜色
    attributes[NSForegroundColorAttributeName] = placeholderColor;
    //设置占位符字体大小
    attributes[NSFontAttributeName] = FONT(placeholderFont);
    TF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:attributes];
    return TF;
}

你可能感兴趣的:(iOS 一行代码简化初始化button、label、imageView、textField等常用控件)