首页 > 编程语言 > 详细

swift中的UITextField

时间:2015-12-09 17:00:31      阅读:189      评论:0      收藏:0      [点我收藏+]

  
        let userTF = UITextField(frame: CGRectMake(0,120,300,40))
        userTF.backgroundColor = UIColor.redColor()
        // 输入框样式
        userTF.borderStyle = .RoundedRect
        // 提示文字
        userTF.placeholder = "请输入用户名"
        
        userTF.text = "我到底哦是的分手都放假 "
        
        // 文字水平对齐
        userTF.textAlignment = .Center
        // 内容水平对齐
        userTF.contentHorizontalAlignment = .Right
        
        // 内容垂直对齐
        userTF.contentVerticalAlignment = .Top
        // 密文输入
        userTF.secureTextEntry = true
        // 设置第一响应者
        userTF.becomeFirstResponder()
        
        // 放弃第一响应者(结束编辑)
        userTF.resignFirstResponder()
        userTF.endEditing(true)
        
        // 设置键盘类型
        userTF.keyboardType = .NamePhonePad
        
        // 设置键盘 return 的类型
        userTF.returnKeyType = .Go
        
        
        // 给输入框绑定事件
        userTF.addTarget(self, action: "changed:", forControlEvents: .EditingChanged)
        
        // 设置一键删除,并设置状态
        userTF.clearButtonMode = .Always
        self.view.addSubview(userTF)
        userTF.delegate = self
        
        
        
        
        
    }
    /**
     
     
     - parameter textField: 表示当前的输入对象
     - parameter range:     表示当前的输入框中将要被修改的字符串的位置和长度
     - parameter string:    表示即将替换的字符串
     
     - returns:
     */
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        // 返回值表示即将输入的内容是否可以输入到输入框内, 如果返回false 则输入对应的字符将不会出现
        
        // 可以通过这个方法对输入的内容进行筛选
        
        
        return true
        
        
    }
    
    
    /**
     在输入框已经成为第一响应者时,调用这个方法
     
     - parameter textField:
     */
    func textFieldDidBeginEditing(textField: UITextField) {
        
        
    }
    
    /**
     在输入框即将成为第一响应者时,调用该方法
     
     - parameter textField:
     
     - returns:
     */
    
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        
        //返回值表示当前的输入框能否成为第一响应者,如果返回false ,则该输入框将不能输入内容
        
        return true
    }
    
    
    /**
     在输入框已经失去第一响应者时,调用此方法
     
     - parameter textField:
     */
    func textFieldDidEndEditing(textField: UITextField) {
        
    }
    
    
    /**
     在输入框即将失去第一响应者时,调用此方法
     
     - parameter textField:
     
     - returns:
     */
    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        
        // 返回值表示当前的输入框能否放弃第一响应者,可以在这个方法里强制用户停留在当前输入框
        
        return true
    }
    
    /**
     在输入框右侧的清除按钮被点击的时候,调用此方法
     
     - parameter textField:
     
     - returns:
     */
    func textFieldShouldClear(textField: UITextField) -> Bool {
        
        
        //返回值表示可删除输入框中的内容
        return true
    }
    
    /**
     点击了键盘右下角的return键时,调用此方法
     
     - parameter textField:
     
     - returns:
     */
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        
        // 返回值表示点击 return 按钮是否有效
        return true
    }
    
    

 

swift中的UITextField

原文:http://www.cnblogs.com/Iceing/p/5032906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!