swift中UITextField的使用

前端之家收集整理的这篇文章主要介绍了swift中UITextField的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. let textfield = UITextField(frame: CGRectMake(10.0,10.0,200.0,40.0))
  2. self.view.addSubview(textfield)
  1. // 字体属性设置
  2. textfield.textColor = UIColor.blackColor()
  3. textfield.font = UIFont(name: "GillSans",size: 15.0)
  4. textfield.textAlignment = NSTextAlignment.Left
  5. textfield.placeholder = "textfield的使用"
  6. textfield.secureTextEntry = false
  1. <pre name="code" class="objc">// 光标颜色
  2. textfield.textColor = UIColor.greenColor()
  1.  
  1. textfield.enabled = true
  2. textfield.userInteractionEnabled = true
  1. // 样式背景属性
  2. textfield.backgroundColor = UIColor.lightGrayColor()
  3. textfield.borderStyle = UITextBorderStyle.Line
  4. // let image = UIImage(named: "normalImage")
  5. // textfield.background = image
  1. // 图标
  2. let leftview = UIImageView(image: UIImage(named: "normalImage"))
  3. leftview.frame = CGRectMake(0.0,0.0,30.0,30.0)
  4. textfield.leftviewmode = UITextFieldviewmode.Always
  5. textfield.leftView = leftview
  6. let rightview = UIImageView(image: UIImage(named: "hightImage"))
  7. rightview.frame = CGRectMake(0.0,30.0)
  8. textfield.rightviewmode = UITextFieldviewmode.Always
  9. textfield.rightView = rightview
  1. // 代理,注意添加代理协议,及实现代理方法
  2. textfield.delegate = self
  1. // 添加协议
  2. class ViewController: UIViewController,UITextFieldDelegate {
  3.  
  4. override func viewDidLoad() {
  5. }
  6. }
  1. // 代理方法
  2. // MARK: - UITextFieldDelegate
  3. func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
  4. print("1 textFieldShouldBeginEditing")
  5. return true
  6. }
  7. func textFieldDidBeginEditing(textField: UITextField) {
  8. print("2 textFieldDidBeginEditing")
  9. }
  10. func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool {
  11. print("3 textField")
  12. return true
  13. }
  14.  
  15. func textFieldShouldEndEditing(textField: UITextField) -> Bool {
  16. print("4 textFieldShouldEndEditing")
  17. print("text:\(textField.text) length = \(textField.text?.characters.count)")
  18. return true
  19. }
  20. func textFieldDidEndEditing(textField: UITextField) {
  21. print("5 textFieldDidEndEditing")
  22. }
  23. func textFieldShouldClear(textField: UITextField) -> Bool {
  24. print("6 textFieldShouldClear")
  25. return true
  26. }
  27. func textFieldShouldReturn(textField: UITextField) -> Bool {
  28. // 结束编辑
  29. textField.resignFirstResponder()
  30. print("7 textFieldShouldReturn")
  31. return true
  32. }
  1. // 编辑属性设置
  2. textfield.clearButtonMode = UITextFieldviewmode.WhileEditing
  1. // 输入设置
  2. textfield.keyboardType = UIKeyboardType.WebSearch
  3. textfield.returnKeyType = UIReturnKeyType.Done
  4. // 自定义输入源控件
  5. // let inputview = UIButton(frame: CGRectMake(0.0,CGRectGetWidth(self.view.bounds),100.0))
  6. // inputview.setImage(UIImage(named: "normalImage"),forState: UIControlState.Normal)
  7. // inputview.backgroundColor = UIColor.lightGrayColor()
  8. // inputview.addTarget(self,action: Selector("click:"),forControlEvents: UIControlEvents.TouchUpInside)
  9. // textfield.inputView = inputview
  10. // 自定义输入源控件副视图
  11. let accessoryview = UIView(frame: CGRectMake(0.0,40.0))
  12. accessoryview.backgroundColor = UIColor.greenColor()
  13. let accessoryLeft = UIButton(frame: CGRectMake(10.0,60.0,20.0))
  14. accessoryview.addSubview(accessoryLeft)
  15. accessoryLeft.setTitle("取消",forState: UIControlState.Normal)
  16. accessoryLeft.backgroundColor = UIColor.orangeColor()
  17. accessoryLeft.addTarget(self,action: Selector("leftClick:"),forControlEvents: UIControlEvents.TouchUpInside)
  18. let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0),20.0))
  19. accessoryview.addSubview(accessoryRight)
  20. accessoryRight.setTitle("确定",forState: UIControlState.Normal)
  21. accessoryRight.backgroundColor = UIColor.orangeColor()
  22. accessoryRight.addTarget(self,action: Selector("rightClick:"),forControlEvents: UIControlEvents.TouchUpInside)
  23. textfield.inputAccessoryView = accessoryview
  1. // 自定义输入源控件时响应事件
  2. // MARK: - click
  3. func click(button:UIButton)
  4. {
  5. self.view.endEditing(true)
  6. }
  7. //MARK: - left/right click
  8. func leftClick(button:UIButton)
  9. {
  10. print("取消")
  11. }
  12. func rightClick(button:UIButton)
  13. {
  14. self.view.endEditing(true)
  15. print("确定")
  16. }
  1. // 其他
  2. // 第一响应,即进入编辑状态
  3. // textfield.becomeFirstResponder()
  4. // 结束响应,即结束编辑
  5. // textfield.resignFirstResponder()
  6. // 发通知-输入改变
  7. NSNotificationCenter.defaultCenter().addObserver(self,selector: Selector("textFiledEditChanged:"),name: UITextFieldTextDidChangeNotification,object: textfield)
  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有效。


猜你在找的Swift相关文章