常用控件

1. UIButton 按钮

1.1 UIButton的创建

1
2
3
4
5
6
 let button = UIButton(type: UIButtonType.contactAdd)
 button.frame = CGRect(x:100, y:100, width:100, height:100)
 button.backgroundColor = UIColor.cyan
 button.setTitle("Add", for: UIControlState.normal)

 self.view.addSubview(button)

1.2 UIButton的类型

1
2
3
4
5
6
 UIButtonType.ContactAdd.     : 前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.DetailDisclosure: 前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.System.         : 前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.Custom.         : 定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
 UIButtonType.InfoDark.       : 为感叹号“!”圆形按钮
 UIButtonType.InfoLight.      : 为感叹号“!”圆形按钮

1.3 UIButton的文字设置

1
2
3
 UIControlState.Normal.    :普通状态下的文字
 UIControlState.Highlighted:触摸状态下的文字
 UIControlState.Disabled.  :禁用状态下的文字

1.4 UIButton文字颜色的设置

1
2
3
 button.setTitleColor(UIColor.black,for: .normal)      //普通状态下文字的颜色
 button.setTitleColor(UIColor.green,for: .highlighted) //触摸状态下文字的颜色
 button.setTitleColor(UIColor.gray,for: .disabled)     //禁用状态下文字的颜色

1.5 UIButton文字阴影颜色的设置

1
2
3
 button.setTitleShadowColor(UIColor.green,for:.normal)       //普通状态下文字阴影的颜色
 button.setTitleShadowColor(UIColor.yellow,for:.highlighted) //普通状态下文字阴影的颜色
 button.setTitleShadowColor(UIColor.gray,for:.disabled)     //普通状态下文字阴影的颜色

1.6 常用的触摸事件类型

1
2
3
4
5
6
7
8
9
 TouchDown.      :单点触摸按下事件,点触屏幕
 TouchDownRepeat :多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
 TouchDragInside :触摸在控件内拖动时
 TouchDragOutside:触摸在控件外拖动时
 TouchDragEnter  :触摸从控件之外拖动到内部时
 TouchDragExit   :触摸从控件内部拖动到外部时
 TouchUpInside   :在控件之内触摸并抬起事件
 TouchUpOutside. :在控件之外触摸抬起事件
 TouchCancel     :触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断

1.7 设置按钮的圆角

1
2
 button.layer.masksToBounds = true
 button.layer.cornerRadius = 6.0

1.8 设置按钮点击时高亮,默认点击时是高亮的状态

1
 button.showsTouchWhenHighlighted = true

2. UILabel 文本视图

2.1 UILabel的创建和文字显示

1
2
3
4
5
 let rect = CGRect(x: 20, y: 100, Width: 280, height: 80)
 let label = UILabel(frame: rect)
 label.text = "Hello, Xcode and Swift 3!"

 label.textAlignment = NSTextAlignment.right

2.2 UILabel处理过长的文字

在实际的开发中,文字过长的情况是难免的,虽然手机屏幕的大小已经从iphone4的3.5寸,到现在的iphone8的5.5寸,但是手机的屏幕的尺寸仍然是有限的,所以UILabel上显示的文字的长度就一定是有限的。 下面罗列出几种处理方法:

1
2
3
4
5
6
7
8
 // 1.增加行数
 label.numberOfLines = 0

 // 2.字符串的截取
 label.lineBreakMode = NSLineBreakMode.byTruncatingMiddle

 // 调整字体大小以完全显示文字
 label.adjustsFontSizeToFitWidth = true

针对上记案<2>的处理方法,有下记的说明进行参照:

名称 功能说明
byWordWrapping 以单词为显示单位显示,后面部分省略不显示
byCharWrapping 以字符为显示单位显示,后面部分省略不显示
byClipping 剪切与文本宽度相同的内容长度,后半部分被删除
byTruncatingHead 开头省略,显示尾部文字内容
byTruncatingTail 结尾省略,显示开头的文字内容,默认
byTruncatingMiddle 中间省略,显示头尾文字内容

