1,文本框的創(chuàng)建,有如下幾個樣式:
UITextBorderStyle.None:無邊框
UITextBorderStyle.Line:直線邊框
UITextBorderStyle.RoundedRect:圓角矩形邊框
UITextBorderStyle.Bezel:邊線+陰影
lettextField =UITextField(frame:CGRectMake(10,160,200,30))
//設置邊框樣式為圓角矩形
textField.borderStyle =UITextBorderStyle.RoundedRect
self.view.addSubview(textField)
2,文本框提示文字
1
textField.placeholder="請輸入用戶名"
3,文字大小超過文本框長度時自動縮小字號,而不是隱藏顯示省略號
textField.adjustsFontSizeToFitWidth=true//當文字超出文本框?qū)挾葧r页徐,自動調(diào)整文字大小
textField.minimumFontSize=14//最小可縮小的字號
4,水平/垂直對齊方式
/** 水平對齊 **/
textField.textAlignment = .Right//水平右對齊
textField.textAlignment = .Center//水平居中對齊
textField.textAlignment = .Left//水平左對齊
/** 垂直對齊 **/
textField.contentVerticalAlignment = .Top//垂直向上對齊
textField.contentVerticalAlignment = .Center//垂直居中對齊
textField.contentVerticalAlignment = .Bottom//垂直向下對齊
5银萍,背景圖片設置
textField.borderStyle = .None//先要去除邊框樣式
textField.background=UIImage(named:"background1");
6变勇,清除按鈕(輸入框內(nèi)右側(cè)小叉)
textField.clearButtonMode=UITextFieldViewMode.WhileEditing//編輯時出現(xiàn)清除按鈕
textField.clearButtonMode=UITextFieldViewMode.UnlessEditing//編輯時不出現(xiàn),編輯后才出現(xiàn)清除按鈕
textField.clearButtonMode=UITextFieldViewMode.Always//一直顯示清除按鈕
7贴唇,設置文本框關聯(lián)的鍵盤類型
Default:系統(tǒng)默認的虛擬鍵盤
ASCII Capable:顯示英文字母的虛擬鍵盤
Numbers and Punctuation:顯示數(shù)字和標點的虛擬鍵盤
URL:顯示便于輸入數(shù)字的虛擬鍵盤
Number Pad:顯示便于輸入數(shù)字的虛擬鍵盤
Phone Pad:顯示便于撥號呼叫的虛擬鍵盤
Name Phone Pad:顯示便于聊天撥號的虛擬鍵盤
Email Address:顯示便于輸入Email的虛擬鍵盤
Decimal Pad:顯示用于輸入數(shù)字和小數(shù)點的虛擬鍵盤
Twitter:顯示方便些Twitter的虛擬鍵盤
Web Search:顯示便于在網(wǎng)頁上書寫的虛擬鍵盤
textField.keyboardType =UIKeyboardType.NumberPad
8搀绣,使文本框在界面打開時就獲取焦點,并彈出輸入鍵盤
textField.becomeFirstResponder()
9戳气,使文本框失去焦點链患,并收回鍵盤
textField.resignfirstresponder()
10,設置鍵盤return鍵的樣式
textField.returnKeyType =UIReturnKeyType.Done//表示完成輸入
textField.returnKeyType =UIReturnKeyType.Go//表示完成輸入瓶您,同時會跳到另一頁
textField.returnKeyType =UIReturnKeyType.Search//表示搜索
textField.returnKeyType =UIReturnKeyType.Join//表示注冊用戶或添加數(shù)據(jù)
textField.returnKeyType =UIReturnKeyType.Next//表示繼續(xù)下一步
textField.returnKeyType =UIReturnKeyType.Send//表示發(fā)送
11麻捻,鍵盤return鍵的響應
class
ViewController:UIViewController,UITextFieldDelegate{
overridefuncviewDidLoad() {
super.viewDidLoad()
lettextField =UITextField(frame:CGRectMake(10,160,200,30))
//設置邊框樣式為圓角矩形
textField.borderStyle =UITextBorderStyle.RoundedRect
textField.returnKeyType =UIReturnKeyType.Done
textField.delegate=self
self.view.addSubview(textField)
}
functextFieldShouldReturn(textField:UITextField) ->Bool
{
//收起鍵盤
textField.resignFirstResponder()
//打印出文本框中的值
print(textField.text)
returntrue;
}
}
article end operate_box