The Swift Code之UITextField的使用,及事件委托

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(image: 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 = 2
 uitf3.layer.borderWidth = 1
 uitf3.layer.borderColor = UIColor.greenColor().CGColor
        
 //uitf3.borderStyle = UITextBorderStyle.RoundedRect
 var img1:UIImageView = UIImageView(image: UIImage(named: "buy.png")!)
 img1.frame = CGRect(x: 5, y: 5, width: 40, height: 30)
 img1.contentMode = UIViewContentMode.ScaleAspectFit
 uitf3.rightView = img1
 uitf3.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 UIKit

class 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.
        
        //构建基本的UITextField
        var 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(image: 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))
        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 = 2
        uitf3.layer.borderWidth = 1
        uitf3.layer.borderColor = UIColor.greenColor().CGColor
        
        //uitf3.borderStyle = UITextBorderStyle.RoundedRect
        var img1:UIImageView = UIImageView(image: UIImage(named: "buy.png")!)
        img1.frame = CGRect(x: 5, y: 5, width: 40, height: 30)
        img1.contentMode = UIViewContentMode.ScaleAspectFit
        uitf3.rightView = img1
        uitf3.rightViewMode = UITextFieldViewMode.Always
        self.view.addSubview(uitf3)
        
        //委托事件
        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
        self.view.addSubview(uitf4)
    }
    
    
    
    // 当鼠标进入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
        
    }
    
    
    
}

效果图

865E70FE-173F-4942-81B7-1DF68487C16C.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值