美文网首页
iOS-UITextField

iOS-UITextField

作者: Swift从入门到崩溃 | 来源:发表于2016-08-15 20:36 被阅读0次

TextField

textfield作为iOS中一个重要的内容我们在很多时候都能用的。只有我们要想使用输入框就会使用textfield。首先我们来看看如何创建一个textfield。在下面的列子中我们创建了两个textfield并给他添加了背景颜色,也让其可以在页面上显示出来。

    let textfeild = UITextField.init(frame: CGRectMake(50,50,200,50))
    textfeild.backgroundColor = UIColor.yellowColor()
    self.view.addSubview(textfeild)
    let textfeild1 = UITextField.init(frame: CGRectMake(50,110,200,50))
    textfeild1.backgroundColor = UIColor.yellowColor()
    self.view.addSubview(textfeild1)
    //commanf+shift+k弹出键盘

1.textfield专有属性

a.文字属性

1.text属性:主要用来获取textfeild中输入信息

    textfeild.text = "输入框"

2.placeHolder

    textfeild.placeholder = "请输入账号"

3.是否密文输入true->密文输入 false->明文输入

    textfeild.secureTextEntry = true

4.文字颜色

    textfeild.textColor = UIColor.redColor()

5.设置字体

    textfeild.font = UIFont.systemFontOfSize(20)

b.显示相关

1.设置边框样式

    textfeild.borderStyle = .RoundedRect
    //.None没有边框
    //.Line:直角边框
    //.Bezel:直角边框并且有凹陷的效果
    //.RoundedRect圆角边框

c.开始编辑的时候是否清空输入框

    //true->每次点击输入框都将原来的输入框中原来的文字清空
    //true->每次点击输入框都将原来的输入框中原来的文字不清空
    textfeild.clearsOnBeginEditing = true

清除按钮模式

    //Never清除按钮一直不显示
    //Always清除按钮一直显示(输入框有文字的时候)
    //.UnlessEditing.在非编辑状态的时候显示
    //.WhileEditing在编辑状态的时候显示
    textfeild.clearButtonMode = .UnlessEditing

4.设置左视图

    //使用图片作文左视图
    let leftimageView = UIImageView.init(image: UIImage.init(named: "1"))
    leftimageView.frame = CGRectMake(0, 0, 40, 40)//坐标是无效的,只有大小有效
    
    //使用文字作为左视图
    let textlabel = UILabel.init(frame: CGRectMake(0, 0, 50, 50))
    textlabel.text = "登陆"
    textfeild.leftView = leftimageView
    //如果想要左视图显示出来,必须设置做视图显示模式
    textfeild.leftViewMode = .Always

c.键盘相关

设置键盘上的回车按钮显示类型:这是一个枚举大家可以自行去看看其他的类型

    textfeild.returnKeyType = .Google

设置键盘类型

    //不同的输入框设置不同的样式
    textfeild.keyboardType = .Twitter

设置输入界面(自定义键盘)

    let inputView = UIView.init(frame: CGRectMake(0, 0, 0, 256))
    inputView.backgroundColor = UIColor.greenColor()
//        textfeild.inputView = inputView

设置二级键盘

    let accessoryView = UIView.init(frame: CGRectMake(0, 0, 0, 56))
    accessoryView.backgroundColor = UIColor.redColor()
    textfeild.inputAccessoryView = accessoryView

相关文章

网友评论

      本文标题:iOS-UITextField

      本文链接:https://www.haomeiwen.com/subject/mnnusttx.html