swift中UITextField的使用

来源:互联网 发布:java swing怎么用 编辑:程序博客网 时间:2024/06/05 10:39
let textfield = UITextField(frame: CGRectMake(10.0, 10.0, 200.0,40.0))self.view.addSubview(textfield)
// 字体属性设置textfield.textColor = UIColor.blackColor()textfield.font = UIFont(name: "GillSans", size: 15.0)textfield.textAlignment = NSTextAlignment.Lefttextfield.placeholder = "textfield的使用"textfield.secureTextEntry = false
<pre name="code" class="objc">// 光标颜色textfield.textColor = UIColor.greenColor()
textfield.enabled = truetextfield.userInteractionEnabled = true
// 样式背景属性textfield.backgroundColor = UIColor.lightGrayColor()textfield.borderStyle = UITextBorderStyle.Line// let image = UIImage(named: "normalImage")// textfield.background = image
// 图标let leftview = UIImageView(image: UIImage(named: "normalImage"))leftview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)textfield.leftViewMode = UITextFieldViewMode.Alwaystextfield.leftView = leftviewlet rightview = UIImageView(image: UIImage(named: "hightImage"))rightview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)textfield.rightViewMode = UITextFieldViewMode.Alwaystextfield.rightView = rightview
// 代理,注意添加代理协议,及实现代理方法textfield.delegate = self
// 添加协议class ViewController: UIViewController, UITextFieldDelegate {    override func viewDidLoad() {    }}
// 代理方法// MARK: - UITextFieldDelegatefunc textFieldShouldBeginEditing(textField: UITextField) -> Bool {                print("1 textFieldShouldBeginEditing")                return true}    func textFieldDidBeginEditing(textField: UITextField) {        print("2 textFieldDidBeginEditing")}    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {        print("3 textField")                return true}func textFieldShouldEndEditing(textField: UITextField) -> Bool {        print("4 textFieldShouldEndEditing")        print("text:\(textField.text) length = \(textField.text?.characters.count)")        return true}    func textFieldDidEndEditing(textField: UITextField) {        print("5 textFieldDidEndEditing")}    func textFieldShouldClear(textField: UITextField) -> Bool {                print("6 textFieldShouldClear")                return true}    func textFieldShouldReturn(textField: UITextField) -> Bool {                // 结束编辑        textField.resignFirstResponder()                print("7 textFieldShouldReturn")                return true}
// 编辑属性设置textfield.clearButtonMode = UITextFieldViewMode.WhileEditing
// 输入设置textfield.keyboardType = UIKeyboardType.WebSearchtextfield.returnKeyType = UIReturnKeyType.Done        // 自定义输入源控件// let inputview = UIButton(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 100.0))// inputview.setImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)// inputview.backgroundColor = UIColor.lightGrayColor()// inputview.addTarget(self, action: Selector("click:"), forControlEvents: UIControlEvents.TouchUpInside)// textfield.inputView = inputview// 自定义输入源控件副视图let accessoryview = UIView(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0))accessoryview.backgroundColor = UIColor.greenColor()let accessoryLeft = UIButton(frame: CGRectMake(10.0, 10.0, 60.0, 20.0))accessoryview.addSubview(accessoryLeft)accessoryLeft.setTitle("取消", forState: UIControlState.Normal)accessoryLeft.backgroundColor = UIColor.orangeColor()accessoryLeft.addTarget(self, action: Selector("leftClick:"), forControlEvents: UIControlEvents.TouchUpInside)let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0), 10.0, 60.0, 20.0))accessoryview.addSubview(accessoryRight)accessoryRight.setTitle("确定", forState: UIControlState.Normal)accessoryRight.backgroundColor = UIColor.orangeColor()accessoryRight.addTarget(self, action: Selector("rightClick:"), forControlEvents: UIControlEvents.TouchUpInside)textfield.inputAccessoryView = accessoryview
// 自定义输入源控件时响应事件// MARK: - clickfunc click(button:UIButton){        self.view.endEditing(true)}    //MARK: - left/right clickfunc leftClick(button:UIButton){        print("取消")}    func rightClick(button:UIButton){        self.view.endEditing(true)        print("确定")}
// 其他// 第一响应,即进入编辑状态// textfield.becomeFirstResponder()// 结束响应,即结束编辑// textfield.resignFirstResponder()// 发通知-输入改变NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFiledEditChanged:"), name: UITextFieldTextDidChangeNotification, object: textfield)
// 通知响应方法-限制输入长度// MARK: - 通知响应方法func textFiledEditChanged(notification:NSNotification){        let textField:UITextField! = notification.object as! UITextField        if (textField != nil)        {            let text:String! = textField.text            let length = text.characters.count            if (length > 20)            {                textField.text = text.substringToIndex(text.startIndex.advancedBy(20))            }        }}


注意:不能同时设置clearButtonModerightViewMode/rightView属性,否则只有rightViewMode/rightView有效。


 








源码:https://github.com/potato512/SYSwiftLearning


0 0
原创粉丝点击