Import UIKit
Class Viewcontroller:uiviewcontroller {
The creation of the button
Uibuttontype.system: Front without icon, default text is blue, with touch highlighting effect
Uibuttontype.custom: Custom button, front without icon, default text is white, no touch highlighting
Uibuttontype.contactadd: Front with + icon button, default text blue, no touch highlighting
Uibuttontype.detaildisclosure: Front Belt! icon, default text blue, with touch highlighting
Uibuttontype.infodark: Ibid.
Uibuttontype.infolight: Ibid.
Override Func Viewdidload () {
Super.viewdidload ()
Let Button:uibutton = UIButton (type:. Custom)
Button.frame = CGRect (x:100, y:100, width:100, height:100)
Button.settitle ("button", for:. Normal)
Self.view.addSubview (Button)
Button.backgroundcolor = uicolor.red
Add a Click event
Button.addtarget (Self, Action: #selector (tapped), for:. Touchupinside)
Button.addtarget (Self, Action: #selector (TOUCHBTN (sender:)), for:. Touchupinside)
Button text too long set Titlelabel Linebreakmode property adjustment
Button.titlelabel?. Linebreakmode =. byclipping
Linebreakmode supports several styles as follows
. Bytruncatinghead: Omit the head text, omit the part with ... Replace
. Bytruncatingmiddle: Omit middle section text
. Bytruncatingtail: Omit trailing text
. byclipping: Truncate the extra part directly
. Bywordwrapping: Line wrap (split by word)
. Bycharwrapping: Line Wrap (split by character)
Note: When you set the line wrap (bycharwrapping or bywordwrapping), we can manually wrap it by adding \ n When setting the title
}
Func tapped () {
Print ("Test")
}
Func touchbtn (Sender:uibutton) {
If let text = Sender.titlelabel?. Text {
Print (text)
}
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any resources the can be recreated.
}
}
Swift Practice--UIButton