1,按鈕的創(chuàng)建
(1)按鈕有下面四種類型:
UIButtonType.system:前面不帶圖標(biāo),默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)的高亮效果
UIButtonType.custom:定制按鈕,前面不帶圖標(biāo),默認(rèn)文字顏色為白色,無觸摸時(shí)的高亮效果
UIButtonType.contactAdd:前面帶“+”圖標(biāo)按鈕,默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)的高亮效果
UIButtonType.detailDisclosure:前面帶“!”圖標(biāo)按鈕,默認(rèn)文字顏色為藍(lán)色,有觸摸時(shí)的高亮效果
UIButtonType.infoDark:為感嘆號(hào)“!”圓形按鈕
UIButtonType.infoLight:為感嘆號(hào)“!”圓形按鈕
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一樣的)
//創(chuàng)建一個(gè)ContactAdd類型的按鈕
let button:UIButton = UIButton(type:.contactAdd)
//設(shè)置按鈕位置和大小
button.frame = CGRect(x:10, y:150, width:100, height:30)
//設(shè)置按鈕文字
button.setTitle("按鈕", for:.normal)
self.view.addSubview(button)
(2)對(duì)于Custom定制類型按鈕,代碼可簡化為:
let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))
2,按鈕的文字設(shè)置
button.setTitle("普通狀態(tài)", for:.normal) //普通狀態(tài)下的文字
button.setTitle("觸摸狀態(tài)", for:.highlighted) //觸摸狀態(tài)下的文字
button.setTitle("禁用狀態(tài)", for:.disabled) //禁用狀態(tài)下的文字
3,按鈕文字顏色的設(shè)置
button.setTitleColor(UIColor.black, for: .normal) //普通狀態(tài)下文字的顏色
button.setTitleColor(UIColor.green, for: .highlighted) //觸摸狀態(tài)下文字的顏色
button.setTitleColor(UIColor.gray, for: .disabled) //禁用狀態(tài)下文字的顏色
4,按鈕文字陰影顏色的設(shè)置
button.setTitleShadowColor(UIColor.green, for:.normal) //普通狀態(tài)下文字陰影的顏色
button.setTitleShadowColor(UIColor.yellow, for:.highlighted) //觸摸狀態(tài)下文字陰影的顏色
button.setTitleShadowColor(UIColor.gray, for:.disabled) //禁用狀態(tài)下文字陰影的顏色
5,按鈕文字的字體和大小設(shè)置
button.titleLabel?.font = UIFont.systemFont(ofSize: 11)
6,按鈕背景顏色設(shè)置
button.backgroundColor = UIColor.black
**7,按鈕文字圖標(biāo)的設(shè)置 **
(1)默認(rèn)情況下按鈕會(huì)被渲染成單一顏色
button.setImage(UIImage(named:"icon1"),forState:.Normal) //設(shè)置圖標(biāo)
button.adjustsImageWhenHighlighted=false //使觸摸模式下按鈕也不會(huì)變暗(半透明)
button.adjustsImageWhenDisabled=false //使禁用模式下按鈕也不會(huì)變暗(半透明)
(2)也可以設(shè)置成保留圖標(biāo)原來的顏色!
let iconImage = UIImage(named:"icon2")?.withRenderingMode(.alwaysOriginal)
button.setImage(iconImage, for:.normal) //設(shè)置圖標(biāo)
button.adjustsImageWhenHighlighted = false //使觸摸模式下按鈕也不會(huì)變暗(半透明)
button.adjustsImageWhenDisabled = false //使禁用模式下按鈕也不會(huì)變暗(半透明)
修改圖標(biāo)與文字的相對(duì)位置和間距 默認(rèn)圖片和文字的相對(duì)位置是固定的(按鈕在前,文字在后)。我們可以通過擴(kuò)展UIButton來實(shí)現(xiàn)自由調(diào)整位置和偏移量,具體參考我寫的另一篇文章:Swift - 自由調(diào)整圖標(biāo)按鈕中的圖標(biāo)和文字位置(擴(kuò)展UIButton) 原文出自:www.hangge.com 轉(zhuǎn)載請(qǐng)保留原文鏈接:http://www.hangge.com/blog/cache/detail_529.html
8,設(shè)置按鈕背景圖片
button.setBackgroundImage(UIImage(named:"bg1"), for:.normal)
9,按鈕觸摸點(diǎn)擊事件響應(yīng)
//不傳遞觸摸對(duì)象(即點(diǎn)擊的按鈕)
button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
func tapped(){
print("tapped")
}
//傳遞觸摸對(duì)象(即點(diǎn)擊的按鈕),需要在定義action參數(shù)時(shí),方法名稱后面帶上冒號(hào)
button.addTarget(self, action:#selector(tapped(_:)), for:.touchUpInside)
func tapped(_ button:UIButton){
print(button.title(for: .normal))
}
常用的觸摸事件類型:
touchDown:單點(diǎn)觸摸按下事件,點(diǎn)觸屏幕
touchDownRepeat:多點(diǎn)觸摸按下事件,點(diǎn)觸計(jì)數(shù)大于1,按下第2、3或第4根手指的時(shí)候
touchDragInside:觸摸在控件內(nèi)拖動(dòng)時(shí)
touchDragOutside:觸摸在控件外拖動(dòng)時(shí)
touchDragEnter:觸摸從控件之外拖動(dòng)到內(nèi)部時(shí)
touchDragExit:觸摸從控件內(nèi)部拖動(dòng)到外部時(shí)
touchUpInside:在控件之內(nèi)觸摸并抬起事件
touchUpOutside:在控件之外觸摸抬起事件
touchCancel:觸摸取消事件,即一次觸摸因?yàn)榉派咸嗍种付蝗∠?,或者電話打?/p>
10,按鈕文字太長時(shí)的處理方法
默認(rèn)情況下,如果按鈕文字太長超過按鈕尺寸,則會(huì)省略中間的文字。比如下面代碼:
let button = UIButton(frame:CGRect(x:20, y:50, width:130, height:50))
button.setTitle("這個(gè)是一段 very 長的文字", for:.normal) //普通狀態(tài)下的文字
button.setTitleColor(UIColor.white, for: .normal) //普通狀態(tài)下文字的顏色
button.backgroundColor = UIColor.orange
self.view.addSubview(button)
運(yùn)行效果如下,中間文字使用 ... 代替。
我們通過修改 button 按鈕中 titleLabel 的 lineBreakMode 屬性,便可以調(diào)整按鈕在文字超長的情況下如何顯示,以及是否換行。
//省略尾部文字
button.titleLabel?.lineBreakMode = .byTruncatingTail
lineBreakMode 共支持如下幾種樣式:
1).byTruncatingHead:省略頭部文字,省略部分用...代替
2).byTruncatingMiddle:省略中間部分文字,省略部分用...代替(默認(rèn))
3).byTruncatingTail:省略尾部文字,省略部分用...代替
4).byClipping:直接將多余的部分截?cái)?/p>
5).byWordWrapping:自動(dòng)換行(按詞拆分)
6).byCharWrapping:自動(dòng)換行(按字符拆分)
注意:當(dāng)設(shè)置自動(dòng)換行后(byWordWrapping 或 byCharWrapping),我們可以在設(shè)置 title 時(shí)通過 \n 進(jìn)行手動(dòng)換行。
button.setTitle("歡迎訪問\nhangge.com", for:.normal)