1, the creation of the text box, there are several styles: uitextborderstyle.none: no Border uitextborderstyle.line: line Border uitextborderstyle.roundedrect: Rounded rectangle border Uitextborderstyle.bezel: Edge + Shadow
1234 |
var textField = UITextField (frame: CGRectMake (10,160,200,30)) //设置边框样式为圆角矩形 textField.borderStyle = UITextBorderStyle . RoundedRect self .view.addSubview(textField) |
2, text box prompt text
1 |
textField.placeholder= "请输入用户名" |
3, the text size exceeds the text box length automatically reduces the font size, instead of hiding the ellipsis display
12 |
textField.adjustsFontSizeToFitWidth= true //当文字超出文本框宽度时,自动调整文字大小 textField.minimumFontSize=14 //最小可缩小的字号 |
4, Horizontal/Vertical alignment
123456789 |
/** 水平对齐 **/
textField.textAlignment = .
Right //水平右对齐
textField.textAlignment = .
Center //水平居中对齐
textField.textAlignment = .
Left //水平左对齐 /** 垂直对齐 **/
textField.contentVerticalAlignment = .
Top
//垂直向上对齐
textField.contentVerticalAlignment = .
Center
//垂直居中对齐
textField.contentVerticalAlignment = .
Bottom
//垂直向下对齐
|
5, Background image settings
1 |
textField.background= UIImage (named: "background1" ); |
6, clear the button (the right fork in the input box)
123 |
textField.clearButtonMode= UITextFieldViewMode . WhileEditing //编辑时出现清除按钮 textField.clearButtonMode= UITextFieldViewMode . UnlessEditing //编辑时不出现,编辑后才出现清除按钮 textField.clearButtonMode= UITextFieldViewMode . Always //一直显示清除按钮 |
7, setting the keyboard type associated with the text box
Default: The system defaults to the virtual keyboard
ascii capable: virtual keyboard showing English alphabet
url: Display a virtual keyboard for easy-to-enter numbers
number pad: Displays a virtual keyboard that is easy to enter a number
phone Pad: Display a virtual keyboard that facilitates dial-up calls
name Phone Pad: Display a virtual keyboard for easy chat dialing
email Address: Displays a virtual keyboard that is easy to enter email
decimal Pad: Displays a virtual keyboard for entering numbers and decimals
twitter: Display the virtual keyboard for easy Twitter
web Search: Display a virtual keyboard that is easy to write on a Web page
1 |
textfield.keyboardtype = uikeyboardtype numberpad |
< Span style= "color: #0000ff;" >
8 so that the text box gets focus when the interface is open and pop-up input keyboard
1 |
textField.becomeFirstResponder() |
< Span style= "color: #0000ff;" >9, causes the text box to lose focus and retract the keyboard
1 |
textField.resignfirstresponder() |
10. Set the style of the keyboard return key
123456 |
textField.returnKeyType =
UIReturnKeyType
.
Done //表示完成输入
textField.returnKeyType =
UIReturnKeyType
.
Go //表示完成输入,同时会跳到另一页
textField.returnKeyType =
UIReturnKeyType
.
Search //表示搜索
textField.returnKeyType =
UIReturnKeyType
.
Join //表示注册用户或添加数据
textField.returnKeyType =
UIReturnKeyType
.
Next //表示继续下一步
textField.returnKeyType =
UIReturnKeyType
.
Send //表示发送
|
11, the response of the keyboard return key
12345678910111213141516171819202122 |
class ViewController
:
UIViewController
,
UITextFieldDelegate {
override func viewDidLoad() {
super
.viewDidLoad()
var textField =
UITextField
(frame:
CGRectMake
(10,160,200,30))
//设置边框样式为圆角矩形
textField.borderStyle =
UITextBorderStyle
.
RoundedRect
textField.returnKeyType =
UIReturnKeyType
.
Done
textField.delegate=
self
self
.view.addSubview(textField)
}
func textFieldShouldReturn(textField:
UITextField
) ->
Bool
{
//收起键盘
textField.resignFirstResponder()
//打印出文本框中的值
println
(textField.text)
return true
;
}
}
|
Use of Swift-text input box (Uitextfield)