iOS 快速创建常用控件

来源:互联网 发布:玻尔兹曼常数测量 知乎 编辑:程序博客网 时间:2024/06/15 19:24

前言

在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];}

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

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

0 0
原创粉丝点击