Swift - 文本输入框(UITextField)的用法
生活随笔
收集整理的這篇文章主要介紹了
Swift - 文本输入框(UITextField)的用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1,文本框的創(chuàng)建,有如下幾個(gè)樣式: UITextBorderStyle.None:無(wú)邊框 UITextBorderStyle.Line:直線邊框 UITextBorderStyle.RoundedRect:圓角矩形邊框 UITextBorderStyle.Bezel:邊線+陰影
2,文本框提示文字
3,文字大小超過(guò)文本框長(zhǎng)度時(shí)自動(dòng)縮小字號(hào),而不是隱藏顯示省略號(hào)
4,水平/垂直對(duì)齊方式
5,背景圖片設(shè)置
6,清除按鈕(輸入框內(nèi)右側(cè)小叉)
7,設(shè)置文本框關(guān)聯(lián)的鍵盤(pán)類型
Default:系統(tǒng)默認(rèn)的虛擬鍵盤(pán)
ASCII Capable:顯示英文字母的虛擬鍵盤(pán)
Numbers and Punctuation:顯示數(shù)字和標(biāo)點(diǎn)的虛擬鍵盤(pán)
URL:顯示便于輸入數(shù)字的虛擬鍵盤(pán)
Number Pad:顯示便于輸入數(shù)字的虛擬鍵盤(pán)
Phone Pad:顯示便于撥號(hào)呼叫的虛擬鍵盤(pán)
Name Phone Pad:顯示便于聊天撥號(hào)的虛擬鍵盤(pán)
Email Address:顯示便于輸入Email的虛擬鍵盤(pán)
Decimal Pad:顯示用于輸入數(shù)字和小數(shù)點(diǎn)的虛擬鍵盤(pán)
Twitter:顯示方便些Twitter的虛擬鍵盤(pán)
Web Search:顯示便于在網(wǎng)頁(yè)上書(shū)寫(xiě)的虛擬鍵盤(pán)
8,使文本框在界面打開(kāi)時(shí)就獲取焦點(diǎn),并彈出輸入鍵盤(pán)
9,使文本框失去焦點(diǎn),并收回鍵盤(pán)
10,設(shè)置鍵盤(pán)return鍵的樣式
11,鍵盤(pán)return鍵的響應(yīng)
| 1 2 3 4 | var textField = UITextField(frame: CGRectMake(10,160,200,30)) //設(shè)置邊框樣式為圓角矩形 textField.borderStyle = UITextBorderStyle.RoundedRect self.view.addSubview(textField) |
2,文本框提示文字
| 1 | textField.placeholder="請(qǐng)輸入用戶名" |
3,文字大小超過(guò)文本框長(zhǎng)度時(shí)自動(dòng)縮小字號(hào),而不是隱藏顯示省略號(hào)
| 1 2 | textField.adjustsFontSizeToFitWidth=true? //當(dāng)文字超出文本框?qū)挾葧r(shí),自動(dòng)調(diào)整文字大小 textField.minimumFontSize=14? //最小可縮小的字號(hào) |
4,水平/垂直對(duì)齊方式
| 1 2 3 4 5 6 7 8 9 | /** 水平對(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ì)齊 |
5,背景圖片設(shè)置
| 1 | textField.background=UIImage(named:"background1"); |
6,清除按鈕(輸入框內(nèi)右側(cè)小叉)
| 1 2 3 | textField.clearButtonMode=UITextFieldViewMode.WhileEditing? //編輯時(shí)出現(xiàn)清除按鈕 textField.clearButtonMode=UITextFieldViewMode.UnlessEditing? //編輯時(shí)不出現(xiàn),編輯后才出現(xiàn)清除按鈕 textField.clearButtonMode=UITextFieldViewMode.Always? //一直顯示清除按鈕 |
7,設(shè)置文本框關(guān)聯(lián)的鍵盤(pán)類型
Default:系統(tǒng)默認(rèn)的虛擬鍵盤(pán)
ASCII Capable:顯示英文字母的虛擬鍵盤(pán)
Numbers and Punctuation:顯示數(shù)字和標(biāo)點(diǎn)的虛擬鍵盤(pán)
URL:顯示便于輸入數(shù)字的虛擬鍵盤(pán)
Number Pad:顯示便于輸入數(shù)字的虛擬鍵盤(pán)
Phone Pad:顯示便于撥號(hào)呼叫的虛擬鍵盤(pán)
Name Phone Pad:顯示便于聊天撥號(hào)的虛擬鍵盤(pán)
Email Address:顯示便于輸入Email的虛擬鍵盤(pán)
Decimal Pad:顯示用于輸入數(shù)字和小數(shù)點(diǎn)的虛擬鍵盤(pán)
Twitter:顯示方便些Twitter的虛擬鍵盤(pán)
Web Search:顯示便于在網(wǎng)頁(yè)上書(shū)寫(xiě)的虛擬鍵盤(pán)
| 1 | textField.keyboardType = UIKeyboardType.NumberPad |
8,使文本框在界面打開(kāi)時(shí)就獲取焦點(diǎn),并彈出輸入鍵盤(pán)
| 1 | textField.becomeFirstResponder() |
9,使文本框失去焦點(diǎn),并收回鍵盤(pán)
| 1 | textField.resignfirstresponder() |
10,設(shè)置鍵盤(pán)return鍵的樣式
| 1 2 3 4 5 6 | 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ā)送 |
11,鍵盤(pán)return鍵的響應(yīng)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class ViewController: UIViewController,UITextFieldDelegate { ????override func viewDidLoad() { ????????super.viewDidLoad() ????????var textField = UITextField(frame: CGRectMake(10,160,200,30)) ????????//設(shè)置邊框樣式為圓角矩形 ????????textField.borderStyle = UITextBorderStyle.RoundedRect ????????textField.returnKeyType = UIReturnKeyType.Done?????? ????????textField.delegate=self ????????self.view.addSubview(textField) ????} ????? ????func textFieldShouldReturn(textField:UITextField) -> Bool ????{ ????????//收起鍵盤(pán) ????????textField.resignFirstResponder() ????????//打印出文本框中的值 ????????println(textField.text) ????????return true; ????} } |
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/4838136.html
總結(jié)
以上是生活随笔為你收集整理的Swift - 文本输入框(UITextField)的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL Server 2008数据库,显
- 下一篇: vijos1774:机器翻译