swift - UITextField 的用法
2024-09-02 18:11:06
1,文本框的创建,有如下几个样式:
public enum UITextBorderStyle : Int { case none 无边框 case line 直线边框 case bezel 圆角矩形边框 case roundedRect 边线+阴影
}
let textField = UITextField(frame:CGRect(x:50,y:240,width:self.view.bounds.size.width - 100,height:50))
//设置边框样式为圆角矩形
textField.borderStyle = .roundedRect
self.view.addSubview(textField)
2,文本框提示文字
textField.placeholder="请输入用户名"
3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号
textField.adjustsFontSizeToFitWidth=true //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize= //最小可缩小的字号
4,水平/垂直对齐方式
/** 水平对齐 **/
textField.textAlignment = .right //水平右对齐
textField.textAlignment = .center //水平居中对齐
textField.textAlignment = .left //水平左对齐
textField.textAlignment = .justified textField.textAlignment = .natural
/** 垂直对齐 **/
textField.contentVerticalAlignment = .top //垂直向上对齐
textField.contentVerticalAlignment = .center //垂直居中对齐
textField.contentVerticalAlignment = .bottom //垂直向下对齐
textField.contentVerticalAlignment = .fill //填充满
/*水平对其的属性和垂直对齐是一样的*/
5,背景图片设置
textField.borderStyle = .none //先要去除边框样式
textField.background=UIImage(named:"background1");
6,清除按钮(输入框内右侧小叉
textField.clearButtonMode=.whileEditing //编辑时出现清除按钮
textField.clearButtonMode=.unlessEditing //编辑时不出现,编辑后才出现清除按钮
textField.clearButtonMode=.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:显示便于在网页上书写的虚拟键盘
asciiCapableNumberPad //显示便于输入数字的虚拟键盘 只支持iOS10
textField.keyboardType = .numberPad
8,使文本框在界面打开时就获取焦点,并弹出输入键盘
textField.becomeFirstResponder()
9,使文本框失去焦点,并收回键盘
textField.resignfirstresponder()
10,设置键盘return键的样式
textField.returnKeyType = .done //表示完成输入
textField.returnKeyType = .go //表示完成输入,同时会跳到另一页
textField.returnKeyType = .search //表示搜索
textField.returnKeyType = .join //表示注册用户或添加数据
textField.returnKeyType = .next //表示继续下一步
textField.returnKeyType = .send //表示发送
textField.returnKeyType = .yahoo //雅虎
textField.returnKeyType = .done //显示完成
textField.returnKeyType = .emergencyCall //显示紧急呼叫
11,键盘return键的响应
//设置代理
class FirstyViewController: UIViewController,UITextFieldDelegate
实现代理方法
func textFieldShouldReturn(textField:UITextField) -> Bool
{
//收起键盘
textField.resignFirstResponder()
//打印出文本框中的值
print(textField.text)
return true;
}
12,点击空白处回收键盘
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
textField.resignFirstResponder()
}
最新文章
- Oracle逻辑备份与恢复
- python学习08——类
- web攻防
- Java 中常用缓存Cache机制的实现
- Git push 常见用法
- Demo3使用bootstrap
- How to bind to data when the DataContext is not inherited【项目】
- 只看Delphi自带的WnAPI帮助似乎不够
- 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称
- 如何快速高效地完成一个Android项目?
- 201521123034《Java程序设计》第十一周学习总结
- ajax补充--------FormData等...
- Ubuntu 14 如何解压 .zip、.rar 文件
- Asp.Net MVC EF查询部分字段
- powershell玩转litedb数据库-第二版
- Lambda表达式浅析
- DrawCall 优化 .
- Java 8新增的Lambda表达式
- FBV和CBV区别
- [翻译] AFSoundManager
热门文章
- ubuntu 文档查看器/gedit查看txt中文乱码问题
- Hibernate中HQLwhere用法和单独取出几列数据的读取方法
- Eclipse中直接操作本地文件系统
- LeetCode: Word Ladder II 解题报告
- show global status和show variables mysql 优化
- docker centos7 dbus error解决方法
- http://blog.csdn.net/beitiandijun/article/details/41678251
- PHP——初学,基础
- MySQL死锁原因分析
- Workerman