您的位置:首页 > 移动开发 > Swift

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

2015-04-06 00:00 323 查看


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

}

}

效果图



转载自吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=98
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息