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è)置輸入框風(fēng)格為line
textField.borderStyle = UITextField.BorderStyle.line
//設(shè)置輸入框風(fēng)格為bezel
textField.borderStyle = UITextField.BorderStyle.bezel
//設(shè)置輸入框的提示文字
textField.placeholder = "請(qǐng)輸入文字"
//設(shè)置輸入的文字顏色
textField.textColor = UIColor.blue
//設(shè)置文字的字體
textField.font = UIFont.italicSystemFont(ofSize: 20)
//設(shè)置文字的對(duì)齊方式
textField.textAlignment = NSTextAlignment.center
//textField.backgroundColor = UIColor.red
//是否每次進(jìn)入編輯狀態(tài)時(shí)都清空輸入框中的文字
textField.clearsOnBeginEditing = true
//是否字體大小自適應(yīng)
textField.adjustsFontSizeToFitWidth = true
//設(shè)置輸入框?yàn)闊o(wú)效
//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ù)
//當(dāng)輸入改變時(shí)調(diào)用的方法
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(string)
return true
}
//textField已經(jīng)進(jìn)入編輯狀態(tài)時(shí)調(diào)用的方法
func textFieldDidBeginEditing(_ textField: UITextField) {
print("didBegin")
}
//結(jié)束編輯狀態(tài)時(shí)調(diào)用的方法
func textFieldDidEndEditing(_ textField: UITextField) {
print("didEnd")
}
//將要進(jìn)入編輯狀態(tài)時(shí)調(diào)用的方法
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("shouldBegin")
return true
}
//將要結(jié)束編輯狀態(tài)時(shí)調(diào)用的方法
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("shouldEnd")
return true
}
//當(dāng)點(diǎn)擊刪除按鈕時(shí)觸發(fā)的方法
func textFieldShouldClear(_ textField: UITextField) -> Bool {
return false
}
//點(diǎn)擊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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。