iOS开发学习笔记之UIButton的使用
UIButton:
https://developer.apple.com/reference/uikit/uibutton
一、 描述:
UIButton对象是一个视图,它执行您的自定义代码以响应用户交互。
二、概述:
当您点击按钮或选择具有焦点的按钮时,该按钮将执行附加的任何操作。 您使用文本标签,图像或两者来传达按钮的目的。 按钮的外观是可配置的,因此您可以使用调节按键颜色或格式标题来匹配应用程序的设计。 您可以通过编程方式或使用Interface Builder向界面添加按键。
三、 按键的使用步骤:
向界面添加按钮时,请执行以下步骤:
1、在创建时设置按钮的类型。
2、提供标题字符串或图像; 为您的内容适当调整按钮的大小。
3、将一个或多个动作方法连接到按钮。
4、设置自动布局规则来控制界面中按钮的大小和位置。
5、提供辅助功能信息和本地化字符串。
四、按钮状态:
按钮有五个状态定义其外观:default, highlighted, focused, selected, and disabled。当您将一个按钮添加到界面时,它最初处于default状态,这意味着该按钮已启用,并且用户未与其进行交互。当用户与按钮进行交互时,其状态将更改为其他值。例如,当用户点击带有标题的按钮时,按钮将移动到highlighted的状态。
以编程方式或在Interface Builder中配置按钮时,可以分别为每个状态指定属性。在Interface Builder中,使用“属性”检查器中的“状态配置”控件选择适当的状态,然后配置其他属性。如果不为特定状态指定属性,则UIButton类提供合理的默认行为。例如,disabled的按钮通常变暗,并且在轻拍时不显示高亮。该类的其他属性,如adjustImageWhenHighlighted和adjustImageWhenDisabled属性,可以在特定情况下更改默认行为。
五、内容:
按钮的内容由您指定的标题字符串或图像组成。您指定的内容用于配置按钮本身管理的UILabel和UIImageView对象。您可以使用titleLabel或imageView属性访问这些对象,并直接修改其值。此类的方法还提供了一个方便的快捷方式来配置字符串或图像的外观。
通常,您可以使用标题或图像配置按钮,并相应地调整按钮的大小。按钮也可以有一个背景图像,它位于您指定的内容后面。可以同时指定图像和按钮的标题,这将导致图2所示的外观。您可以使用指定的属性访问按钮的当前内容。
六、代码块:
//代码生成的button
//设置按键的大小和位置
//customizeButton.frame = CGRect(x:10, y:150, width:200, height:40)
customizeButton.frame = changeButton.frame
customizeButton.frame.origin.x = changeButton.frame.origin.x + changeButton.frame.size.width + 10
//设置按键文字
customizeButton.setTitle("按键", for: .normal)
customizeButton.setTitleColor(UIColor.red, for: .normal)
customizeButton.setTitleColor(UIColor.green, for: .highlighted)
//设置字体大小
customizeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//设置背景颜色
customizeButton.backgroundColor = UIColor(red: 0.5, green: 0.3, blue: 0.1, alpha: 1.0)
//customizeButton.backgroundColor = UIColor(red: 0.1, green: 0.3, blue: 0.5, alpha: 1.0)
self.view.addSubview(customizeButton)
//在IB中添加的button
@IBOutlet weak var changeButton: UIButton!
@IBAction func changeButton(_ sender: UIButton) {
if sender.currentTitle == "Current" {
displayText.text = "Hello World!"
displayText.backgroundColor = UIColor(red: 0.6, green: 0.5, blue: 0.3, alpha: 1.0)
changeButton.setTitle("Change", for: .normal)
changeButton.backgroundColor = UIColor(red: 0.6, green: 0.5, blue: 0.3, alpha: 1.0)
}else if sender.currentTitle == "Change"{
displayText.text = "chenling"
displayText.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0)
changeButton.setTitle("Current", for: .normal)
changeButton.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0)
}
}
网上的资料
一、按钮的创建
1、按钮有下面四种类型:
(1)
UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.infoDark:为感叹号“!”圆形按钮
UIButtonType.infoLight:为感叹号“!”圆形按钮
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)
//创建一个ContactAdd类型的按钮
let button:UIButton = UIButton(type:.contactAdd)
//设置按钮位置和大小
button.frame = CGRect(x:10, y:150, width:100, height:30)
//设置按钮文字button.setTitle("按钮", for:.normal)
self.view.addSubview(button)
(2)对于Custom定制类型按钮,代码可简化为:
let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))
2,按钮的文字设置
button.setTitle("普通状态", for:.normal) //普通状态下的文字
button.setTitle("触摸状态", for:.highlighted) //触摸状态下的文字
button.setTitle("禁用状态", for:.disabled) //禁用状态下的文字
3,按钮文字颜色的设置
button.setTitleColor(UIColor.black, for: .normal) //普通状态下文字的颜色
button.setTitleColor(UIColor.green, for: .highlighted) //触摸状态下文字的颜色
button.setTitleColor(UIColor.gray, for: .disabled) //禁用状态下文字的颜色
4,按钮文字阴影颜色的设置
button.setTitleShadowColor(UIColor.green, for:.normal) //普通状态下文字阴影的颜色
button.setTitleShadowColor(UIColor.yellow, for:.highlighted) //普通状态下文字阴影的颜色
button.setTitleShadowColor(UIColor.gray, for:.disabled) //普通状态下文字阴影的颜色
5,按钮文字的字体和大小设置
button.titleLabel?.font = UIFont.systemFont(ofSize: 11)
6,按钮背景颜色设置
button.backgroundColor = UIColor.black
7,按钮文字图标的设置
(1)默认情况下按钮会被渲染成单一颜色
button.setImage(UIImage(named:"icon1"),forState:.Normal) //设置图标
button.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
button.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
(2)也可以设置成保留图标原来的颜色
let iconImage = UIImage(named:"icon2")?.withRenderingMode(.alwaysOriginal)
button.setImage(iconImage, for:.normal) //设置图标
button.adjustsImageWhenHighlighted = false //使触摸模式下按钮也不会变暗(半透明)
button.adjustsImageWhenDisabled = false //使禁用模式下按钮也不会变暗(半透明)
修改图标与文字的相对位置和间距
默认图片和文字的相对位置是固定的(按钮在前,文字在后)。我们可以通过扩展UIButton来实现自由调整位置和偏移量,具体参考我写的另一篇文章:Swift - 自由调整图标按钮中的图标和文字位置(扩展UIButton)
8,设置按钮背景图片
button.setBackgroundImage(UIImage(named:"bg1"), for:.normal)
9,按钮触摸点击事件响应
//不传递触摸对象(即点击的按钮)
button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
func tapped(){
print("tapped")
}
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
button.addTarget(self, action:#selector(tapped(_:)), for:.touchUpInside)
func tapped(_ button:UIButton){
print(button.title(for: .normal))
}
常用的触摸事件类型:
touchDown:单点触摸按下事件,点触屏幕
touchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
touchDragInside:触摸在控件内拖动时
touchDragOutside:触摸在控件外拖动时
touchDragEnter:触摸从控件之外拖动到内部时
touchDragExit:触摸从控件内部拖动到外部时
touchUpInside:在控件之内触摸并抬起事件
touchUpOutside:在控件之外触摸抬起事件
touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
10,按钮文字太长时的处理方法
默认情况下,如果按钮文字太长超过按钮尺寸,则会省略中间的文字。比如下面代码:
let button = UIButton(frame:CGRect(x:20, y:50, width:130, height:50))
button.setTitle("这个是一段 very 长的文字", for:.normal) //普通状态下的文字
button.setTitleColor(UIColor.white, for: .normal) //普通状态下文字的颜色
button.backgroundColor = UIColor.orange
self.view.addSubview(button)运行效果如下,中间文字使用 ... 代替。
我们通过修改 button 按钮中 titleLabel 的 lineBreakMode 属性,便可以调整按钮在文字超长的情况下如何显示,以及是否换行。
//省略尾部文字
button.titleLabel?.lineBreakMode = .byTruncatingTail
lineBreakMode 共支持如下几种样式:
• .byTruncatingHead:省略头部文字,省略部分用…代替
• .byTruncatingMiddle:省略中间部分文字,省略部分用…代替(默认)
• .byTruncatingTail:省略尾部文字,省略部分用…代替
• .byClipping:直接将多余的部分截断
• .byWordWrapping:自动换行(按词拆分)
• .byCharWrapping:自动换行(按字符拆分)
注意:当设置自动换行后(byWordWrapping 或 byCharWrapping),我们可以在设置 title 时通过 \n 进行手动换行。
button.setTitle("欢迎访问\nhangge.com", for:.normal)