Common property methods for UIbutton for iOS development, and uibutton for ios development
Simple use of UIbutton (basically enough ).
UIButton * myButton = [UIButton buttonWithType: UIButtonTypeCustom];
/*
UIButtonTypeCustom = 0, // custom Style
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS (7_0), // standard system button
UIButtonTypeDetailDisclosure, blue arrow button, mainly used for detailed description
UIButtonTypeInfoLight, blue exclamation point
UIButtonTypeInfoDark, dark exclamation point
UIButtonTypeContactAdd, cross plus button
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
*/
MyButton. frame = CGRectMake (100,100,100, 50); // The Position of the button on the view
[MyButton setBackgroundColor: [UIColor brownColor]; // background color
[Self. view addSubview: myButton];
[MyButton setImage: [UIImage imageNamed: @ "buttonimage.gif"] forState: UIControlStateNormal];
// [MyButton setBackgroundImage: [UIImage imageNamed: @ "buttonimage.gif"] forState: UIControlStateNormal];
[MyButton setTitle: @ "not easy" forState: UIControlStateNormal];
// Set the padding of the image
/*
UIControlStateNormal = 0, the general status is displayed
UIControlStateHighlighted = 1 <0, highlighted
UIControlStateDisabled = 1 <1, the disabled status is displayed.
UIControlStateSelected = 1 <2, selected
UIControlStateApplication = 0x00FF0000, when the application flag
UIControlStateReserved = 0xFF000000 is reserved for the internal framework.
*/
MyButton. imageEdgeInsets = UIEdgeInsetsMake (0, 20, 0, 0 );
// Set the padding of Text
MyButton. titleEdgeInsets = UIEdgeInsetsMake (0, 50, 0, 0 );
/**
Make sure that the imageView of the button is in the original shape after rotation.
UIViewContentModeScaleToFill, stretch and fill
UIViewContentModeScaleAspectFit, adaptive
UIViewContentModeScaleAspectFill, adaptive Filling
UIViewContentModeCenter to maintain the original size
*/
MyButton. imageView. contentMode = UIViewContentModeCenter;
/*
* By default, when the button is highlighted, the color of the image will be darker. If this attribute is set to no,
* This function can be removed.
*/
MyButton. adjustsImageWhenHighlighted = NO;
/* Keep up with the above situation. By default, when the button is disabled, the image will be painted a little deeper. Setting NO can cancel the setting */
MyButton. adjustsImageWhenDisabled = NO;
/* When this attribute is set to yes, the button will emit light when it is pressed */
MyButton. showsTouchWhenHighlighted = YES;
/* Add events to the button. There are many types of events. I will open a separate blog to introduce them. The following time indicates
This event is triggered when you press the button and move your finger away from the screen, just like the click event on the web.
After this event is triggered, execute butClick: This method. addTarget: self means that this method is in this class.
You can also input pointers of other classes */
[MyButton addTarget: self action: @ selector (butClick :) forControlEvents: UIControlEventTouchUpInside];
// Cancel all events added by the button: (this is important. If two events are added, both events are triggered)
[MyButton removeTarget: nil action: nil forControlEvents: UIControlEventTouchUpInside];
/*
{
UIControlEventTouchDown
Single point touch: when a user clicks the screen, or a new finger falls.
UIControlEventTouchDownRepeat
When you press the second, third, or fourth finger, the trigger count is greater than 1.
UIControlEventTouchDragInside
When one touch is dragged in the control window.
UIControlEventTouchDragOutside
When one touch is dragged outside the control window.
UIControlEventTouchDragEnter
When one touch is dragged from outside the control window to the interior.
UIControlEventTouchDragExit
When a touch is dragged from the inside of the control window to the outside.
UIControlEventTouchUpInside
All touch-up events within the control.
UIControlEventTouchUpOutside
All touch-up events outside the control (the touch must start with the control before sending notifications to the control ).
UIControlEventTouchCancel
All touch cancellation events, that is, one touch is canceled because too many fingers are put on it, or it is locked or the call is interrupted.
UIControlEventTouchChanged
Send a notification when the value of the control changes. It is used for slider, segment control, and other value controls. You can configure the slider control to send notifications when the slider is put down, or when the slider is dragged.
UIControlEventEditingDidBegin
A notification is sent when editing is started in the text control.
UIControlEventEditingChanged
A notification is sent when the text in the text control is changed.
UIControlEventEditingDidEnd
A notification is sent when the editing ends in the text control.
UIControlEventEditingDidOnExit
Send a notification when the text control ends editing by pressing the Enter key (or equivalent behavior.
UIControlEventAlltouchEvents
Notify all touch events.
UIControlEventAllEditingEvents
Notify all text editing events.
UIControlEventAllEvents
Notify all events.
}
*/
// ClipsToBounds = YES; the part beyond the boundary of the parent view will be cut out.
MyButton. imageView. clipsToBounds = NO;
// Obtain the button image and title
UIImage * image = [myButton backgroundImageForState:
UIControlStateHighlighted]; // obtain the highlighted image;
UIImage * img = myButton. currentImage; // obtain the image of the current button;
NSString * str = [myButton titleForState: UIControlStateNormal]; // obtain the button title in normal state.
// Cancel button image adjustment
MyButton. adjustsImageWhenDisabled = NO;
// Cancel the highlighted button image adjustment
MyButton. adjustsImageWhenHighlighted = NO;
// ============================= Auto-size of Button title
// ********** 1. Determine the font size of the Label or Button to make its width adaptive.
UILabel * contentLabel = [[UILabel alloc] initWithFrame: CGRectMake (100,100,120, 30)];
ContentLabel. font = [UIFont systemFontOfSize: 15]; // --------> define the Font size
ContentLabel. backgroundColor = [UIColor redColor];
ContentLabel. text = @ "I already know and have read and agreed to the above terms ";
[ContentLabel sizeToFit]; // --------> fixed font size and adaptive width
[Self. view addSubview: contentLabel];
// ************ 2. Determine the width of the Label or Button to make the font size adaptive
// You do not need to set the font size.
UILabel * contentLabel1 = [[UILabel alloc] initWithFrame: CGRectMake (100,100,120, 30)];
ContentLabel1.backgroundColor = [UIColor redColor];
ContentLabel1.text = @ "I already know and have read and agree to the above terms ";
ContentLabel1.adjustsFontSizeToFitWidth = YES; // fixed width and adaptive font size
[Self. view addSubview: contentLabel1];
// *********** If it is a Button, there is only one small difference as above:
[MyButton. titleLabel sizeToFit];
MyButton. titleLabel. adjustsFontSizeToFitWidth = YES;
// Set the border color and rounded corner
[MyButton. layer setMasksToBounds: YES];
[MyButton. layer setCornerRadius: 10.0]; // you can specify the radius of the four rounded corners of a rectangle.
// Border Width
[MyButton. layer setBorderWidth: 1.0];
// You can set the border color in either of the following ways:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGColorRef colorref = CGColorCreate (colorSpace, (CGFloat []) {0, 0, 0, 1 });
[MyButton. layer setBorderColor: colorref]; // border color
// The second method is as follows:
// _ TestButton. layer. borderColor = [UIColor grayColor]. CGColor;
Convenience for your own use, and hope to make it easier for others