swift中UITextField的使用
来源:互联网 发布:淘宝助理批量修改 编辑:程序博客网 时间:2024/06/04 18:42
- 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.Left
- textfield.placeholder = "textfield的使用"
- textfield.secureTextEntry = false
- <pre name="code" class="objc">
- textfield.textColor = UIColor.greenColor()
- textfield.enabled = true
- textfield.userInteractionEnabled = true
-
- textfield.backgroundColor = UIColor.lightGrayColor()
- textfield.borderStyle = UITextBorderStyle.Line
-
-
-
- let leftview = UIImageView(image: UIImage(named: "normalImage"))
- leftview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
- textfield.leftViewMode = UITextFieldViewMode.Always
- textfield.leftView = leftview
- let rightview = UIImageView(image: UIImage(named: "hightImage"))
- rightview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
- textfield.rightViewMode = UITextFieldViewMode.Always
- textfield.rightView = rightview
-
- textfield.delegate = self
-
- class ViewController: UIViewController, UITextFieldDelegate {
-
- override func viewDidLoad() {
-
- }
- }
-
-
- func 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.WebSearch
- textfield.returnKeyType = UIReturnKeyType.Done
-
-
-
-
-
-
-
-
- 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
-
-
- func click(button:UIButton)
- {
- self.view.endEditing(true)
- }
-
-
- func leftClick(button:UIButton)
- {
- print("取消")
- }
-
- func rightClick(button:UIButton)
- {
- self.view.endEditing(true)
- print("确定")
- }
-
-
-
-
-
-
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFiledEditChanged:"), name: UITextFieldTextDidChangeNotification, object: textfield)
-
-
- 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))
- }
- }
- }
注意:不能同时设置clearButtonMode、rightViewMode/rightView属性,否则只有rightViewMode/rightView有效。
0 0