工厂类: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
@H_403_116@
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,100,30)
return
defaultFrame
}
createControl(type:
String
,title:[
],action:
Selector
AnyObject
) ->
UIView
{
switch
(type)
{
case
"label"
:
return
ViewFactory
.createLabel(title[0])
"button"
:
.createButton(title[0],action: action,
sender: sender
as
UIViewController
)
"text"
@H_403_333@.createTextField(title[0],
:
UITextFieldDelegate
)
"segment"
:
.createSegment(title,sender:
sender
)
default
:
.createLabel(title[0])
}
}
/**
* 创建按钮控件
*/
createButton(title:
)->
UIButton
{
var
button =
UIButton
(frame:
.getDefaultFrame())
button.backgroundColor =
UIColor
.orangeColor()
button.setTitle(title,forState:.
Normal
)
button.titleLabel!.textColor =
.whiteColor()
button.titleLabel!.font =
UIFont
.systemFontOfSize(14)
button.addTarget(sender,action:action,forControlEvents:
UIControlEvents
.
TouchUpInside
)
button
}
/**
* 创建文本输入框控件
*/
createTextField(value:
)
->
UITextField
{
textField =
UITextField
.getDefaultFrame())
textField.backgroundColor =
.clearColor()
textField.textColor =
.blackColor()
textField.text = value
textField.borderStyle =
UITextBorderStyle
RoundedRect
textField.adjustsFontSizeToFitWidth =
@H_405_502@textField.delegate = sender
true
textField
}
/**
* 创建分段单选控件
*/
createSegment(items: [
)
UISegmentedControl
{
segment =
UISegmentedControl
(items:items)
segment.frame =
.getDefaultFrame()
//segment.segmentedControlStyle = UISegmentedControlStyle.Bordered
segment.momentary =
false
segment.addTarget(sender,147)!important">ValueChanged
)
segment
}
/**
* 创建文本标签控件
*/
createLabel(title:
UILabel
{
label =
UILabel
()
label.textColor =
.blackColor();
label.backgroundColor =
.whiteColor();
label.text = title;
label.frame =
.getDefaultFrame()
label.font =
(name:
"HelveticaNeue-Bold"
label
}
}
|
工厂类的使用:
ViewController
:
{
txtNum:
!
segDimension:
!
btn:
!
override
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
setupControls()
}
setupControls()
{
//创建文本标签
labelNum =
.createLabel(
"阈值:"
)
labelNum.frame =
CGRect
(x: 20,y: 100,width: 60,height: 30)
self
.view.addSubview(labelNum)
labelDm =
"维度:"
labelDm.frame =
403_333@.view.addSubview(labelDm)
//创建文本输入框
txtNum =
.createTextField(
""
(
"numChanged"
),sender:
)
txtNum.frame =
(x:80,y:100,width:200,height:30)
txtNum.returnKeyType =
UIReturnKeyType
Done
.view.addSubview(txtNum)
//创建分段单选控件
segDimension =
.createSegment([
"3x3"
"4x4"
"5x5"
"dimensionChanged:"
sender:
)
segDimension.frame =
.view.addSubview(segDimension)
segDimension.selectedSegmentIndex = 1
//创建按钮控件
btn =
.createButton(
"确定"
nil
)
btn.frame.origin =
CGPointMake
(80,300)
.view.addSubview(btn)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
猜你在找的Swift相关文章 |