Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)
在iOS开发中,页面里有时会大量的用到一些控件,如果要一个个单独创建再设置样式的话就显得很麻烦。我们可以创建一个生成各种控件的工厂类,这样在需要的时候调用下就可以了。

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 { var 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: UIControlEvents . TouchUpInside ) return button } /** * 创建文本输入框控件 */ class func createTextField(value: String , action: Selector , sender: UITextFieldDelegate ) -> UITextField { var 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 { var segment = UISegmentedControl (items:items) segment.frame = ViewFactory .getDefaultFrame() //segment.segmentedControlStyle = UISegmentedControlStyle.Bordered segment.momentary = false segment.addTarget(sender, action:action, forControlEvents: UIControlEvents . 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
|
import UIKit class ViewController : UIViewController , UITextFieldDelegate { var txtNum: UITextField ! var segDimension: UISegmentedControl ! var btn: UIButton ! override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. 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: Selector ( "numChanged" ), 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) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)的更多相关文章
- 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画
[源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...
- 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板
[源码下载] 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板 作者:webabcd 介绍背水一战 Windows 10 之 控件( ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
[源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox
[源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...
- 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox
[源码下载] 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox 作者:webabcd ...
- 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox
[源码下载] 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) T ...
- 背水一战 Windows 10 (27) - 控件(文本类): TextBlock
[源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...
随机推荐
- VC++界面编程之--使用分层窗口实现界面皮肤
使用分层界面来实现界面皮肤的好处是:可以保证图片边缘处理不失真,且能用于异形窗口上,如一些不规则的窗口,你很难用SetWindowRgn来达到理想效果. 在很多情况下,界面的漂亮与否,取决于PS的制作 ...
- JavaScript和JQuery获取DIV的值
1.设计源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 知识点1-1:什么是ASP.NET MVC
ASP.NET MVC是微软.NET平台上的一个Web开发框架,它为开发者提供了一种构建结构良好的Web应用程序的方式.自2007年首次公布预览以来,作为Web Form的替代品,ASP.NET MV ...
- 道格拉斯—普克(Douglas一Peukcer)节点抽稀算法
Douglas一Peukcer算法由D.Douglas和T.Peueker于1973年提出,简称D一P算法,是眼下公认的线状要素化简经典算法.现有的线化简算法中,有相当一部分都是在该算法基础上进行改进 ...
- struts2之高危远程代码执行漏洞,可造成服务器被入侵,下载最新版本进行修复
Struts2 被发现存在新的高危远程代码执行漏洞,可造成服务器被入侵,只要是Struts2版本 低于 2.3.14.3 全部存在此漏洞.目前官方已经发布了最新的版本进行修复.请将stru ...
- 有关android工程的构建脚本(build.xml)的学习
学习[android-sdk-linux根目录]/tools/ant/build.xml,觉得如下几点很有用,记录之 1)ant脚本中属性值是于前置定义优化的原则,即属性发生重复定义时,前面定义的值不 ...
- django-cookieless 0.7 : Python Package Index
django-cookieless 0.7 : Python Package Index django-cookieless 0.7 Download django-cookieless-0.7.ta ...
- linux下crontab的使用方法
<span style="font-size:14px;">在Linux中任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行. cront ...
- c++实现atoi()和itoa()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
- Android漫游记(4)---.so文件动态调试一例
Android平台的动态调试一直以来是个困扰我等Coder的头疼问题,特别是对于本地的动态调试支持.能够说是"弱智"级别的,不知道Google的新版NDK和新出的Android S ...