Basic definitions and common settings that UIButton, UILabel, and UITextField beginners need to understand. uibuttonuilabel
The following are the most commonly used controls in IOS development. As a basic learning tutorial for IOS, beginners need to understand the basic definition and frequently used settings to be used in development.
UIButtonButton
1. UIButton Definition
UIButton * button = [[UIButton buttonWithType :( UIButtonType );
The following six types of buttons can be defined,
Typedef enum {
UIButtonTypeCustom = 0, custom Style
UIButtonTypeRoundedRect, rounded rectangle
UIButtonTypeDetailDisclosure, blue arrow button, mainly used for detailed description
UIButtonTypeInfoLight, brilliant exclamation point
UIButtonTypeInfoDark, dark exclamation point
UIButtonTypeContactAdd, plus sign
} UIButtonType;
2. Set frame
Button1.frame = CGRectMake (20, 20,280, 40 );
[Button setFrame: CGRectMake (20, 20, 50, 50)];
Third, button background color
Button1.backgroundColor = [UIColor clearColor];
[Button setBackgroundColor: [UIColor blueColor];
4. state
ForState: this parameter defines the status in which the text or image of a button appears.
Enum {
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.
};
@ Property (nonatomic, getter = isEnabled) BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently
@ Property (nonatomic, getter = isSelected) BOOL selected; // default is NO may be used by some subclasses or by application
@ Property (nonatomic, getter = isHighlighted) BOOL highlighted;
Fifth, set the button to fill in the image and background image
[ButtonsetImage: [UIImageimageNamed: @ "checkmarkControllerIcon"] forState: UIControlStateNormal];
[ButtonsetBackgroundImage: [UIImageimageNamed: @ "checkmarkControllerIcon"] forState: UIControlStateNormal];
6. Set the button title and title Color
[Button1 setTitle: @ "click" forState: UIControlStateNormal];
[ButtonsetTitleColor: [UIColorredColor] forState: UIControlStateNormal];
Seventh, the setting button will shine
Button. showsTouchWhenHighlighted = NO;
8. add or delete events
[Button1 addTarget: self action: @ selector (butClick :) forControlEvents: UIControlEventTouchUpInside];
[Btn removeTarget: nil action: nil forControlEvents: UIControlEventTouchUpInside];
9. Set the image spacing and title spacing inside the button.
UIEdgeInsets insets; // you can specify the image spacing in the buttons.
Insets. top = insets. bottom = insets. right = insets. left = 10;
Bt. contentEdgeInsets = insets;
Bt. titleEdgeInsets = insets; // Title spacing
10. Others
// The set button is invalid. If the button is invalid, the button no longer responds to the user.
Btn. enabled = YES;
// Add a gesture reader to the button
[Btn addGestureRecognizer: tap];
// Add a button, for example
UIButton * calBtn = [[UIButton alloc] initWithFrame: CGRectMake (50,200,200, 40)]; // button size
CalBtn. backgroundColor = [UIColor orangeColor]; // background color
[CalBtn setTitle: @ "Click me, and I will calculate" forState: UIControlStateNormal]; // set the text in the default state
[CalBtn setTitle: @ "Click me, and I will calculate" forState: UIControlStateHighlighted]; // set the text in the highlighted state
[CalBtn setBackgroundImage: [UIImage imageNamed: @ "login_btn_n_Normal"] forState: UIControlStateNormal]; // sets the background image in the default state.
[CalBtn setBackgroundImage: [UIImage imageNamed: @ "logoff_btn_n_Highlighted"] forState: UIControlStateHighlighted]; // sets the background image in the highlighted state.
[Self. view addSubview: calBtn]; // you must add a button.
[Note] the image name should be modified in advance. It is best to add a word to the end to identify whether it is a default or highlighted state.
UILabelTag
UILabel * lbl = [[UILabel alloc] initWithFrame: CGRectMake (50,100,300,160)]; // size
Lbl. backgroundColor = [UIColor lightGrayColor]; // background color
Lbl. textColor = [UIColor blueColor]; // font color
// Lbl. shadowColor = [UIColor redColor]; // shadow effect, not commonly used
// Lbl. shadowOffset = CGSizeMake (4,-10 );
Lbl. text = @ "dormitory"; // Add text
// TAG content alignment
Lbl. textAlignment = NSTextAlignmentCenter;
// Set the number of rows of the tag. If it is set to 0, there can be any number of rows.
Lbl. numberOfLines = 2;
// When the label has multiple rows, set the line feed mode. The default unit is words.
Lbl. lineBreakMode = NSLineBreakByTruncatingMiddle; // if it cannot be fully displayed, there will be three dots in the middle
// Set the label highlight status
Lbl. highlighted = YES;
// Set the font color when the label is highlighted
Lbl. highlightedTextColor = [UIColor purpleColor];
// Allows users to interact with tags
Lbl. userInteractionEnabled = YES; // allows user interaction
// Define a click gesture identifier object
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (lblClicked :)];
// Add a gesture identification device to the tag
[Lbl addGestureRecognizer: tap];
// Lbl. enabled = NO;
Lbl. adjustsFontSizeToFitWidth = YES;
// Lbl. baselineAdjustment = UIBaselineAdjustmentAlignCenters;
[Self. view addSubview: lbl]; // The control must be added at the end.
[Summary] You can try the following,