3. UITextField 文本框控件

3.1 文本框的创建

1
2
3
4
5
 let textField = UITextField(frame: CGRect(x: 50, y: 300, width: 100, height: 30))
 textField.borderStyle = UITextBorderStyle.none
 textField.placeholder="请输入"
 textField.backgroundColor = UIColor.white
 self.view.addSubview(textField)

3.2 文本框的样式

1
2
3
4
 UITextBorderStyle.None       :无边框
 UITextBorderStyle.Line       :直线边框
 UITextBorderStyle.RoundedRect:圆角矩形边框
 UITextBorderStyle.Bezel      :边线+阴影

3.3 文字大小超过文本框长度时自动缩小字号

1
2
 textField.adjustsFontSizeToFitWidth = true 
 textField.minimumFontSize = 14

3.4 设置水平/垂直对齐方式

1
2
3
4
5
6
7
8
9
 //水平对齐 
 textField.textAlignment = .right  //水平右对齐
 textField.textAlignment = .center //水平居中对齐
 textField.textAlignment = .left   //水平左对齐

 // 垂直对齐 
 textField.contentVerticalAlignment = .top    //垂直向上对齐
 textField.contentVerticalAlignment = .center //垂直居中对齐
 textField.contentVerticalAlignment = .bottom //垂直向下对齐。

3.5 设置输入框右边的小叉叉(清除按钮)

1
2
3
 textField.clearButtonMode=UITextFieldViewMode.whileEditing   //编辑时出现清除按钮
 textField.clearButtonMode=UITextFieldViewMode.unlessEditing  //编辑时不出现,编辑后才出现清除按钮
 textField.clearButtonMode=UITextFieldViewMode.always         //一直显示清除按钮

3.6 设置文本框关联的键盘类型

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 textField.keyboardType = UIKeyboardType.default                //系统默认的虚拟键盘
 textField.keyboardType = UIKeyboardType.numbersAndPunctuation  //显示数字和标点的虚拟键盘
 textField.keyboardType = UIKeyboardType.URL                    //显示便于输入数字的虚拟键盘
 textField.keyboardType = UIKeyboardType.numberPad              //显示便于输入数字的虚拟键盘
 textField.keyboardType = UIKeyboardType.phonePad               //显示便于拨号呼叫的虚拟键盘
 textField.keyboardType = UIKeyboardType.namePhonePad           //显示便于聊天拨号的虚拟键盘
 textField.keyboardType = UIKeyboardType.emailAddress           //显示便于输入Email的虚拟键盘
 textField.keyboardType = UIKeyboardType.decimalPad             //显示用于输入数字和小数点的虚拟键盘
 textField.keyboardType = UIKeyboardType.twitter                //显示方便些Twitter的虚拟键盘
 textField.keyboardType = UIKeyboardType.webSearch              //显示便于在网页上书写的虚拟键盘。

3.7 使文本框在界面打开时就获取焦点,并弹出输入键盘

1
 textField.becomeFirstResponder()

3.8 使文本框失去焦点,并收回键盘

1
 textField.resignFirstResponder()

3.9 设置键盘return键的样式

1
2
3
4
5
6
 textField.returnKeyType = UIReturnKeyType.done   //表示完成输入
 textField.returnKeyType = UIReturnKeyType.go     //表示完成输入,同时会跳到另一页
 textField.returnKeyType = UIReturnKeyType.search //表示搜索
 textField.returnKeyType = UIReturnKeyType.join   //表示注册用户或添加数据
 textField.returnKeyType = UIReturnKeyType.next   //表示继续下一步
 textField.returnKeyType = UIReturnKeyType.send   //表示发送

4. UISwitch 开关视图

注意:UISwitch的大小设置是无效的,其永远保持在width为51px,height为31px的大小