UIButton, uibutton text in the left

Source: Internet
Author: User

UIButton, uibutton text in the left

// UIButton-button, which can be used to interact with users

// UIButton-> UIControl-> UIView

// Button is generally not created using the instance method

/*

UIButtonTypeCustom // custom button type, which is also the default value of the system (this is usually used)

UIButtonTypeSystem // system Style

The following style is rarely used, because we usually add a background image directly for the project button.

UIButtonTypeDetailDisclosure // Style

UIButtonTypeInfoLight // Style

UIButtonTypeInfoDark // Style

UIButtonTypeContactAdd // display a "" Image

UIButtonTypeRoundedRect = UIButtonTypeSystem // same as UIButtonTypeSystem

*/

UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom];

  

// Set the button position and size

Btn. frame = CGRectMake (100,100,100, 50 );

  

// Set the text of the button.

/* Button status

UIControlStateNormal // normal status

UIControlStateHighlighted // The status when highlighted

UIControlStateDisabled // status when the button is unavailable

UIControlStateSelected // The selected status

These statuses must be used together with the two attributes of the button. [note] These two attributes are very important, because many dynamic operations need to be determined through these two attributes in the future.

[Note] when using these two attributes, you must pay attention to the logic and actual situation. For example, you can set the button btn. set enabled to NO and btn. if selected is set to YES, it is a conflict, because you set it to not be able to press, then how to select it?

Btn. enabled // whether the press is allowed. YES indicates that the press is allowed, and NO indicates that the press is not allowed.

Btn. selected // check whether it is selected, YES is selected, and NO is selected

  

*/

// NORMAL STATE

[Btn setTitle: @ "button" forState: UIControlStateNormal];

// The status when the image is highlighted (the status when the image is not clicked)

[Btn setTitle: @ "clicked" forState: UIControlStateHighlighted];

// Set the font color

[Btn setTitleColor: [UIColor yellowColor] forState: UIControlStateNormal];

// The color when you click it.

[Btn setTitleColor: [UIColor purpleColor] forState: UIControlStateHighlighted];

// Modify the font size

Btn. titleLabel. font = [UIFont boldSystemFontOfSize: 16];

// Set the background color of the button

Btn. backgroundColor = [UIColor redColor];

// Set the shadow of button text

// [Btn setTitleShadowColor: [UIColor greenColor] forState: UIControlStateNormal];

// Set the offset of the button text shadow.

// [Btn setTitleShadowOffset: CGSizeMake (3, 3)];

// Modify the text offset

// Create a struct

// UIEdgeInsets set = UIEdgeInsetsMake (20, 0, 0, 0 );

// [Btn setTitleEdgeInsets: set];

// Set the background image of the button

// The image will be stretched to the width and height of the entire button by loading the background image. The disadvantage is that the image will be deformed.

// ImageNamed

UIImage * bgrdImg = [UIImage imageNamed: @ "account_candou"];

[Btn setBackgroundImage: bgrdImg forState: UIControlStateNormal];

// Set the button Image

// The image will not be pulled up. [note] What is the difference between the image and the above background image?

[Btn setImage: [UIImage imageNamed: @ "account_collect"] forState: UIControlStateNormal];

// Set the image offset

[Btn setImageEdgeInsets: UIEdgeInsetsMake (0, 0, 0,-30)];

 

// Add an event

/*

There are many types of UIControlEventyou. You can click command + to view multiple types.

*/

[Btn addTarget: self action: @ selector (btnAction :) forControlEvents: UIControlEventTouchUpInside];

 

// Add a tag for the button

// Tag is the property of UIView. [note] the tag value is usually large to avoid duplication. Do not set it to 0 because the default tag of self. window is 0.

_ Btn. tag= 5678;

  

 

 

*******************

// This function is the response event of the above button. Here, the function parameter is the button we press. With this value, we can get a lot of information about the button.

/*

Sender. currentTitle; // button text

Sender. currentTitleColor; // The text color of the button.

Sender. currentImage // The image on the button

Sender. currentBackgroundImage // The background image on the button

There are many more. You can view the files that come with Xcode on your own.

*/

-(Void) btnAction :( UIButton *) sender

{

Sender. enabled = NO;

}

 

Beginner exercise questions:

1. Question

It is imitation, click, then the other three will become gray, and no more clicks, you can change the above background color by clicking

Four buttons

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.