下面是在程序生成5個標籤,文本框,按鈕的語句web
for var i=0;i<5;i++ {app
var y:CGFloat = CGFloat(i*100 + 50)ide
var SwiftLable=UILabel()spa
SwiftLable.frame = CGRectMake (10 , y, 60, 40)code
SwiftLable.text = "標題"orm
SwiftLable.backgroundColor = .redColor()圖片
self.view.addSubview(SwiftLable)ci
var SwiftTextField=UITextField()字符串
SwiftTextField.frame=CGRectMake(80, y , 180, 40)get
SwiftTextField.borderStyle = UITextBorderStyle.RoundedRect
SwiftTextField.placeholder="請輸入字符串"
array.append(SwiftTextField)
self.view.addSubview(SwiftTextField)
var SwiftButton=UIButton()
SwiftButton.frame=CGRectMake(300, y , 60, 40)
SwiftButton.backgroundColor = .greenColor()
SwiftButton.tag=i
SwiftButton.setTitle("change" , forState: UIControlState.Normal)
SwiftButton.addTarget(self, action: "tickMethod:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(SwiftButton)
}
因爲文本框除了設置顏色外還有其餘方法顯示,搜索資料以下:
1,文本框的建立,有以下幾個樣式:
UITextBorderStyle.None:無邊框
UITextBorderStyle.Line:直線邊框
UITextBorderStyle.RoundedRect:圓角矩形邊框
UITextBorderStyle.Bezel:邊線+陰影
1
2
3
4
|
var
textField =
UITextField
(frame:
CGRectMake
(10,160,200,30))
//設置邊框樣式爲圓角矩形
textField.borderStyle =
UITextBorderStyle
.
RoundedRect
self
.view.addSubview(textField)
|
2,文本框提示文字
1
|
textField.placeholder=
"請輸入用戶名"
|
3,文字大小超過文本框長度時自動縮小字號,而不是隱藏顯示省略號
1
2
|
textField.adjustsFontSizeToFitWidth=
true
//當文字超出文本框寬度時,自動調整文字大小
textField.minimumFontSize=14
//最小可縮小的字號
|
4,水平/垂直對齊方式
1
2
3
4
5
6
7
8
9
|
/** 水平對齊 **/
textField.textAlignment = .
Right
//水平右對齊
textField.textAlignment = .
Center
//水平居中對齊
textField.textAlignment = .
Left
//水平左對齊
/** 垂直對齊 **/
textField.contentVerticalAlignment = .
Top
//垂直向上對齊
textField.contentVerticalAlignment = .
Center
//垂直居中對齊
textField.contentVerticalAlignment = .
Bottom
//垂直向下對齊
|
5,背景圖片設置
1
|
textField.background=
UIImage
(named:
"background1"
);
|