iOS 快速创建控件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface QuickBuild : NSObject


/** 创建UILabel */

+ (UILabel *)quickBuildLableWithFrame:(CGRect)frame

                             text:(NSString *)text

                            color:(UIColor *)color

                             font:(UIFont *)font

                    textAlignment:(NSTextAlignment)textAlignment;



/** 创建UITextField */

+ (UITextField *)quickBuildTextFieldWithFrame:(CGRect)frame

                              placeholder:(NSString *)placeholder

                                    color:(UIColor *)color

                                     font:(UIFont *)font

                          secureTextEntry:(BOOL)secureTextEntry

                                 delegate:(id)delegate;

/** 创建UITextView */

+ (UITextView *)quickBuildTextViewWithFrame:(CGRect)frame

                                   text:(NSString *)text

                                  color:(UIColor *)color

                                   font:(UIFont *)font

                          textAlignment:(NSTextAlignment)textAlignment;


/** 创建UIButton */

+ (UIButton *)quickBuildButtonWithFrame:(CGRect)frame

                              title:(NSString *)title

                              color:(UIColor *)color

                               font:(UIFont *)font

                    backgroundImage:(UIImage *)backgroundImage

                             target:(id)target

                             action:(SEL)action;

/** 创建图片控件 */

+ (UIImageView*) quickBuildImageViewWithFrame:(CGRect)frame

                                    image:(UIImage*)image;


/** 创建带颜色图片 */

+ (UIImage*) quickBuildimageWithColor:(UIColor*)color;

@end



@implementation QuickBuild


+ (UILabel *)quickBuildLableWithFrame:(CGRect)frame text:(NSString *)text color:(UIColor *)color font:(UIFont *)font  textAlignment:(NSTextAlignment)textAlignment {

    

    UILabel *label = [[UILabelalloc] initWithFrame:frame];

    label.text = text;

    label.textColor = color;

    label.font = font;

    label.textAlignment = textAlignment;

    label.backgroundColor = [UIColorclearColor];

    

    return label;

}



+ (UITextField *)quickBuildTextFieldWithFrame:(CGRect)frame  placeholder:(NSString *)placeholder color:(UIColor *)color font:(UIFont *)font  secureTextEntry:(BOOL)secureTextEntry  delegate:(id)delegate {

    

    UITextField *textField = [[UITextFieldalloc] initWithFrame:frame];

    textField.placeholder = placeholder;

    textField.textColor = color;

    textField.font = font;

    textField.secureTextEntry = secureTextEntry;

    textField.delegate = delegate;

    

    return textField;

}


+ (UITextView *)quickBuildTextViewWithFrame:(CGRect)frame  text:(NSString *)text   color:(UIColor *)color  font:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment {

    

    UITextView *textView = [[UITextViewalloc] initWithFrame:frame];

    textView.text = text;

    textView.textColor = color;

    textView.textAlignment = textAlignment;

    

    textView.backgroundColor = [UIColorclearColor];

    textView.editable = NO;

    textView.scrollEnabled = NO;

    textView.dataDetectorTypes =UIDataDetectorTypeLink;

    

    return textView;

}



+ (UIButton *)quickBuildButtonWithFrame:(CGRect)frame  title:(NSString *)title  color:(UIColor *)color font:(UIFont *)font  backgroundImage:(UIImage *)backgroundImage  target:(id)target  action:(SEL)action {

    

    UIButton *btn = [[UIButtonalloc] initWithFrame:frame];

    [btn setTitle:titleforState:UIControlStateNormal];

    [btn setTitleColor:colorforState:UIControlStateNormal];

    [btn.titleLabelsetFont:font];

    

    [btn setBackgroundImage:backgroundImageforState:UIControlStateNormal];

    [btn addTarget:targetaction:action forControlEvents:UIControlEventTouchUpInside];

    

    return btn;

}



+ (UIImageView*) quickBuildImageViewWithFrame:(CGRect)frame image:(UIImage*)image {

    

    UIImageView *imgView = [[UIImageViewalloc] initWithFrame:frame];

    imgView.contentMode =UIViewContentModeScaleAspectFill;

    imgView.image = image;

    

    return imgView;

}



+ (UIImage*) quickBuildimageWithColor:(UIColor*)color {

    

    CGSize imageSize = CGSizeMake(1, 1);

    UIGraphicsBeginImageContextWithOptions(imageSize,0, [UIScreenmainScreen].scale);

    [color set];

    UIRectFill(CGRectMake(0,0, imageSize.width, imageSize.height));

    UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}


@end

    // 使用方法

    UILabel *label = [QuickBuild quickBuildLableWithFrame:CGRectMake(0, 60, 200, 100) text:@"123dsfsfsfdsfsfsfsfsfsfdsfsdfsd" color:[UIColor whiteColor] font:[UIFont systemFontOfSize:30] textAlignment:(NSTextAlignmentCenter)];

    [self.view addSubview:label];






猜你喜欢

转载自blog.csdn.net/saw471/article/details/77944012