Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)

在iOS开发中,页面里有时会大量的用到一些控件,如果要一个个单独创建再设置样式的话就显得很麻烦。我们可以创建一个生成各种控件的工厂类,这样在需要的时候调用下就可以了。
下面以一个自定义的工厂类为例,其中提供了文本标签,按钮,文本输入框,分段单选控件的生成,效果图如下:
  
原文:Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)
工厂类:ViewFactory.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import  UIKit
 
class  ViewFactory
{
     /**
      * 控件默认尺寸
      */
     class  func  getDefaultFrame() ->  CGRect
     {
         let  defaultFrame =  CGRectMake (0, 0, 100, 30)
         return  defaultFrame
     }
     
     class  func  createControl(type: String , title:[ String ], action: Selector , sender: AnyObject )
         ->  UIView  {
         switch (type)
         {
         case  "label" :
             return  ViewFactory .createLabel(title[0])
         case  "button" :
             return  ViewFactory .createButton(title[0], action: action,
                 sender: sender  as UIViewController )
         case  "text" :
             return  ViewFactory .createTextField(title[0], action: action,
                 sender: sender  as UITextFieldDelegate )
         case  "segment" :
             return  ViewFactory .createSegment(title, action: action, sender:
                 sender  as UIViewController )
         default :
             return  ViewFactory .createLabel(title[0])
         }
     }
     
     /**
      * 创建按钮控件
      */
     class  func  createButton(title: String , action: Selector , sender: UIViewController )
         ->  UIButton  {
         let  button =  UIButton (frame: ViewFactory .getDefaultFrame())
         button.backgroundColor =  UIColor .orangeColor()
         button.setTitle(title, forState:. Normal )
         button.titleLabel!.textColor =  UIColor .whiteColor()
         button.titleLabel!.font =  UIFont .systemFontOfSize(14)
         button.addTarget(sender, action:action, forControlEvents:. TouchUpInside )
         return  button
     }
     
     /**
      * 创建文本输入框控件
      */
     class  func  createTextField(value: String , action: Selector , sender: UITextFieldDelegate )
         ->  UITextField
     {
         let  textField =  UITextField (frame: ViewFactory .getDefaultFrame())
         textField.backgroundColor =  UIColor .clearColor()
         textField.textColor =  UIColor .blackColor()
         textField.text = value
         textField.borderStyle =  UITextBorderStyle . RoundedRect
         textField.adjustsFontSizeToFitWidth =  true
         textField.delegate = sender
         return  textField
     }
     
     /**
      * 创建分段单选控件
      */
     class  func  createSegment(items: [ String ], action: Selector , sender: UIViewController )
         -> UISegmentedControl
     {
         let  segment =  UISegmentedControl (items:items)
         segment.frame =  ViewFactory .getDefaultFrame()
         //segment.segmentedControlStyle = UISegmentedControlStyle.Bordered
         segment.momentary =  false
         segment.addTarget(sender, action:action, forControlEvents:. ValueChanged )
         return  segment
     }
     
     /**
      * 创建文本标签控件
      */
     class  func  createLabel(title: String ) ->  UILabel
     {
         let  label =  UILabel ()
         label.textColor =  UIColor .blackColor();
         label.backgroundColor =  UIColor .whiteColor();
         label.text = title;
         label.frame =  ViewFactory .getDefaultFrame()
         label.font =   UIFont (name:  "HelveticaNeue-Bold" , size: 16)
         return  label
     }
}

工厂类的使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import  UIKit
 
class  ViewController UIViewController , UITextFieldDelegate  {
     
     var  txtNum: UITextField !
     var  segDimension: UISegmentedControl !
     var  btn: UIButton !
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         setupControls()
     }
     
     func  setupControls()
     {
         //创建文本标签
         let  labelNum =  ViewFactory .createLabel( "阈值:" )
         labelNum.frame =  CGRect (x: 20, y: 100, width: 60, height: 30)
         self .view.addSubview(labelNum)
         
         let  labelDm =  ViewFactory .createLabel( "维度:" )
         labelDm.frame =  CGRect (x: 20, y: 200, width: 60, height: 30)
         self .view.addSubview(labelDm)
         
         //创建文本输入框
         txtNum =  ViewFactory .createTextField( "" , action:  nil , sender: self )
         txtNum.frame =  CGRect (x:80,y:100,width:200,height:30)
         txtNum.returnKeyType =  UIReturnKeyType . Done
         self .view.addSubview(txtNum)
         
         //创建分段单选控件
         segDimension =  ViewFactory .createSegment([ "3x3" "4x4" "5x5" ],
             action: "dimensionChanged:" , sender: self )
         segDimension.frame =  CGRect (x:80,y: 200,width: 200,height: 30)
         self .view.addSubview(segDimension)
         segDimension.selectedSegmentIndex = 1
         
         //创建按钮控件
         btn =  ViewFactory .createButton( "确定" , action:  nil , sender:  self )
         btn.frame.origin =  CGPointMake (80, 300)
         self .view.addSubview(btn)
     }
     
     func  textFieldShouldReturn(textField:  UITextField ) ->  Bool  {
         //收起键盘
         txtNum.resignFirstResponder()
         //打印出文本框中的值
         print (txtNum.text)
         return  true
     }
     
     func  dimensionChanged(sender: AnyObject ) {
         print ( "dimensionChanged" )
     }
     
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}

原文出自: www.hangge.com   转载请保留原文链接: http://www.hangge.com/blog/cache/detail_655.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值