加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 文本输入框(UITextField)的用法

发布时间:2020-12-14 01:28:40 所属栏目:百科 来源:网络整理
导读:1,文本框的创建,有如下几个样式: UITextBorderStyle.None :无边框 UITextBorderStyle.Line :直线边框 UITextBorderStyle.RoundedRect :圆角矩形边框 UITextBorderStyle.Bezel :边线+阴影 1 2 3 4 let textField = UITextField (frame: CGRectMake (10
1,文本框的创建,有如下几个样式:
UITextBorderStyle.None:无边框
UITextBorderStyle.Line:直线边框
UITextBorderStyle.RoundedRect:圆角矩形边框
UITextBorderStyle.Bezel:边线+阴影
1
2
3
4
let textField = UITextField (frame: CGRectMake (10,160,200,30))
//设置边框样式为圆角矩形
textField.borderStyle = UITextBorderStyle . RoundedRect
self .view.addSubview(textField)

2,文本框提示文字
1
textField.placeholder= "请输入用户名"

3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
2
textField.adjustsFontSizeToFitWidth= true //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14 //最小可缩小的字号

4,水平/垂直对齐方式
4
5
6
7
8
9
/** 水平对齐 **/
textField.textAlignment = . Right //水平右对齐
Center //水平居中对齐
Left //水平左对齐
/** 垂直对齐 **/
textField.contentVerticalAlignment = . Top //垂直向上对齐
//垂直居中对齐
Bottom //垂直向下对齐

5,背景图片设置
textField.borderStyle = .None//先要去除边框样式
textField.background= UIImage (named: "background1" );
6,清除按钮(输入框内右侧小叉)
3
textField.clearButtonMode= UITextFieldViewMode WhileEditing //编辑时出现清除按钮
UnlessEditing //编辑时不出现,编辑后才出现清除按钮
Always //一直显示清除按钮
7,设置文本框关联的键盘类型
Default :系统默认的虚拟键盘
ASCII Capable :显示英文字母的虚拟键盘
Numbers and Punctuation :显示数字和标点的虚拟键盘
URL :显示便于输入数字的虚拟键盘
Number Pad Phone Pad :显示便于拨号呼叫的虚拟键盘
Name Phone Pad :显示便于聊天拨号的虚拟键盘
Email Address :显示便于输入Email的虚拟键盘
Decimal Pad :显示用于输入数字和小数点的虚拟键盘
Twitter :显示方便些Twitter的虚拟键盘
Web Search :显示便于在网页上书写的虚拟键盘
textField.keyboardType =UIKeyboardTypeNumberPad

8,使文本框在界面打开时就获取焦点,并弹出输入键盘
textField.becomeFirstResponder()
9,使文本框失去焦点,并收回键盘
textField.resignfirstresponder()
10,设置键盘return键的样式
6
textField.returnKeyType = UIReturnKeyType Done //表示完成输入
Go //表示完成输入,同时会跳到另一页
Search //表示搜索
Join //表示注册用户或添加数据
Next //表示继续下一步
Send //表示发送
11,键盘return键的响应
1
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ViewController : UIViewController , UITextFieldDelegate {
override func viewDidLoad() {
super .viewDidLoad()
//设置边框样式为圆角矩形
RoundedRect
Done
textField.delegate= self
.view.addSubview(textField)
}
textFieldShouldReturn(textField: ) -> Bool
{
//收起键盘
textField.resignFirstResponder()
//打印出文本框中的值
print (textField.text)
return true ;
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读