• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

SwiftUITextField各种属性的设置

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//MARK: 文本输入框

    func _initTextField() {

        //如果需要在模拟器中调用电脑的键盘 快捷键:command + shift + k

        let textField = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 30))

        textField.backgroundColor = UIColor.redColor()

        textField.tag = 100

        textField.delegate = self

        self.view.addSubview(textField)

        

        //设置输入框的边框样式 默认无边框

        textField.borderStyle = UITextBorderStyle.RoundedRect

        

        //设置输入文本的字体

        textField.font = UIFont.boldSystemFontOfSize(10)

        

        //设置文字的颜色

        textField.textColor = UIColor.greenColor()

        

        //设置或获取输入框中的内容

//        textField.text = "hehe"

        

        //设置对齐方式

        textField.textAlignment = NSTextAlignment.Center

        

        //设置首字母是否自动大写

        textField.autocapitalizationType = UITextAutocapitalizationType.Words

        //设置自动单词提示

        textField.autocorrectionType = UITextAutocorrectionType.Yes

        

        //输入框为空时的提示文本

        textField.placeholder = "请输入登陆邮箱"

        

        //修改键盘上的return按钮上的标题

        textField.returnKeyType = UIReturnKeyType.Next

        

        //输入框是否安全输入

        textField.secureTextEntry = true

        

        //数字键盘

//        textField.keyboardType = UIKeyboardType.NumberPad

        

        //开启清楚按钮

        textField.clearButtonMode = UITextFieldViewMode.WhileEditing

        

//        //成为第一响应者,弹出键盘

        textField.becomeFirstResponder()

//

//        //失去第一响应者,收起键盘

//        textField.resignFirstResponder()

        

        let btn = UIButton(type: UIButtonType.ContactAdd)

        btn.frame = CGRect(x: 0, y: 100, width: 50, height: 50)

        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(btn)

        

        

    }

    

    func btnClick(btn: UIButton) {

        //失去第一响应者

        let textField = self.view.viewWithTag(100) as! UITextField

        textField.resignFirstResponder()

    }

    

    //MARK: UITextFieldDelegate

    //将要开始编辑

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

        print("将要开始编辑")

        //返回true表示继续编辑,返回false是阻止编辑

        return true

    }

 

    //开始编辑

    func textFieldDidBeginEditing(textField: UITextField) {

        print("开始编辑")

    }

    

    //结束编辑时调用的协议方法

    func textFieldDidEndEditing(textField: UITextField) {

        print("已经结束")

    }

    

    //输入框的内容被修改时调用的协议方法

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        print("range is \(range), string is \(string)")

        return true

    }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[swift、oc]浅聊nil、Nil、NULL、NSNull的区别发布时间:2022-07-13
下一篇:
Swift中的单例的实现方式发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap