UITextField是用来文本输入的,比如常用的登录用户名和密码输入等等,那我们直接进入主题吧

1.创建普通的UITextField

//构建基本的UITextField
var uitf:UITextField = UITextField(frame: CGRect(x: 50, y: 50, width: 200, height: 30))
uitf.backgroundColor = UIColor.whiteColor()
uitf.placeholder = "普通UITextField"

2.构建密码输入框

var uitf1:UITextField = UITextField(frame: CGRect(x: 50, y: 90, width: 200, height: 35))
uitf1.placeholder = "密码输入框"
uitf1.borderStyle = UITextBorderStyle.RoundedRect //边框样式
uitf1.backgroundColor = UIColor.whiteColor()
uitf1.secureTextEntry = true //密码输入框

3.设置UITextField的左边视图

var uitf2:UITextField = UITextField(frame: CGRect(x: 50, y: 135, width: 200, height: 30))
uitf2.placeholder = "请输入购买的物品"
uitf2.backgroundColor = UIColor.whiteColor()
var img:UIImageView = UIImageView(p_w_picpath: UIImage(named: "buy.png")!)
img.frame = CGRect(x: 5, y: 5, width: 40, height: 30)
img.contentMode = UIViewContentMode.ScaleAspectFit
uitf2.leftView = img
uitf2.leftViewMode = UITextFieldViewMode.Always
uitf2.leftViewRectForBounds(CGRect(x: 0, y: 0, width: 44, height: 44))

4.设置UITextField的右边边视图

 var uitf3:UITextField = UITextField(frame: CGRect(x: 50, y: 175, width: 200, height: 35))uitf3.placeholder = "请输入购买的物品"uitf3.backgroundColor = UIColor.whiteColor()uitf3.layer.cornerRadius = 2uitf3.layer.borderWidth = 1uitf3.layer.borderColor = UIColor.greenColor().CGColor//uitf3.borderStyle = UITextBorderStyle.RoundedRectvar img1:UIImageView = UIImageView(p_w_picpath: UIImage(named: "buy.png")!)img1.frame = CGRect(x: 5, y: 5, width: 40, height: 30)img1.contentMode = UIViewContentMode.ScaleAspectFituitf3.rightView = img1uitf3.rightViewMode = UITextFieldViewMode.Always

5.委托事件,及加入clear按钮

var uitf4:UITextField = UITextField(frame: CGRect(x: 50, y: 220, width: 200, height: 35))
uitf4.backgroundColor = UIColor.whiteColor()
uitf4.delegate = self
uitf4.clearButtonMode = UITextFieldViewMode.WhileEditing

当鼠标进入UITextField时,响应textFieldShouldBeginEditing -> textFieldDidBeginEditing
当鼠标退出UITextField时,响应textFieldShouldEndEditing -> textFieldDidEndEditing
当输入文本内容时,响应textField
在输入框里,在虚拟键盘上点击return时,响应textFieldShouldReturn
在输入框里,点击UITextField的clear按钮,响应textFieldShouldClear

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {NSLog("textField")return true}func textFieldShouldBeginEditing(textField: UITextField) -> Bool {NSLog("textFieldShouldBeginEditing")return true}func textFieldDidBeginEditing(textField: UITextField) {NSLog("textFieldDidBeginEditing")}func textFieldShouldEndEditing(textField: UITextField) -> Bool {NSLog("textFieldShouldEndEditing")return true}func textFieldDidEndEditing(textField: UITextField) {NSLog("textFieldDidEndEditing")}func textFieldShouldClear(textField: UITextField) -> Bool {NSLog("textFieldShouldClear")return true}func textFieldShouldReturn(textField: UITextField) -> Bool {NSLog("textFieldShouldReturn")textField.resignFirstResponder() //这个隐藏(放弃)虚拟键盘return true}

全部代码

import UIKitclass ViewController: UIViewController,UITextFieldDelegate {override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.//构建基本的UITextFieldvar uitf:UITextField = UITextField(frame: CGRect(x: 50, y: 50, width: 200, height: 30))uitf.backgroundColor = UIColor.whiteColor()uitf.placeholder = "普通UITextField"self.view.addSubview(uitf)//构建密码输入框var uitf1:UITextField = UITextField(frame: CGRect(x: 50, y: 90, width: 200, height: 35))uitf1.placeholder = "密码输入框"uitf1.borderStyle = UITextBorderStyle.RoundedRect //边框样式uitf1.backgroundColor = UIColor.whiteColor()uitf1.secureTextEntry = true //密码输入框self.view.addSubview(uitf1)//设置UITextField的左边视图var uitf2:UITextField = UITextField(frame: CGRect(x: 50, y: 135, width: 200, height: 30))uitf2.placeholder = "请输入购买的物品"uitf2.backgroundColor = UIColor.whiteColor()var img:UIImageView = UIImageView(p_w_picpath: UIImage(named: "buy.png")!)img.frame = CGRect(x: 5, y: 5, width: 40, height: 30)img.contentMode = UIViewContentMode.ScaleAspectFituitf2.leftView = imguitf2.leftViewMode = UITextFieldViewMode.Alwaysuitf2.leftViewRectForBounds(CGRect(x: 0, y: 0, width: 44, height: 44))self.view.addSubview(uitf2)//设置UITextField的右边边视图var uitf3:UITextField = UITextField(frame: CGRect(x: 50, y: 175, width: 200, height: 35))uitf3.placeholder = "请输入购买的物品"uitf3.backgroundColor = UIColor.whiteColor()uitf3.layer.cornerRadius = 2uitf3.layer.borderWidth = 1uitf3.layer.borderColor = UIColor.greenColor().CGColor//uitf3.borderStyle = UITextBorderStyle.RoundedRectvar img1:UIImageView = UIImageView(p_w_picpath: UIImage(named: "buy.png")!)img1.frame = CGRect(x: 5, y: 5, width: 40, height: 30)img1.contentMode = UIViewContentMode.ScaleAspectFituitf3.rightView = img1uitf3.rightViewMode = UITextFieldViewMode.Alwaysself.view.addSubview(uitf3)//委托事件var uitf4:UITextField = UITextField(frame: CGRect(x: 50, y: 220, width: 200, height: 35))uitf4.backgroundColor = UIColor.whiteColor()uitf4.delegate = selfuitf4.clearButtonMode = UITextFieldViewMode.WhileEditingself.view.addSubview(uitf4)}// 当鼠标进入UITextField时,响应textFieldShouldBeginEditing -> textFieldDidBeginEditing// 当鼠标退出UITextField时,响应textFieldShouldEndEditing -> textFieldDidEndEditing// 当输入文本内容时,响应textField// 在输入框里,在虚拟键盘上点击return时,响应textFieldShouldReturn// 在输入框里,点击UITextField的clear按钮,响应textFieldShouldClearfunc textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {NSLog("textField")return true}func textFieldShouldBeginEditing(textField: UITextField) -> Bool {NSLog("textFieldShouldBeginEditing")return true}func textFieldDidBeginEditing(textField: UITextField) {NSLog("textFieldDidBeginEditing")}func textFieldShouldEndEditing(textField: UITextField) -> Bool {NSLog("textFieldShouldEndEditing")return true}func textFieldDidEndEditing(textField: UITextField) {NSLog("textFieldDidEndEditing")}func textFieldShouldClear(textField: UITextField) -> Bool {NSLog("textFieldShouldClear")return true}func textFieldShouldReturn(textField: UITextField) -> Bool {NSLog("textFieldShouldReturn")textField.resignFirstResponder() //这个隐藏(放弃)虚拟键盘return true}}

效果图

转载自吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=98

转载于:https://blog.51cto.com/ophir/1629205

The Swift Code之UITextField的使用,及事件委托相关推荐

  1. anz的swift code_澳洲Commonwealth bank是不是只有一个SWIFT CODE?

    展开全部 不可能是一个的,因为swift code是用来定62616964757a686964616fe58685e5aeb931333433623131位具体是哪家银行哪家分行的.最保险的就是让亲戚 ...

  2. SWIFT Code 和 Routing Number 的关系

    SWIFT Code 该号相当于各个银行的身份证号 每个银行(包括每个分行.支行)都有一个代码,是由银行名称的英文缩写和总行所在地的英文缩写(也有用数字加字母表示某城市的)以及该分行所在地的代码(字母 ...

  3. Atitt 支付业务 银行国际代码(SWIFT Code银行国际代码(SWIFT Code)是由SWIFT协会提出并被ISO通过的银行识别代码,凡该协会的成员银行都有自己特定的SWIFT代码

    Atitt  支付业务  银行国际代码(SWIFT Code 银行国际代码(SWIFT Code)是由SWIFT协会提出并被ISO通过的银行识别代码,凡该协会的成员银行都有自己特定的SWIFT代码.在 ...

  4. IBAN 国际银行帐户号码 + Swift code + BIC代码

    IBAN号码 IBAN是指国际银行帐户号码(The International Bank Account Number),通常简称IBAN,是由欧洲银行标准委员会(European Committee ...

  5. 电汇Swift Code汇总

    什么是SWIFT CODE? SWIFT是"Society Worldwide Interbank Financial Telecommunication环球同业银行金融电讯协会" ...

  6. 银行swift code(银行国际代码)简介 (zt)

    1.SWIFT 环球银行电信协会SWIFT(Society for Worldwide Interbank Financial Telecommunication)是国际上最重要的金融通信网络之一.通 ...

  7. Swift Code Snippet

    1 swi_crecell func numberOfSections(in tableView: UITableView) -> Int {return 1;}func tableView(_ ...

  8. Category:UITextField添加点击事件

    为什么80%的码农都做不了架构师?>>>    #import <UIKit/UIKit.h>typedef void (^Handler)(UITextField *t ...

  9. [老码团队]Swift中的协议 - 用协议来实现委托模式

    委托是一种设计模式,它允许类或结构体将一些需要它们负责的功能交由(委托)给其他的类型的实例.在这种设计模式中,会涉及到几个角色: 公共接口(Interface):负责封装起需要被委托的功能 代理者(D ...

最新文章

  1. HtmlParser中的各种Filter(1)
  2. Dataset之LSUN:LSUN数据集的下载使用教程
  3. 常数乘以无穷大等于多少_电气知识知多少?第一弹
  4. nginx 配置详解
  5. Spring IOC实现原理
  6. 验证码---H_img.php
  7. 从苹果店员到机器学习工程师,高中学历澳洲小哥的自学路
  8. 开源社区_建立一个开源社区
  9. 复用Oracle数据文件,Oracle 多元复用数据库文件
  10. 40. 数组中只出现一次的数字(C++版本)
  11. ARM开发5.3.6 基础实训( 2 ) 单个 LED 显示单个按键的状态--LPC21XX
  12. 新手做短视频自媒体,再也不用担心找不到视频素材了,抓紧收藏
  13. otc焊接机器人编程模拟软件_OTC机器人编程
  14. 厉害了,10行代码实现抽奖助手自动参与抽奖
  15. QT(5.12)+Qgis(3.10) 距离、面积测量
  16. 2021-08-27-亚马逊 MWS 坑 handling time(handing time)
  17. 全国中学生计算机竞赛被取消,叫停4个月,中国计算机学会发布通知,恢复信奥联赛...
  18. 理解嵌入式系统中基本的语音算法
  19. KDD2020的一篇序列推荐的论文《Geography-Aware Sequential Location Recommendation》
  20. Kiner算法刷题记(二十一):字典树与双数组字典树(数据结构基础篇)

热门文章

  1. Data Lake Analytics + OSS数据文件格式处理大全
  2. [日常] Go语言圣经-Deferred函数
  3. FluxSink实例及解析
  4. Hadoop streaming 排序、分桶参数设置
  5. C#Windows服务程序安装常见问题解决方法
  6. loadrunner中创建唯一随机数
  7. Android拖动和缩放图片
  8. Windows 2008 r2域更名
  9. 20165313-张晨晖课设个人报告
  10. 甲骨文第四财季SAAS和PAAS收入增长66%