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)對于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ì)被渲染成單一顏色 (藍(lán)色)
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ì)變暗(半透明)
8,設(shè)置按鈕背景圖片
button.setBackgroundImage(UIImage(named:"bg1"), for:.normal)
9鉴逞,按鈕觸摸點(diǎn)擊事件響應(yīng)
//不傳遞觸摸對象(即點(diǎn)擊的按鈕)
button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
@objc func tapped(){
? ? print("tapped")
}
//傳遞觸摸對象(即點(diǎn)擊的按鈕)记某,需要在定義action參數(shù)時(shí),方法名稱后面帶上冒號(hào)
button.addTarget(self, action:#selector(tapped(_:)), for:.touchUpInside)
@objc 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 共支持如下幾種樣式:
.byTruncatingHead:省略頭部文字,省略部分用...代替
.byTruncatingMiddle:省略中間部分文字级乍,省略部分用...代替(默認(rèn))
.byTruncatingTail:省略尾部文字舌劳,省略部分用...代替
.byClipping:直接將多余的部分截?cái)?/p>
.byWordWrapping:自動(dòng)換行(按詞拆分)
.byCharWrapping:自動(dòng)換行(按字符拆分)
注意:當(dāng)設(shè)置自動(dòng)換行后(byWordWrapping 或 byCharWrapping),我們可以在設(shè)置 title 時(shí)通過 \n 進(jìn)行手動(dòng)換行玫荣。
button.setTitle("歡迎訪問\nhangge.com", for:.normal)