1.Textfiled的創(chuàng)建
let textField:UITextField = UITextField(frame: CGRect(x: 20, y: 100, width: 240, height: 40))
self.view.addSubview(textField)
2.Textfiled的基礎(chǔ)應(yīng)用
//設(shè)置輸入框風格為line
textField.borderStyle = UITextField.BorderStyle.line
//設(shè)置輸入框風格為bezel
textField.borderStyle = UITextField.BorderStyle.bezel
//設(shè)置輸入框的提示文字
textField.placeholder = "請輸入文字"
//設(shè)置輸入的文字顏色
textField.textColor = UIColor.blue
//設(shè)置文字的字體
textField.font = UIFont.italicSystemFont(ofSize: 20)
//設(shè)置文字的對齊方式
textField.textAlignment = NSTextAlignment.center
//textField.backgroundColor = UIColor.red
//是否每次進入編輯狀態(tài)時都清空輸入框中的文字
textField.clearsOnBeginEditing = true
//是否字體大小自適應(yīng)
textField.adjustsFontSizeToFitWidth = true
//設(shè)置輸入框為無效
//textField.isEnabled = false
//設(shè)置輸入框的左視圖
let viewLeft:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let viewRight: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
viewLeft.backgroundColor = UIColor.red
viewRight.backgroundColor = UIColor.blue
textField.leftView = viewLeft
textField.rightView = viewRight
//設(shè)置左右視圖的顯示模式
textField.leftViewMode = UITextField.ViewMode.always
textField.rightViewMode = UITextField.ViewMode.always
//設(shè)置彈出的交互鍵盤
let board:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
board.backgroundColor = UIColor.orange
//textField.inputView = board
//設(shè)置副鍵盤視圖
textField.inputAccessoryView = board
//設(shè)置代理
textField.delegate = self
//設(shè)置文本框刪除按鈕的顯示模式
textField.clearButtonMode = UITextField.ViewMode.always
3.Textfiled的方法函數(shù)
//當輸入改變時調(diào)用的方法
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(string)
return true
}
//textField已經(jīng)進入編輯狀態(tài)時調(diào)用的方法
func textFieldDidBeginEditing(_ textField: UITextField) {
print("didBegin")
}
//結(jié)束編輯狀態(tài)時調(diào)用的方法
func textFieldDidEndEditing(_ textField: UITextField) {
print("didEnd")
}
//將要進入編輯狀態(tài)時調(diào)用的方法
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("shouldBegin")
return true
}
//將要結(jié)束編輯狀態(tài)時調(diào)用的方法
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("shouldEnd")
return true
}
//當點擊刪除按鈕時觸發(fā)的方法
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return false
}
//點擊return鍵觸發(fā)的方法
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
textField.resignFirstResponder()
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者