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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • 这是我个人第一个博客,个人自学iOS开发一两个月,很早开始就想整理一些学习笔记,一方面以便以后自己查看,也是...
    JahoJiang阅读 330评论 4 1
  • 午后阳光透过窗户洒在墙面上,不知道是外面的什么物体影响,在墙面上形成了一会特别的图案,我觉得是一只狗狗^_^ 手机...
    云卷云舒ya阅读 195评论 1 1
  • 美日将在中国东海进行联合军演,中国海军副参谋长强硬表态 近日日本发布快讯;以美国海军“卡尔文森”号为核心的航母群,...
    再见无效阅读 289评论 0 0
  • 很多次晚上睡觉前,我都突然口渴想喝水,用胳膊碰碰他,他就知道拿水给我喝,昨天晚上又这么碰他,他去拿水,然后他自己咕...
    洛央_eb55阅读 254评论 0 1