Some factory methods for creating and initializing the iOS View

Source: Internet
Author: User

Header file

// Provides some factory methods for UI controls to implement initialization of some general controls # import
 
  
@ Interface UIView (uifacloud) // 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
         
        
       
      
     
    
   
  
 

Implementation File

#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
        
       
      
     
    
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.