1,文本框的創(chuàng)建,有如下幾個(gè)樣式:
UITextBorderStyle.none:無(wú)邊框
UITextBorderStyle.line:直線邊框
UITextBorderStyle.roundedRect:圓角矩形邊框
UITextBorderStyle.bezel:邊線+陰影
let?textField =?UITextField(frame:?CGRect(x:10, y:60, width:200, height:30))
//設(shè)置邊框樣式為圓角矩形
textField.borderStyle =?UITextBorderStyle.roundedRect
self.view.addSubview(textField)
2,修改邊框顏色、粗細(xì)、圓角半徑
let?textField =?UITextField(frame:?CGRect(x:10, y:60, width:200, height:30))
//設(shè)置邊框樣式為圓角矩形
textField.borderStyle =?UITextBorderStyle.roundedRect
self.view.addSubview(textField)
//修改圓角半徑的話需要將masksToBounds設(shè)為true
textField.layer.masksToBounds =?true
textField.layer.cornerRadius = 12.0?//圓角半徑
textField.layer.borderWidth = 2.0?//邊框粗細(xì)
textField.layer.borderColor =?UIColor.red.cgColor?//邊框顏色
3,文本框提示文字
textField.placeholder="請(qǐng)輸入用戶名"
4,文字大小超過(guò)文本框長(zhǎng)度時(shí)自動(dòng)縮小字號(hào),而不是隱藏顯示省略號(hào)
textField.adjustsFontSizeToFitWidth=true?//當(dāng)文字超出文本框?qū)挾葧r(shí),自動(dòng)調(diào)整文字大小
textField.minimumFontSize=14?//最小可縮小的字號(hào)
5,水平/垂直對(duì)齊方式
/** 水平對(duì)齊 **/
textField.textAlignment = .right?//水平右對(duì)齊
textField.textAlignment = .center?//水平居中對(duì)齊
textField.textAlignment = .left?//水平左對(duì)齊
/** 垂直對(duì)齊 **/
textField.contentVerticalAlignment = .top?//垂直向上對(duì)齊
textField.contentVerticalAlignment = .center?//垂直居中對(duì)齊
textField.contentVerticalAlignment = .bottom?//垂直向下對(duì)齊
6,背景圖片設(shè)置
textField.borderStyle = .none?//先要去除邊框樣式
textField.background =?UIImage(named:"background1");
7,清除按鈕(輸入框內(nèi)右側(cè)小叉)
textField.clearButtonMode = .never?//從不出現(xiàn)
textField.clearButtonMode = .whileEditing?//編輯時(shí)出現(xiàn)清除按鈕
textField.clearButtonMode = .unlessEditing?//編輯時(shí)不出現(xiàn),編輯后才出現(xiàn)清除按鈕
textField.clearButtonMode = .always?//一直顯示清除按鈕
8,密碼輸入框
textField.isSecureTextEntry =?true?//輸入內(nèi)容會(huì)顯示成小黑點(diǎn)
9,設(shè)置文本框關(guān)聯(lián)的鍵盤類型
textField.keyboardType =?UIKeyboardType.numberPad
keyboardType設(shè)置值:
Default:系統(tǒng)默認(rèn)的虛擬鍵盤
ASCII Capable:顯示英文字母的虛擬鍵盤
Numbers and Punctuation:顯示數(shù)字和標(biāo)點(diǎn)的虛擬鍵盤
URL:顯示便于輸入url網(wǎng)址的虛擬鍵盤
Number Pad:顯示便于輸入數(shù)字的虛擬鍵盤
Phone Pad:顯示便于撥號(hào)呼叫的虛擬鍵盤
Name Phone Pad:顯示便于聊天撥號(hào)的虛擬鍵盤
Email Address:顯示便于輸入Email的虛擬鍵盤
Decimal Pad:顯示用于輸入數(shù)字和小數(shù)點(diǎn)的虛擬鍵盤
Twitter:顯示方便些Twitter的虛擬鍵盤
Web Search:顯示便于在網(wǎng)頁(yè)上書寫的虛擬鍵盤
10,使文本框在界面打開時(shí)就獲取焦點(diǎn),并彈出輸入鍵盤
textField.becomeFirstResponder()
11,使文本框失去焦點(diǎn),并收回鍵盤
textField.resignFirstResponder()
12,設(shè)置鍵盤 return 鍵的樣式
textField.returnKeyType =?UIReturnKeyType.done?//表示完成輸入
textField.returnKeyType =?UIReturnKeyType.go?//表示完成輸入,同時(shí)會(huì)跳到另一頁(yè)
textField.returnKeyType =?UIReturnKeyType.search?//表示搜索
textField.returnKeyType =?UIReturnKeyType.join?//表示注冊(cè)用戶或添加數(shù)據(jù)
textField.returnKeyType =?UIReturnKeyType.next?//表示繼續(xù)下一步
textField.returnKeyType =?UIReturnKeyType.send?//表示發(fā)送
13,鍵盤 return 鍵的響應(yīng)
import?UIKit
class?ViewController:?UIViewController,UITextFieldDelegate?{
????override?func?viewDidLoad() {
????????super.viewDidLoad()
????????let?textField =?UITextField(frame:?CGRect(x:10,y:160,width:200,height:30))
????????//設(shè)置邊框樣式為圓角矩形
????????textField.borderStyle =?UITextBorderStyle.roundedRect
????????textField.returnKeyType =?UIReturnKeyType.done
????????textField.delegate=self
????????self.view.addSubview(textField)
????}
????func?textFieldShouldReturn(_ textField:?UITextField) ->?Bool?{
????????//收起鍵盤
????????textField.resignFirstResponder()
????????//打印出文本框中的值
????????print(textField.text ???"")
????????return?true
????}
}
