天天看点

UITTextField、UIStepper步进控件、UISlider 滑块控件、UISwitch 开关控件

1.UITextField控件  

文本框控件, 主要用于用户输入

设置:

1. 如何输入密码 : 勾选Secure Text Entry

2. 设置软键盘类型: keyboard type:

3. 设置软键盘外观: appearance

4. 设置软键盘的返回按钮显示: returnKey

5. 设置文本框的初始文字:placeholder

6. 设置文本框的边框风格: border style

7. 设置文本框什么情况出现清空按钮: Clear Button

8. 当文本框开始输入时,是否先请空原先的内容:

勾选:Clear when editing begins

  收回键盘:

两种办法:

  1.让正在输入的文本框对象放弃第一响应者身份

  2.让当前视图结束编辑:  [self.view endEditing:YES];

什么时候收回键盘:

  1> 当点击屏幕的空白处

- (void)touchesBegan:

    (NSSet *)touches withEvent:

    (UIEvent *)event

  2> 当点击了一些按钮时 ,在按钮执行的方法中收回键盘 

  3> 当点击了软键盘上的returnKey按钮时,给文本框对象添加事件响应方法

第一响应者身份:

  当用户点了一个文本框后,这个文本框就会成为第一响应者。此时键盘的输入会送到这个文本框中,光标也会在这个文本框中闪烁。

  可以用代码的方式让一个文本框成为第一响应者或放弃第一响应者。

成为第一响应者:  becomeFirstResponder

放弃第一响应者:  resignFirstResponder

