The Objective-c:uibutton button is detailed

Source: Internet
Author: User

Detailed description of UIButton:

One, the button has the properties:

@property (nonatomic,readonly) Uibuttontype ButtonType; Button type

@property (Nonatomic,readonly,retain) NSString *currenttitle; Button Current text

@property (Nonatomic,readonly,retain) Uicolor *currenttitlecolor; button Current text color

@property (Nonatomic,readonly,retain) Uicolor *currenttitleshadowcolor; Button text Current shadow color

@property (Nonatomic,readonly,retain) UIImage *currentimage; Button current foreground picture

@property (Nonatomic,readonly,retain) UIImage *currentbackgroundimage; Button Current background picture

@property (nonatomic,readonly,retain) nsattributedstring *currentattributedtitle//Button Text Current property

@property (nonatomic,readonly,retain) UILabel *titlelabel//Button label

@property (nonatomic,readonly,retain) Uiimageview *imageview//Button view

@property (nonatomic) uicontrolcontentverticalalignment contentverticalalignment; Button Vertical Placement method

@property (nonatomic) uicontrolcontenthorizontalalignment contenthorizontalalignment; Button Horizontal Placement method

@property (nonatomic,readonly) uicontrolstate//button state type

Second, set the property value of the button

-(void) Settitle: (NSString *) title forstate: (uicontrolstate) state; Set Button text content

-(void) Settitlecolor: (Uicolor *) color forstate: (uicontrolstate) state//Set button text color

-(void) Settitleshadowcolor: (Uicolor *) color forstate: (uicontrolstate) state//Set button text shadow color

-(void) SetImage: (UIImage *) image forstate: (uicontrolstate) state; Set Button foreground picture

-(void) SetBackgroundImage: (UIImage *) image forstate: (uicontrolstate) state//Set button background picture

-(void) Setattributedtitle: (nsattributedstring *) title forstate: (uicontrolstate) state//Set Button Text property

Third, the state type of the button

Button Type Uicontrolstate:

UIControlStateNormal//Normal type

uicontrolstatehighlighted//Highlight Type

uicontrolstatedisabled//disable type

uicontrolstateselected//Check type

Uicontrolstateapplication//When application identity is used

uicontrolstatereserved//reserved for frame

Iv. getting the properties of a button

-(NSString *) Titleforstate: (uicontrolstate) state; Get button text

-(Uicolor *) Titlecolorforstate: (uicontrolstate) state; Get button text color

-(Uicolor *) Titleshadowcolorforstate: (uicontrolstate) state; Get button text Shadow color

-(UIImage *) Imageforstate: (uicontrolstate) state; Get button foreground picture

-(UIImage *) Backgroundimageforstate: (uicontrolstate) state; Get button background picture

-(Nsattributedstring *) Attributedtitleforstate: (uicontrolstate) state;//Get Button Text property

Five, button text placement method

Vertical placement:

Uicontrolcontentverticalalignmentcenter//Center

Uicontrolcontentverticalalignmenttop//Sticky

Uicontrolcontentverticalalignmentbottom//Bottom

Uicontrolcontentverticalalignmentfill//Fill

Horizontal placement:

Uicontrolcontenthorizontalalignmentcenter//Center

Uicontrolcontenthorizontalalignmentleft//Home Left

Uicontrolcontenthorizontalalignmentright//Home Right

Uicontrolcontenthorizontalalignmentfill//Fill

Description

(1) The button is used when adding text to the button because it has a state type. Titlelabel.text = @ "button" This assignment is not valid and is not displayed in the view, you should use [button Settitle: (NSString *) title forstate: (uicontrolstate)

State] This type of square is effective. This also sets the color of the text:

Set the color of the font on UIButton UIButton the color of the font, not with:

[Btn.titlelabel Settextcolor:[uicolorblackcolor]];

Btn.titlelabel.textcolor=[uicolor Redcolor];

Instead, use:

[Btn Settitlecolor:[uicolor Blackcolor]forstate:uicontrolstatenormal];

(2) to get the text of the button, you should use [button Currenttitle], if you use Button.titleLabel.Text, the result is not the text content you set. The same is true for the color of the text. [Button Currenttitlecolor]

(3) Set the size of the font on the button

[Button SetFont: [Uifont systemfontsize:14.0]; This can be used to set the size of the font, but may be removed in the Future SDK version of the change method

Button.titleLabel.font = [Uifont fontwithname: (nsstring*) FontName size:14.0]; should use

Or

button.    Titlelabel.font = [Uifont systemfontofsize:14.0]; should use

(4) Sometimes we want UIButton's title to align to the left.

Button.textLabel.textAlignment = uitextalignmentleft//is not useful, we need to set

Button.contenthorizontalalignment = Uicontrolcontenthorizonalignmentleft; Show left

But the problem comes out, the text will close to the border, we can set the text distance from the left side of the box to keep 10 pixels.

Button.contentedgeinsets = Uiedgeinsetsmake (0,10, 0, 0);

The Objective-c:uibutton button is detailed

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.