iOS 快速创建常用控件

前言

在app的开发中,往往需要创建大量的控件,就算创建一个最基本的控件也得写上五六行代码。为了降低开发人员的机械劳动和提高代码的质量,其实我们完全可以把常用的一些控件,自己进行封装,大大简化创建控件的代码。下面举几个最常用的控件的封装。

一. h文件中进行声明

注意:这个控件辅助类继承于NSObject,头文件要引入#import

//
//  YCCommonCtrl.h
//  UICommonCtrol
//
//  Created by 袁灿 on 15/9/22.
//  Copyright (c) 2015年 yuancan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface YCCommonCtrl : NSObject

//创建UILabel
+ (UILabel *)commonLableWithFrame:(CGRect)frame
                             text:(NSString *)text
                            color:(UIColor *)color
                             font:(UIFont *)font
                    textAlignment:(NSTextAlignment)textAlignment;

//创建UITextField
+ (UITextField *)commonTextFieldWithFrame:(CGRect)frame
                              placeholder:(NSString *)placeholder
                                    color:(UIColor *)color
                                     font:(UIFont *)font
                          secureTextEntry:(BOOL)secureTextEntry
                                 delegate:(id)delegate;

//创建UITextView
+ (UITextView *)commonTextViewWithFrame:(CGRect)frame
                                   text:(NSString *)text
                                  color:(UIColor *)color
                                   font:(UIFont *)font
                          textAlignment:(NSTextAlignment)textAlignment;

//创建UIButton
+ (UIButton *)commonButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                              color:(UIColor *)color
                               font:(UIFont *)font
                    backgroundImage:(UIImage *)backgroundImage
                             target:(id)target
                             action:(SEL)action;

//创建图片
+ (UIImageView*) commonImageViewWithFrame:(CGRect)frame
                                    image:(UIImage*)image;

//创建背景图片
+ (UIImage*) imageWithColor:(UIColor*)color;

@end

二. m文件中实现

#import "YCCommonCtrl.h"

@implementation YCCommonCtrl

//创建UILabel
+ (UILabel *)commonLableWithFrame:(CGRect)frame
                             text:(NSString *)text
                            color:(UIColor *)color
                             font:(UIFont *)font
                    textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.font = font;
    label.textAlignment = textAlignment;
    label.backgroundColor = [UIColor clearColor];

    return label;
}

//创建UITextField
+ (UITextField *)commonTextFieldWithFrame:(CGRect)frame
                              placeholder:(NSString *)placeholder
                                    color:(UIColor *)color
                                     font:(UIFont *)font
                          secureTextEntry:(BOOL)secureTextEntry
                                 delegate:(id)delegate
{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.placeholder = placeholder;
    textField.textColor = color;
    textField.font = font;
    textField.secureTextEntry = secureTextEntry;
    textField.delegate = delegate;

    return textField;
}

//创建UITextView
+ (UITextView *)commonTextViewWithFrame:(CGRect)frame
                                   text:(NSString *)text
                                  color:(UIColor *)color
                                   font:(UIFont *)font
                          textAlignment:(NSTextAlignment)textAlignment
{
    UITextView *textView = [[UITextView alloc] initWithFrame:frame];
    textView.text = text;
    textView.textColor = color;
    textView.textAlignment = textAlignment;

    textView.backgroundColor = [UIColor clearColor];
    textView.editable = NO;
    textView.scrollEnabled = NO;
    textView.dataDetectorTypes = UIDataDetectorTypeLink;

    return textView;
}

//创建UIButton
+ (UIButton *)commonButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                              color:(UIColor *)color
                               font:(UIFont *)font
                    backgroundImage:(UIImage *)backgroundImage
                             target:(id)target
                             action:(SEL)action
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];

    [btn setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return btn;
}

//创建图片
+ (UIImageView*) commonImageViewWithFrame:(CGRect)frame
                                    image:(UIImage*)image
{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.image = image;

    return imgView;
}

//创建背景图片
+ (UIImage*) imageWithColor:(UIColor*)color
{
    CGSize imageSize = CGSizeMake(1, 1);
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [color set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end

三. 调用

#import "ViewController.h"
#import "YCCommonCtrl.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [YCCommonCtrl commonLableWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 20)
                                                   text:@"测试"
                                                  color:[UIColor blackColor]
                                                   font:[UIFont systemFontOfSize:17.0f]
                                          textAlignment:NSTextAlignmentLeft];

    UITextField *textField = [YCCommonCtrl commonTextFieldWithFrame:CGRectMake(10, 150, self.view.frame.size.width-20, 35)
                                                        placeholder:@"测试"
                                                              color:[UIColor grayColor]
                                                               font:[UIFont systemFontOfSize:17.0f]
                                                    secureTextEntry:NO
                                                           delegate:self];


    UIButton *btn = [YCCommonCtrl commonButtonWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)
                                                  title:@"测试"
                                                  color:[UIColor orangeColor]
                                                   font:[UIFont systemFontOfSize:17.0f]
                                        backgroundImage:[YCCommonCtrl imageWithColor:[UIColor blueColor]]
                                                 target:self
                                                 action:NULL];
    [self.view addSubview:label];
    [self.view addSubview:textField];
    [self.view addSubview:btn];

}

其实,这些写不但精简了代码,而且排版上更加整齐美观,何乐不为呢!

扫二维码,关注作者最新的技术博客喔。
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值