Swift Practice--UIButton

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.