swift中UITextField的使用

来源:互联网 发布:淘宝助理批量修改 编辑:程序博客网 时间:2024/06/04 18:42
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. let textfield = UITextField(frame: CGRectMake(10.010.0200.0,40.0))  
  2. self.view.addSubview(textfield)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 字体属性设置  
  2. textfield.textColor = UIColor.blackColor()  
  3. textfield.font = UIFont(name: "GillSans", size15.0)  
  4. textfield.textAlignment = NSTextAlignment.Left  
  5. textfield.placeholder = "textfield的使用"  
  6. textfield.secureTextEntry = false  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="objc">// 光标颜色  
  2. textfield.textColor = UIColor.greenColor()  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. textfield.enabled = true  
  2. textfield.userInteractionEnabled = true  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 样式背景属性  
  2. textfield.backgroundColor = UIColor.lightGrayColor()  
  3. textfield.borderStyle = UITextBorderStyle.Line  
  4. // let image = UIImage(named: "normalImage")  
  5. // textfield.background = image  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 图标  
  2. let leftview = UIImageView(image: UIImage(named: "normalImage"))  
  3. leftview.frame = CGRectMake(0.00.030.030.0)  
  4. textfield.leftViewMode = UITextFieldViewMode.Always  
  5. textfield.leftView = leftview  
  6. let rightview = UIImageView(image: UIImage(named: "hightImage"))  
  7. rightview.frame = CGRectMake(0.00.030.030.0)  
  8. textfield.rightViewMode = UITextFieldViewMode.Always  
  9. textfield.rightView = rightview  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 代理,注意添加代理协议,及实现代理方法  
  2. textfield.delegate = self  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 添加协议  
  2. class ViewController: UIViewController, UITextFieldDelegate {  
  3.   
  4.     override func viewDidLoad() {  
  5.    
  6.    }  
  7. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 代理方法  
  2. // MARK: - UITextFieldDelegate  
  3. func textFieldShouldBeginEditing(textField: UITextField) -> Bool {  
  4.           
  5.         print("1 textFieldShouldBeginEditing")  
  6.           
  7.         return true  
  8. }  
  9.       
  10. func textFieldDidBeginEditing(textField: UITextField) {  
  11.         print("2 textFieldDidBeginEditing")  
  12. }  
  13.       
  14. func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {  
  15.         print("3 textField")  
  16.           
  17.         return true  
  18. }  
  19.   
  20. func textFieldShouldEndEditing(textField: UITextField) -> Bool {  
  21.         print("4 textFieldShouldEndEditing")  
  22.         print("text:\(textField.text) length = \(textField.text?.characters.count)")  
  23.         return true  
  24. }  
  25.       
  26. func textFieldDidEndEditing(textField: UITextField) {  
  27.         print("5 textFieldDidEndEditing")  
  28. }  
  29.       
  30. func textFieldShouldClear(textField: UITextField) -> Bool {  
  31.           
  32.         print("6 textFieldShouldClear")  
  33.           
  34.         return true  
  35. }  
  36.       
  37. func textFieldShouldReturn(textField: UITextField) -> Bool {  
  38.           
  39.         // 结束编辑  
  40.         textField.resignFirstResponder()  
  41.           
  42.         print("7 textFieldShouldReturn")  
  43.           
  44.         return true  
  45. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 编辑属性设置  
  2. textfield.clearButtonMode = UITextFieldViewMode.WhileEditing  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 输入设置  
  2. textfield.keyboardType = UIKeyboardType.WebSearch  
  3. textfield.returnKeyType = UIReturnKeyType.Done  
  4.           
  5. // 自定义输入源控件  
  6. // let inputview = UIButton(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 100.0))  
  7. // inputview.setImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)  
  8. // inputview.backgroundColor = UIColor.lightGrayColor()  
  9. // inputview.addTarget(self, action: Selector("click:"), forControlEvents: UIControlEvents.TouchUpInside)  
  10. // textfield.inputView = inputview  
  11. // 自定义输入源控件副视图  
  12. let accessoryview = UIView(frame: CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 40.0))  
  13. accessoryview.backgroundColor = UIColor.greenColor()  
  14. let accessoryLeft = UIButton(frame: CGRectMake(10.010.060.020.0))  
  15. accessoryview.addSubview(accessoryLeft)  
  16. accessoryLeft.setTitle("取消", forState: UIControlState.Normal)  
  17. accessoryLeft.backgroundColor = UIColor.orangeColor()  
  18. accessoryLeft.addTarget(self, action: Selector("leftClick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  19. let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0), 10.060.020.0))  
  20. accessoryview.addSubview(accessoryRight)  
  21. accessoryRight.setTitle("确定", forState: UIControlState.Normal)  
  22. accessoryRight.backgroundColor = UIColor.orangeColor()  
  23. accessoryRight.addTarget(self, action: Selector("rightClick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  24. textfield.inputAccessoryView = accessoryview  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 自定义输入源控件时响应事件  
  2. // MARK: - click  
  3. func click(button:UIButton)  
  4. {  
  5.         self.view.endEditing(true)  
  6. }  
  7.       
  8. //MARK: - left/right click  
  9. func leftClick(button:UIButton)  
  10. {  
  11.         print("取消")  
  12. }  
  13.       
  14. func rightClick(button:UIButton)  
  15. {  
  16.         self.view.endEditing(true)  
  17.         print("确定")  
  18. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 其他  
  2. // 第一响应,即进入编辑状态  
  3. // textfield.becomeFirstResponder()  
  4. // 结束响应,即结束编辑  
  5. // textfield.resignFirstResponder()  
  6. // 发通知-输入改变  
  7. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFiledEditChanged:"), name: UITextFieldTextDidChangeNotification, object: textfield)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 通知响应方法-限制输入长度  
  2. // MARK: - 通知响应方法  
  3. func textFiledEditChanged(notification:NSNotification)  
  4. {  
  5.         let textField:UITextField! = notification.object as! UITextField  
  6.         if (textField != nil)  
  7.         {  
  8.             let text:String! = textField.text  
  9.             let length = text.characters.count  
  10.             if (length > 20)  
  11.             {  
  12.                 textField.text = text.substringToIndex(text.startIndex.advancedBy(20))  
  13.             }  
  14.         }  
  15. }  


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


 


0 0
原创粉丝点击