//创建一个文本输入框

        let textField = UITextField()

        //设置坐标

        textField.frame = CGRectMake(40, 100, self.view.frame.size.width-80, 100)

        //设置文本框外观样式

        textField.borderStyle = UITextBorderStyle.None//没有样式

        textField.borderStyle = UITextBorderStyle.Line//文本输入框的边框是一条黑线

        textField.borderStyle = UITextBorderStyle.Bezel//文本输入框的边框是一条棕色

        textField.borderStyle = UITextBorderStyle.RoundedRect//文本输入框的边框是圆角矩形

        //设置文本框背景颜色

        textField.backgroundColor = UIColor.greenColor()

        //设置提示文字

        textField.placeholder = "请输入帐号"

        //设置字体

        textField.font = UIFont.systemFontOfSize(15)

        //设置字体的颜色

        textField.textColor = UIColor.redColor()

        //文字内容对其方式

        textField.textAlignment = NSTextAlignment.Center//居中对齐

        textField.textAlignment = NSTextAlignment.Right//右对齐

        textField.textAlignment = NSTextAlignment.Left//左对齐

        //内容水平对齐方式

        textField.contentHorizontalAlignment =UIControlContentHorizontalAlignment.Center//居中

        textField.contentHorizontalAlignment =UIControlContentHorizontalAlignment.Left//左对齐

        textField.contentHorizontalAlignment =UIControlContentHorizontalAlignment.Right//右对齐

        textField.contentHorizontalAlignment =UIControlContentHorizontalAlignment.Fill//充满

        //内容垂直对齐方式

        textField.contentVerticalAlignment =UIControlContentVerticalAlignment.Center//居中

        textField.contentVerticalAlignment =UIControlContentVerticalAlignment.Top//顶部对齐

        textField.contentVerticalAlignment =UIControlContentVerticalAlignment.Bottom//底部对齐

        textField.contentVerticalAlignment =UIControlContentVerticalAlignment.Fill//充满

        //开始开始缩小,当文字小到一定地步的时候会不再缩小

        textField.adjustsFontSizeToFitWidth = true

        //设置最小字号

        textField.minimumFontSize = 10

        //设置首字母是否大小写

        textField.autocapitalizationType =UITextAutocapitalizationType.None//从不大写

        textField.autocapitalizationType =UITextAutocapitalizationType.Words//单词首字母大写(以空格为区分)

        textField.autocapitalizationType =UITextAutocapitalizationType.Sentences//回车时第一个字符大写

        textField.autocapitalizationType =UITextAutocapitalizationType.AllCharacters//回车时当前的

        //关闭错误纠正(会记录之前输入的内容)

        textField.autocorrectionType = UITextAutocorrectionType.No

        //设置对文本加密

        textField.secureTextEntry = true

        //设置键盘样式

        textField.keyboardType = UIKeyboardType.URL

        textField.keyboardType = UIKeyboardType.Twitter

        textField.keyboardType = UIKeyboardType.Alphabet

        textField.keyboardType = UIKeyboardType.PhonePad

        textField.keyboardType = UIKeyboardType.NumberPad

        textField.keyboardType = UIKeyboardType.WebSearch

        textField.keyboardType = UIKeyboardType.DecimalPad

        textField.keyboardType = UIKeyboardType.ASCIICapable

        textField.keyboardType = UIKeyboardType.EmailAddress

        textField.keyboardType = UIKeyboardType.NamePhonePad

        textField.keyboardType =UIKeyboardType.NumbersAndPunctuation

        textField.keyboardType = UIKeyboardType.Default

        //设置键盘风格

        textField.keyboardAppearance =UIKeyboardAppearance.Alert//提示

        textField.keyboardAppearance =UIKeyboardAppearance.Dark;//黑色的

        textField.keyboardAppearance =UIKeyboardAppearance.Light//亮色的

        textField.keyboardAppearance =UIKeyboardAppearance.Default//系统默认

        //设置键盘的return键样式

        textField.returnKeyType = UIReturnKeyType.Go

        textField.returnKeyType = UIReturnKeyType.Google

        textField.returnKeyType = UIReturnKeyType.Join

        textField.returnKeyType = UIReturnKeyType.Next

        textField.returnKeyType = UIReturnKeyType.Route

        textField.returnKeyType = UIReturnKeyType.Search

        textField.returnKeyType = UIReturnKeyType.Send

        textField.returnKeyType = UIReturnKeyType.Yahoo

        textField.returnKeyType = UIReturnKeyType.Done

        textField.returnKeyType = UIReturnKeyType.EmergencyCall

        textField.returnKeyType = UIReturnKeyType.Default

        //设置清除按钮模式

        textField.clearButtonMode = UITextFieldViewMode.Never//从不

        textField.clearButtonMode =UITextFieldViewMode.WhileEditing//当编辑的时候

        textField.clearButtonMode =UITextFieldViewMode.UnlessEditing//当不编辑的时候

        textField.clearButtonMode = UITextFieldViewMode.Always//总是

        //再次编辑时是否清空

        textField.clearsOnBeginEditing = true

        //设置输入框左侧头像

        let hadeimage = UIImageView.init(frame: CGRectMake(0, 0, 25, 30))

        hadeimage.image = UIImage.init(named: "DOVE 10")

        textField.leftView = hadeimage;

        //设置模式

        textField.leftViewMode = UITextFieldViewMode.Never;

        textField.leftViewMode = UITextFieldViewMode.WhileEditing;

        textField.leftViewMode =UITextFieldViewMode.UnlessEditing;

        textField.leftViewMode = UITextFieldViewMode.Always;

        //设置自定义弹出视图

        let imageView = UIImageView.init(image: UIImage(named: "DOVE 1"))

        //这里100的设置是无效的

        imageView.frame=CGRectMake(0, 200, 320, 300)

        //弹出图片

        textField.inputView = imageView

        self.view.addSubview(textField)

2.UIStepper步进控件

作用:精确的修改及记录一个数值

核心属性:  value

核心事件:  valueChanged

常用设置:

autorepeat:按下不松手时,是否可以连续变化

continuous:按下不松手时,连续变化的过程是否可见

wrap:到达边界值时,是否重复

设置步进控件stepper的属性(代码的方式)

self.stepper.minimumValue = 13; //设置最小值

self.stepper.maximumValue = 50; //设置最大值

self.stepper.stepValue = 1; //设置步进规律

self.stepper.value = 13; //设置当前value

设置textField 中placeholder 文字颜色 

[_userName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

  3)UISlider 滑块控件

作用:通过一个滑块来快速的在某个范围内记录一个数值,此数值不是很容易精确的控制

核心属性:value

核心事件:valueChanged

[Demo2_滑块控件]

  4)UISwitch 开关控件

作用:记录一个是或否的状态

核心属性: on (类型:BOOL)

核心事件:valueChanged