你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS視圖創建初始化的一些工廠方法

iOS視圖創建初始化的一些工廠方法

編輯:IOS開發綜合

頭文件

// 提供一些UI控件的工廠方法,實現一些通用的控件初始化工作

#import 

@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)dataSource
             delegete:(id)delegate;

+ (id)createTableView:(id)dataSource
             delegete:(id)delegate
                style:(UITableViewStyle)style;

+ (id)createTableView:(CGRect)frame
           dataSource:(id)dataSource
             delegete:(id)delegate;

+ (id)createTableView:(CGRect)frame
           dataSource:(id)dataSource
             delegete:(id)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)dataSource
             delegete:(id)delegate
{
    return [UIView createTableView:CGRectZero
                        dataSource:dataSource
                          delegete:delegate
                             style:UITableViewStyleGrouped];
}

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

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


+ (id)createTableView:(CGRect)frame
           dataSource:(id)dataSource
             delegete:(id)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


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved