UIButton, uibutton text in the left
// UIButton-> UIControl-> UIView
// UIControl controls with operations are inherited from them
// UIButton instantiation class method instantiation
// There is no position or size during instantiation. You need to set the frame attribute.
/*
1. UIButtonTypeSystem = UIButtonTypeRoundedRect before iOS7 UIButtonTypeRoundedRect has a rounded corner effect, which is not available after iOS7
2. UIButtonTypeInfoLight = UIButtonTypeInfoDark = UIButtonTypeDetailDisclosure blue circle I
3. UIButtonTypeContactAdd Blue Circle +
*/
UIButton * button = [UIButton buttonWithType: UIButtonTypeSystem];
// Frame
Button. frame = CGRectMake (20,100,280, 40 );
// Attributes
Button. backgroundColor = [UIColor redColor];
// Button availability: NO: unavailable YES: available, default status
Button. enabled = YES;
// Set text: setTitle: forState:
/*
1. General UIControlStateNormal status
2. UIControlStateHighlighted highlighted state
3. UIControlStateDisabled disabled
*/
[Button setTitle: @ "button" forState: UIControlStateNormal];
// [Button setTitle: @ "highlighted" forState: UIControlStateHighlighted];
[Button setTitle: @ "disabled" forState: UIControlStateDisabled];
// Set the text color: Default blue setTitleColor: forState:
[Button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[Button setTitleColor: [UIColor whiteColor] forState: UIControlStateHighlighted];
// Set the text size:. titleLabel. font
Button. titleLabel. font = [UIFont systemFontOfSize: 17.0];
// Text alignment
// Horizontal direction: contentHorizontalAlignment
/*
1. UIControlContentHorizontalAlignmentCenter Center
2. UIControlContentHorizontalAlignmentLeft left
3. UIControlContentHorizontalAlignmentRight right
*/
Button. contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Vertical direction: contentVerticalAlignment
/*
1. UIControlContentVerticalAlignmentBottom
2. UIControlContentVerticalAlignmentCenter Center
3. UIControlContentVerticalAlignmentTop alignenttop
*/
Button. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;
// ** Click Event **//
/*
1. First parameter: who will be notified after the button is clicked: self
2. Second parameter: button event: @ selector (method name)
3. Third parameter: Button clicking Method
*/
// Button event style without Parameters
// [Button addTarget: self action: @ selector (buttonClick) forControlEvents: UIControlEventTouchUpInside];
// Button event with parameter Style
[Button addTarget: self action: @ selector (buttonDown :) forControlEvents: UIControlEventTouchUpInside];
/*
Button clicking Method
1. UIControlEventTouchUpInside
2. UIControlEventTouchUpOutside
3. Drag in UIControlEventTouchDragInside
4. UIControlEventTouchDragOutside
5. Drag and Drop the UIControlEventTouchDragExit file.
6. Drag the UIControlEventTouchDragEnter file and drag it back.
7. UIControlEventTouchDown
8. Double-click UIControlEventTouchDownRepeat.
*/
// Add to parent View
[Self. window addSubview: button];
**************************************** *********
# Pragma mark-button click event
// Implement the method with Parameters
-(Void) buttonDown :( UIButton *) button {
NSLog (@ "ddd ");
Self. window. backgroundColor = [UIColor colorWithRed: arc4random () % 256/255. 0 green: arc4random () % 256/255. 0 blue: arc4random () % 256/255. 0 alpha: 1.0];
// Obtain the text of the button
// CurrentTitle: If the button is highlighted, the text in the highlighted state is obtained directly. If only the nomal state exists, the text in the nomal state is obtained.
NSString * title = button. currentTitle;
//. TitleLabel. text: If the highlighted status of a button does not appear when you click it, you can obtain the text in nomal State. If the highlighted status is displayed, you can obtain the text in highlighted State. If the button only has nomal state, get text in nomal state
NSString * title2 = button. titleLabel. text;
NSLog (@ "currentTitle = % @; titleLabel = % @", title, title2 );
}
// Implementation without Parameters
-(Void) buttonClick {
// Arc4random () Random Number arc4random () % 256 random: 0-255
// Arc4random_uniform (256)
Self. window. backgroundColor = [UIColor colorWithRed: arc4random () % 256/255. 0 green: arc4random () % 256/255. 0 blue: arc4random () % 256/255. 0 alpha: 1.0];
}