iOS视图创建初始化的一些工厂方法

头文件

// 提供一些UI控件的工厂方法,实现一些通用的控件初始化工作

#import <UIKit/UIKit.h>

@interface UIView (UIFactory)

// Label
+ (id)createLabel;
+ (id)createLabel:(CGRect)frame;

// TextField
+ (id)createTextFiled;
+ (id)createTextFiled:(UITextBorderStyle)style;
+ (id)createTextFiled:(CGRect)frame style:(UITextBorderStyle)style;

// Button
+ (id)createButton:(CGRect)frame;

+ (id)createButton:(CGRect)frame
              type:(UIButtonType)type;

+ (id)createButton:(CGRect)frame
            target:(id)target
            action:(SEL)action;

+ (id)createButton:(CGRect)frame
            target:(id)target
            action:(SEL)action
        buttonType:(UIButtonType)type;


// TableView
+ (id)createTableView:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate;

+ (id)createTableView:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
                style:(UITableViewStyle)style;

+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate;

+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
                style:(UITableViewStyle)style;


// TextView
    

@end

实现文件

#import "UIView+UIFactory.h"

#ifndef Demo_Macros_h
#define Demo_Macros_h
    #ifdef __IPHONE_6_0
        #define kTextAlignmentLeft NSTextAlignmentLeft
        #define kTextAlignmentCenter NSTextAlignmentCenter
        #define kTextAlignmentRight NSTextAlignmentRight
        #define kLineBreakModeCharaterWrap NSLineBreakByCharWrapping
        #define kLineBreakModeWordWrap NSLineBreakByWordWrapping
        #define kLineBreakModeClip NSLineBreakByClipping
        #define kLineBreakModeTruncatingHead NSLineBreakByTruncatingHead
        #define kLineBreakModeTruncatingMiddle NSLineBreakByTruncatingMiddle
        #define kLineBreakModeTruncatingTail NSLineBreakByTruncatingTail
    #else
        #define kTextAlignmentLeft UITextAlignmentLeft
        #define kTextAlignmentCenter UITextAlignmentCenter
        #define kTextAlignmentRight UITextAlignmentRight
        #define kLineBreakModeCharaterWrap UILineBreakModeCharacterWrap
        #define kLineBreakModeWordWrap UILineBreakModeWordWrap
        #define kLineBreakModeClip UILineBreakModeClip
        #define kLineBreakModeTruncatingHead UILineBreakModeHeadTruncation
        #define kLineBreakModeTruncatingMiddle UILineBreakModeMiddleTruncation
        #define kLineBreakModeTruncatingTail UILineBreakModeTailTruncation
    #endif

    #define kMainScreenFrame [[UIScreen mainScreen] bounds]
    #define kMainScreenWidth kMainScreenFrame.size.width
    #define kMainScreenHeight kMainScreenFrame.size.height-20
    #define kApplicationFrame [[UIScreen mainScreen] applicationFrame]
#endif





@implementation UIView (UIFactory)

#pragma mark Label

+ (id)createLabel
{
    return [UIView createLabel:CGRectZero];
}

+ (id)createLabel:(CGRect)frame
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = kTextAlignmentCenter;

#if __has_feature(objc_arc)
    return label;
#else
    return [label autorelease];
#endif
    
}

#pragma mark TextField

+ (id)createTextFiled
{
    return [UIView createTextFiled:UITextBorderStyleRoundedRect];
}

+ (id)createTextFiled:(UITextBorderStyle)style
{
    return [UIView createTextFiled:CGRectZero style:style];
}

+ (id)createTextFiled:(CGRect)frame style:(UITextBorderStyle)style
{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.textAlignment = kTextAlignmentCenter;
    textField.textColor = [UIColor blackColor];
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.borderStyle = style;
    
#if __has_feature(objc_arc)
    return textField;
#else
    return [textField autorelease];
#endif
    
}


#pragma mark Button

+ (id)createButton:(CGRect)frame
{
    return [UIView createButton:frame type:UIButtonTypeRoundedRect];
}

+ (id)createButton:(CGRect)frame
              type:(UIButtonType)type
{
    UIButton *btn = [UIButton buttonWithType:type];
    btn.frame = frame;
    return btn;
}

+ (id)createButton:(CGRect)frame
            target:(id)target
            action:(SEL)action
{
    return [UIView createButton:frame
                         target:target
                         action:action
                     buttonType:UIButtonTypeRoundedRect];
}


+ (id)createButton:(CGRect)frame
            target:(id)target
            action:(SEL)action
        buttonType:(UIButtonType)type
{
    UIButton *btn = [UIButton buttonWithType:type];
    btn.frame = frame;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}


#pragma mark TableView

+ (id)createTableView:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
{
    return [UIView createTableView:CGRectZero
                        dataSource:dataSource
                          delegete:delegate
                             style:UITableViewStyleGrouped];
}

+ (id)createTableView:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
                style:(UITableViewStyle)style
{
    return [UIView createTableView:CGRectZero
                        dataSource:dataSource
                          delegete:delegate
                             style:style];
}

+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
{
    return [UIView createTableView:frame
                        dataSource:dataSource
                          delegete:delegate
                             style:UITableViewStyleGrouped];
}


+ (id)createTableView:(CGRect)frame
           dataSource:(id<UITableViewDataSource>)dataSource
             delegete:(id<UITableViewDelegate>)delegate
                style:(UITableViewStyle)style
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:style];
    tableView.delegate = delegate;
    tableView.dataSource = dataSource;
    
    
#if __has_feature(objc_arc)
    return tableView;
#else
    return [tableView autorelease];
#endif
    
}


#pragma mark TextView

+ (id)createTextView:(CGRect)frame
{
    UITextView *tv = [[UITextView alloc] initWithFrame:frame];
    
#if __has_feature(objc_arc)
    return tv;
#else
    return [tv autorelease];
#endif

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值