Swift基础(十四)UITextField

来源:互联网 发布:typescript 调用js 编辑:程序博客网 时间:2024/05/18 01:23
class RootViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.whiteColor()
        // 初始化UITextField控件
        var textField = UITextField(frame: CGRectMake(10, 100, 300, 40))
        // 为方便看到效果,设置一下背景色
        textField.backgroundColor = UIColor.redColor()
        // 添加到视图上
        self.view.addSubview(textField)
        
        // 文本框常用属性
        // 设置显示文本
        textField.text = "akjdhfajalsdjfmqowe;ldjfm";
        // 获取文本框内容
        var textString = textField.text
        // 设置背景色
        textField.backgroundColor = UIColor.yellowColor();
        // 设置背景图
        textField.background = UIImage(named: "textImage")
        // 设置富文本
        var attributeStr: NSMutableAttributedString = NSMutableAttributedString(string: "adfqwedfa")
        // 文本0开始5个字符字体HeveticaNeue,16号
        attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica Neue", size: 16)!, range: NSMakeRange(0, 4))
        // 设置字体颜色
        attributeStr.addAttribute(NSFontAttributeName, value: UIColor.redColor(), range: NSMakeRange(4, 2))
        // 设置文字背景颜色
        attributeStr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(0, 4))
        
//        textField.attributedText = attributeStr
        
        // 设置文本字体
        textField.font = UIFont.systemFontOfSize(18) // 使用系统默认字体,指定18号字号
        textField.font = UIFont(name: "Helvetica", size: 18); // 指定字体,指定字号
        // 设置文本颜色
        textField.textColor = UIColor.magentaColor()
        // 设置文本文字对齐方式,对齐属性有一下几种,默认是左对齐
        // Left: 左对齐
        // Center: 居中
        // Right: 右对齐
        // Justified: 最后一行自然对齐
        // Natural: 默认对齐脚本
        textField.textAlignment = NSTextAlignment.Right;
        
        // 用contentVerticalAlignment属性设置垂直对齐方式
        // 向上对齐
        textField.contentVerticalAlignment = UIControlContentVerticalAlignment.Top
        // UIControlContentVerticalAlignment单词太长,可以简写成:
        // 向下对齐
        textField.contentVerticalAlignment = .Bottom
        // 垂直居中对齐
        textField.contentVerticalAlignment = .Center
        
        // 设置文本框样式,可分别看看效果
        // None
        // Line
        // Bezel
        // RoundedRect
        textField.borderStyle = .RoundedRect
        // 富文本占位符
        textField.attributedPlaceholder = attributeStr
        // 设置占位字符
        textField.placeholder = "请输入学校名称"
        // 最小字体
        textField.minimumFontSize = 10.0
        // 文本内容自动适应标签大小,默认是false
        textField.adjustsFontSizeToFitWidth = true;
        
        // 设置删除效果(文本框输入内容时,显示清除按钮-- × )
        // Never: 不显示清除按钮
        // WhileEditing: 编辑时才出现清除按钮
        // UnlessEditing: 完成编辑后才出现清除按钮
        // Always: 一直都显示清除按钮
        textField.clearButtonMode = .WhileEditing
        
        // 设置文本框编辑状态,默认true可以编辑
        textField.editing
        // 文本框是否可用,默认是true
        textField.enabled = true;
        // 开始输入时,清空文本框原有内容
        textField.clearsOnBeginEditing = true;
        
        // 创建左视图
        var leftView: UIView = UIView(frame: CGRectMake(0, 0, 40, 40))
        leftView.backgroundColor = UIColor.blackColor()
        textField.leftView = leftView;
        
        // 设置左视图显示类型
        // Never
        // WhileEditing
        // UnlessEditing
        // Always
        textField.leftViewMode = .WhileEditing // 开始编辑时显示
        
        // 创建右视图
        var rightView: UIView = UIView(frame: CGRectMake(260, 0, 40, 40));
        rightView.backgroundColor = UIColor.blackColor()
        textField.rightView = rightView;
        // 设置左视图显示类型
        // Never
        // WhileEditing
        // UnlessEditing
        // Always
        textField.rightViewMode = .UnlessEditing // 开始编辑时显示
        // 成为第一响应者,光标定位到该文本框
        textField.becomeFirstResponder()
        // 取消光标,释放键盘
        textField.resignFirstResponder()
        // 首字母是否大写
        /*
         enum UITextAutocapitalizationType : Int {
            case None 不自动大写
            case words 单词首字母大写
            case Sentences 句子的首字母大写
            case AllCheracters 所有字母都大写
        }
         */
        textField.autocapitalizationType = .Words
       
        // 文本框代理
        // 指定代理人
        textField.delegate = self
        // 可以实现的协议方法
        /*
        @available(iOS 2.0, *)
        optional public func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
        @available(iOS 2.0, *)
        optional public func textFieldDidBeginEditing(textField: UITextField) // became first responder
        @available(iOS 2.0, *)
        optional public func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
        @available(iOS 2.0, *)
        optional public func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
        
        @available(iOS 2.0, *)
        optional public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text
        
        @available(iOS 2.0, *)
        optional public func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications)
        @available(iOS 2.0, *)
        optional public func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
        */
        
        // 文本框状态监听
        // UITextField具有3个通知类型:
        /*
        public let UITextFieldTextDidBeginEditingNotification: String
        public let UITextFieldTextDidEndEditingNotification: String
        public let UITextFieldTextDidChangeNotification: String
 */
        // 创建3个监听
        // 文本框开始编辑是,触发
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textDidBeginEditing"), name: UITextFieldTextDidBeginEditingNotification, object: nil)
        // 文本框编辑结束时,触发
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textDidEndEditing"), name: UITextFieldTextDidEndEditingNotification, object: nil)
        // 文本框内容改变时,触发
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textDidChange"), name: UITextFieldTextDidChangeNotification, object: nil)
        
        // 键盘知识
        // 键盘外观有3种
        /*
         enum UIKeyboardAppearance : Int {
            case Default 默认外观
            case Dark   深灰石墨色
            case Light  浅灰色
         }
         */
        textField.keyboardAppearance = .Default
        // 设置键盘完成的按钮样式
        /*
        enum UIReturnKeyType : Int {
            case Default 默认灰色按钮,标有Return
            case Go     标有Go的蓝色按钮
            case Google 标有Google的蓝色按钮,用于搜索
            case Join   标有Join的蓝色按钮
            case Next   标有Next的蓝色按钮
            case Route  标有Route的蓝色按钮
            case Search 标有Search的蓝色按钮
            case Send   标有Send的蓝色按钮
            case Yahoo  标有Yahoo的蓝色按钮
            case Done   标有Done的蓝色按钮
            case EmergencyCall 紧急呼叫按钮
         }
         */
        textField.returnKeyType = .Done
    }

    func textDidBeginEditing() {
        print("123")
    }
    
    func textDidEndEditing() {
        print("456")
    }
    
    func textDidChange() {
        print("789")
    }
}

0 0
原创粉丝点击