1,文本框的创建,有如下几个样式:
圆角矩形边框样例:
2,文本框提示文字
3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
4,水平/垂直对齐方式
5,背景图片设置
8,设置文本框关联的键盘类型
Default:系统默认的虚拟键盘
ASCII Capable:显示英文字母的虚拟键盘
Numbers and Punctuation:显示数字和标点的虚拟键盘
URL:显示便于输入url网址的虚拟键盘
Number Pad:显示便于输入数字的虚拟键盘
Phone Pad:显示便于拨号呼叫的虚拟键盘
Name Phone Pad:显示便于聊天拨号的虚拟键盘
Email Address:显示便于输入Email的虚拟键盘
Decimal Pad:显示用于输入数字和小数点的虚拟键盘
Twitter:显示方便些Twitter的虚拟键盘
Web Search:显示便于在网页上书写的虚拟键盘
数字键盘使用样例:
9,使文本框在界面打开时就获取焦点,并弹出输入键盘
10,使文本框失去焦点,并收回键盘
11,设置键盘return键的样式
12,键盘return键的响应
UITextBorderStyle.none:无边框
UITextBorderStyle.line:直线边框
UITextBorderStyle.roundedRect:圆角矩形边框
UITextBorderStyle.bezel:边线+阴影
圆角矩形边框样例:
1
2
3
4
|
let
textField =
UITextField
(frame:
CGRect
(x:10, y:60, width:200, height:30))
//设置边框样式为圆角矩形
textField.borderStyle =
UITextBorderStyle
.roundedRect
self
.view.addSubview(textField)
|
1
|
textField.placeholder=
"请输入用户名"
|
1
2
|
textField.adjustsFontSizeToFitWidth=
true
//当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14
//最小可缩小的字号
|
1
2
3
4
5
6
7
8
9
|
/** 水平对齐 **/
textField.textAlignment = .right
//水平右对齐
textField.textAlignment = .center
//水平居中对齐
textField.textAlignment = .left
//水平左对齐
/** 垂直对齐 **/
textField.contentVerticalAlignment = .top
//垂直向上对齐
textField.contentVerticalAlignment = .center
//垂直居中对齐
textField.contentVerticalAlignment = .bottom
//垂直向下对齐
|
1
2
|
textField.borderStyle = .none
//先要去除边框样式
textField.background =
UIImage
(named:
"background1"
);
|
6,设置光标的颜色
textField.tintColor = UIColor.cyanColor()
7,清除按钮(输入框内右侧小叉)
1
2
3
|
textField.clearButtonMode = .whileEditing
//编辑时出现清除按钮
textField.clearButtonMode = .unlessEditing
//编辑时不出现,编辑后才出现清除按钮
textField.clearButtonMode = .always
//一直显示清除按钮
|
Default:系统默认的虚拟键盘
ASCII Capable:显示英文字母的虚拟键盘
Numbers and Punctuation:显示数字和标点的虚拟键盘
URL:显示便于输入url网址的虚拟键盘
Number Pad:显示便于输入数字的虚拟键盘
Phone Pad:显示便于拨号呼叫的虚拟键盘
Name Phone Pad:显示便于聊天拨号的虚拟键盘
Email Address:显示便于输入Email的虚拟键盘
Decimal Pad:显示用于输入数字和小数点的虚拟键盘
Twitter:显示方便些Twitter的虚拟键盘
Web Search:显示便于在网页上书写的虚拟键盘
数字键盘使用样例:
1
|
textField.keyboardType =
UIKeyboardType
.numberPad
|
9,使文本框在界面打开时就获取焦点,并弹出输入键盘
1
|
textField.becomeFirstResponder()
|
1
|
textField.resignFirstResponder()
|
1
2
3
4
5
6
|
textField.returnKeyType =
UIReturnKeyType
.done
//表示完成输入
textField.returnKeyType =
UIReturnKeyType
.go
//表示完成输入,同时会跳到另一页
textField.returnKeyType =
UIReturnKeyType
.search
//表示搜索
textField.returnKeyType =
UIReturnKeyType
.
join
//表示注册用户或添加数据
textField.returnKeyType =
UIReturnKeyType
.next
//表示继续下一步
textField.returnKeyType =
UIReturnKeyType
.send
//表示发送
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
UIKit
class
ViewController
:
UIViewController
,
UITextFieldDelegate
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
textField =
UITextField
(frame:
CGRect
(x:10,y:160,width:200,height:30))
//设置边框样式为圆角矩形
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
}
}
|