Swift - 文本输入框(UITextField)的用法
1
2
3
4
|
var textField = UITextField (frame: CGRectMake (10,160,200,30))
//设置边框样式为圆角矩形
textField.borderStyle = UITextBorderStyle . RoundedRect
self .view.addSubview(textField)
|
2,文本框提示文字
1
|
textField.placeholder= "请输入用户名"
|
3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
1
2
|
textField.adjustsFontSizeToFitWidth= true //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14 //最小可缩小的字号
|
4,水平/垂直对齐方式
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 //垂直向下对齐
|
5,背景图片设置
1
|
textField.background= UIImage (named: "background1" );
|
6,清除按钮(输入框内右侧小叉)
1
2
3
|
textField.clearButtonMode= UITextFieldViewMode . WhileEditing //编辑时出现清除按钮
textField.clearButtonMode= UITextFieldViewMode . UnlessEditing //编辑时不出现,编辑后才出现清除按钮
textField.clearButtonMode= UITextFieldViewMode . 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:显示便于在网页上书写的虚拟键盘
1
|
textField.keyboardType = UIKeyboardType . NumberPad
|
8,使文本框在界面打开时就获取焦点,并弹出输入键盘
1
|
textField.becomeFirstResponder()
|
9,使文本框失去焦点,并收回键盘
1
|
textField.resignfirstresponder()
|
10,设置键盘return键的样式
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 //表示发送
|
11,键盘return键的响应
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))
//设置边框样式为圆角矩形
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 ;
}
}
|
转载于:https://www.cnblogs.com/Free-Thinker/p/4838136.html
Swift - 文本输入框(UITextField)的用法相关推荐
- Swift - 文本输入框内容改变时响应,并获取最新内容
1,问题描述 有时我们开发的时候需要先把"确认"按钮初始设置为不可用,当文本框中输入文字以后,再将输入按钮变为可用. 2,实现原理 (1)要检测文本框内容的变化,我们需要让新界面的 ...
- IOS 单行文本输入框 UITextField 使用
UITextField 继承 UIControl 类,只支持单行输入和显示,可输入密码类型.支持实现代理 UITextFieldDelegate 属性 名称 类型 说明 默认值 text NSStri ...
- 代码创建按钮,文本输入框
//创建按钮UIButton *btn = [[UIButton alloc] init];btn.frame = CGRectMake(0, 0, 100, 100);// 设置按钮在普通状态下的属 ...
- alert获取输入框内容_实用开源:Web 聊天工具的富文本输入框
最近折腾 Websocket,打算开发一个聊天室应用练练手.在应用开发的过程中发现可以插入 emoji ,粘贴图片的富文本输入框其实蕴含着许多有趣的知识,于是便打算记录下来和大家分享. 仓库地址:ch ...
- android实现文本输入,Android实现智能提示的文本输入框AutoCompleteTextView
今天我们要讲一个十分简单的内容,就是一个安卓控件的使用,用法很简单,但是很常用的一个.这里我用两种不同的写法来处理.当然,无论用哪一种写法,效果都是一样的. 我们先来看效果图. 要实现这种效果十分简单 ...
- python qt5 增加文本框_PyQt5 文本输入框自动补全 QLineEdit
一.QCompleter类 - 自动补全会用到的一个类 主要代码 def init_lineedit(self): # 增加自动补全 self.completer = QCompleter(items ...
- python qt5 增加文本框_PyQt5 文本输入框自动补全QLineEdit的实现示例
一.QCompleter类 自动补全会用到的一个类 主要代码 def init_lineedit(self): # 增加自动补全 self.completer = QCompleter(items_l ...
- Flutter文本输入框TextField属性(InputDecoration、textInputAction、inputFormatters等等)详解
文章目录 TextField TextField decoration keyboardType TextField textInputAction TextField onChanged TextF ...
- 文本输入框、密码输入框
当用户要在表单中键入字母.数字等内容时,就会用到文本输入框.文本框也可以转化为密码输入框. 语法: <form><input type="text/password&quo ...
最新文章
- ProgressDialog的使用-------------范例(学习笔记)
- OAF[1]开发环境的配置
- 网盘纷退场 “云存储”仍是刚需
- spring源码分析之cache demo
- MySQL HINT:Straight_JOIN
- 【CyberSecurityLearning 77】DC系列之DC-8渗透测试(Drupal)
- openresty测试lua脚本配置示例
- 解决安装pytorch慢的方法(pip安装)
- sync.Once 的前世今生
- 《炉石传说》建筑设计欣赏(7):采用Google.ProtocolBuffers处理网络消息
- wp8对json的处理
- jQuery知识(转)
- 开放式虚拟仿真实验服务器,润尼尔开放式虚拟仿真实验教学及资源共享平台
- 软工网络15团队作业4——Alpha阶段敏捷冲刺之Scrum 冲刺博客(Day4)
- Python 自动化办公
- 如何使用 DiskGenius 合并分区教程
- VAR.P和VAR.S
- 可以模拟人工操作的软件;如访问网页,在网页中自动获取固定信息等
- Python学习之title()函数
- html和js画圣诞树图片,基于JS2Image实现圣诞树代码,