前言
在app的开发中,往往需要创建大量的控件,就算创建一个最基本的控件也得写上五六行代码。为了降低开发人员的机械劳动和提高代码的质量,其实我们完全可以把常用的一些控件,自己进行封装,大大简化创建控件的代码。下面举几个最常用的控件的封装。
一. h文件中进行声明
//
// UIViewSpeedCreat.h
// UIViewSpeedCreat
//
// Created by humiaor on 2019/3/1.
// Copyright © 2019年 humaior. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UILabel (SCExtension)
//创建UILabel
+ (UILabel *)sCreateWithFrame:(CGRect)frame
text:(NSString * __nullable)text
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
textAlignment:(NSTextAlignment)textAlignment
addToView:(UIView * __nullable)view;
@end
typedef void (^ButtonClickedBlock)(UIButton *button);
@interface UIButton (SCExtension)
//创建UIButton
+ (UIButton *)sCreateWithFrame:(CGRect)frame
title:(NSString * __nullable)title
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
backgroundImage:(UIImage * __nullable)backgroundImage
backgroundColor:(UIColor * __nullable)backgroundColor
cornerRadius:(CGFloat)cornerRadius
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor * __nullable)borderClor
target:(id)target
action:(SEL)action
addToView:(UIView * __nullable)view;
//创建UIButton+点击block回调
+ (UIButton *)sCreateWithFrame:(CGRect)frame
title:(NSString * __nullable)title
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
backgroundImage:(UIImage * __nullable)backgroundImage
backgroundColor:(UIColor * __nullable)backgroundColor
cornerRadius:(CGFloat)cornerRadius
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor * __nullable)borderClor
clickBlock:(ButtonClickedBlock)clickBlock
addToView:(UIView * __nullable)view;
/**
UIButton 附加 Block 点击回调
@param event 点击状态
@param action 回调方法
*/
- (void)sc_handleControlEvent:(UIControlEvents)event withBlock:(ButtonClickedBlock)action;
@end
@interface UITextField (SCExtension)
//创建UITextField
+ (UITextField *)sCreateWithFrame:(CGRect)frame
placeholder:(NSString * __nullable)placeholder
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
secureTextEntry:(BOOL)secureTextEntry
delegate:(id __nullable)delegate
addToView:(UIView * __nullable)view;
@end
@interface UITextView (SCExtension)
//创建UITextView
+ (UITextView *)sCreateWithFrame:(CGRect)frame
text:(NSString * __nullable)text
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
textAlignment:(NSTextAlignment)textAlignment
addToView:(UIView * __nullable)view;
@end
@interface UIImageView (SCExtension)
//创建图片
+ (UIImageView*) sCreateWithFrame:(CGRect)frame
image:(UIImage * __nullable)image
addToView:(UIView * __nullable)view;
//创建背景图片
+ (UIImage*) imageWithColor:(UIColor*)color;
@end
NS_ASSUME_NONNULL_END
二. m文件中实现
//
// UIViewSpeedCreat.m
// UIViewSpeedCreat
//
// Created by humiaor on 2019/3/1.
// Copyright © 2019年 humaior. All rights reserved.
//
#import "UIViewSpeedCreate.h"
#import <objc/runtime.h>
@implementation UILabel (SCExtension)
//创建UILabel
+ (UILabel *)sCreateWithFrame:(CGRect)frame
text:(NSString * __nullable)text
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
textAlignment:(NSTextAlignment)textAlignment
addToView:(UIView * __nullable)view
{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.textColor = color;
label.font = font;
label.textAlignment = textAlignment;
label.backgroundColor = [UIColor clearColor];
return label;
}
@end
@implementation UIButton (SCExtension)
//创建UIButton
+ (UIButton *)sCreateWithFrame:(CGRect)frame
title:(NSString * __nullable)title
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
backgroundImage:(UIImage * __nullable)backgroundImage
backgroundColor:(UIColor * __nullable)backgroundColor
cornerRadius:(CGFloat)cornerRadius
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor * __nullable)borderClor
target:(id)target
action:(SEL)action
addToView:(UIView * __nullable)view
{
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.backgroundColor = backgroundColor;
btn.layer.cornerRadius = cornerRadius;
btn.layer.borderWidth = borderWidth;
btn.layer.borderColor = borderClor.CGColor;
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return btn;
}
//创建UIButton+点击block回调
+ (UIButton *)sCreateWithFrame:(CGRect)frame
title:(NSString * __nullable)title
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
backgroundImage:(UIImage * __nullable)backgroundImage
backgroundColor:(UIColor * __nullable)backgroundColor
cornerRadius:(CGFloat)cornerRadius
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor * __nullable)borderClor
clickBlock:(ButtonClickedBlock)clickBlock
addToView:(UIView * __nullable)view{
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.backgroundColor = backgroundColor;
btn.layer.cornerRadius = cornerRadius;
btn.layer.borderWidth = borderWidth;
btn.layer.borderColor = borderClor.CGColor;
[btn sc_handleControlEvent:UIControlEventTouchUpInside withBlock:^(UIButton * _Nonnull button) {
if (clickBlock) {
clickBlock(button);
}
}];
if (view && ![view.subviews containsObject:btn]) {
[view addSubview:btn];
}
return btn;
}
static char eventKey;
- (void)sc_handleControlEvent:(UIControlEvents)event withBlock:(ButtonClickedBlock)action {
objc_setAssociatedObject(self, &eventKey, action, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(sc_callActionBlock:) forControlEvents:event];
}
- (void)sc_callActionBlock:(UIButton *)sender {
ButtonClickedBlock block = (ButtonClickedBlock)objc_getAssociatedObject(self, &eventKey);
if (block) {
block(sender);
}
}
@end
@implementation UITextField (SCExtension)
//创建UITextField
+ (UITextField *)sCreateWithFrame:(CGRect)frame
placeholder:(NSString * __nullable)placeholder
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
secureTextEntry:(BOOL)secureTextEntry
delegate:(id __nullable)delegate
addToView:(UIView * __nullable)view
{
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.placeholder = placeholder;
textField.textColor = color;
textField.font = font;
textField.secureTextEntry = secureTextEntry;
textField.delegate = delegate;
return textField;
}
@end
@implementation UITextView (SCExtension)
//创建UITextView
+ (UITextView *)sCreateWithFrame:(CGRect)frame
text:(NSString * __nullable)text
color:(UIColor * __nullable)color
font:(UIFont * __nullable)font
textAlignment:(NSTextAlignment)textAlignment
addToView:(UIView * __nullable)view
{
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;
}
@end
@implementation UIImageView (SCExtension)
//创建图片
+ (UIImageView*) sCreateWithFrame:(CGRect)frame
image:(UIImage * __nullable)image
addToView:(UIView * __nullable)view
{
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
三.调用
//
// TestViewController.m
// UIViewSpeedCreat
//
// Created by humiaor on 2019/3/1.
// Copyright © 2019年 humaior. All rights reserved.
//
#import "TestViewController.h"
#import "UIViewSpeedCreate.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UILabel
[UILabel sCreateWithFrame:CGRectMake(50, 20, 80, 40) text:@"Label" color:[UIColor whiteColor] font:[UIFont systemFontOfSize:14] textAlignment:NSTextAlignmentCenter backgroundColor:[UIColor greenColor] addToView:self.view];
//UIButton
[UIButton sCreateWithFrame:CGRectMake(50, 80, 80, 40) title:@"UIButton" color:[UIColor whiteColor] font:[UIFont systemFontOfSize:14] backgroundImage:nil backgroundColor:[UIColor blueColor] cornerRadius:5 borderWidth:0 borderColor:nil clickBlock:^(UIButton * _Nonnull button) {
NSLog(@"%@",button);
} addToView:self.view];
//UITextField
[UITextField sCreateWithFrame:CGRectMake(50, 140, 80, 40) placeholder:@"UITextField" color:[UIColor blackColor] font:[UIFont systemFontOfSize:14] secureTextEntry:NO delegate:nil addToView:self.view];
//UITextView
[UITextView sCreateWithFrame:CGRectMake(50, 200, 80, 40) text:@"UITextView" color:[UIColor yellowColor] font:[UIFont systemFontOfSize:14] textAlignment:NSTextAlignmentCenter addToView:self.view];
//UIImageView
[UIImageView sCreateWithFrame:CGRectMake(50, 260, 80, 40) image:[UIImage imageNamed:@"image"] addToView:self.view];
}
@end