iOS視圖建立初始化的一些Factory 方法

來源:互聯網
上載者:User

標頭檔

// 提供一些UI控制項的Factory 方法,實現一些通用的控制項初始化工作#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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.