IOS stage study 26th days note (UILabel Introduction), 26th days uilabel
IOS Learning (UI) knowledge point sorting
I. Introduction to UILabel
1) concept: UILabel is a control inherited from UIView for displaying text information.
2) All controls in the UI are inherited from the UIView, that is, the UIView is the ancestor class of the UI.
3) Code of the UILable instantiation method:
1 UILabel * label = [[UILabel alloc] init]; // initialize UILabel 2 label. text = @ "Hello, KingKong"; // assign text content 3 label to the label. backgroundColor = [UIColor blackColor]; // set the lable background color to black 4 [label setTextColor: [UIColor whiteColor]; // set the text font color to white 5 label. font = [UIFont systemFontOfSize: 15.0]; // set the label font size to 6 abel. frame = CGRectMake (20, 60,200, 40); // you can specify the x, y, and length and height of the label. window addSubview: label]; // load the initialized label to the screen view.
4) The View color can be set based on the RGB type. You can use the color software to obtain the RGB value of the desired color. You can easily set any color type.
For ease of use, we can write a special macro definition method code based on RGB color settings as follows:
1 #define ColorWithRGB(r,g,b) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1])
5) We can also use NSMutableAttributedString to set the style instance code of the control:
1 UILabel * label = [[UILabel alloc] init]; // initialize UILabel 2 // initialize consumer object 3 NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString: text]; 4 // set the font size by range 5 [attribute addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 30] range: NSMakeRange (0, 1)]; 6 // set the background color based on the range. 7. [attribute addAttribute: NSBackgroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange (0, 1)]; 8 // set the font color according to the range 9 [attribute addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange (0, 1)]; 10 // adjust the font size of the text according to the label width 11 label. adjustsFontSizeToFitWidth = YES; 12 label. numberOfLines = 0; // The maximum number of rows until the label width and height are fully filled with 13 labels. lineBreakMode = NSLineBreakByTruncatingTail; 14 // set the text color based on RGB. The custom label is used here. textColor = ColorWithRGB (255,154,155); 16 // set the text to center 17 // UITextAlignmentCenter18 label before iOS 7. textAlignment = NSTextAlignmentCenter; 19 // set the Border width of the label to 20 labels. layer. borderWidth = 1; 21 // set the border color 22 label. layer. borderColor = label. textColor. CGColor; 23 // set the rounded corner (when the image is square and the rounded corner value is half of the image, the image changes to a circle) 24 label. layer. cornerRadius = 6; 25 // shadow of label text 26 label. shadowColor = [UIColor blackColor]; 27 // laebl text offset 28 label. shadowOffset = CGSizeMake (10, 10); 29 // assign the set style to the control 30 label. attributedText = attribute; 31 // load control 32 [self. window addSubview: label];
6) when we use a custom view page instead of the iOS Main. storyboard, We need to delete the Main storyboard file base name of the Info option under the Target.
Otherwise, an error is reported.
7) When we cannot select the iOS simulator required for project running, we can find the Deployment Target item in the General of Target and set the corresponding iOS version.
8) the iOS UI Project Creation option is generally a Single View Application item.
9) When initializing the control object, we must first initialize the view Code as follows:
1 // initialize the view object [UIScreen mainScreen]. bounds automatically obtains the size of the screen 2 self. window = [[UIWindow alloc] initWithFrame: [UIScreenmainScreen]. bounds]; 3 // set the view background color 4 self. window. backgroundColor = [UIColor whiteColor]; 5 // set the current window to the main window and display it. Note: This item must be set; otherwise, view 6 [self. window makeKeyAndVisible];
10) What is Frame? Frame is the coordinate system. The first parameter is the x coordinate, the second parameter is the y coordinate, the third parameter is the width, and the fourth parameter is the height.
1 CGRect frame = CGRectMake(10, 0, 100, 50);
11) obtain the width, height, x coordinate, and y coordinate of the screen or view.
1 CGRect mainScreenFrame = [UIScreen mainScreen]. bounds; 2 // obtain the screen width. 3 CGFloat screenWidth1 = mainScreenFrame. size. width; 4 // obtain the view width 5 CGFloat screenwid2= CGRectGetWidth (self. view. frame); 6 // obtain the screen height 7 CGFloat screenHeight1 = mainScreenFrame. size. height; 8 // obtain the height of the view 9 CGFloat screenHeight2 = CGRectGetHeight (self. view. frame); 10 // obtain the X coordinate of the screen 11 CGFloat startX = mainScreenFrame. origin. x; 12 // obtain the Y coordinate of the screen 13 CGFloat startY = mainScreenFrame. origin. y;
12) Description of the PCH file: the PCH file is a pre-compiled file in iOS that can be used to reference some of the leading files and macro definition methods,
In this way, you can implement a global reference. After adding the PCH file, you need to find it under the Build Settings item of Target.
Set the Prefix Header to $ (SRCROOT)/Target name/PCH file name. pch
13) necessary procedures for creating controls
1. Control Initialization
2. Set Frame
3. Set the background color.
4. Load controls on a specific view
Optional
* Can I set text?
* Can I set the text color?
*......