UILabel of UI -- attributes and usage, uiuilabel -- Usage

Source: Internet
Author: User

UILabel of UI -- attributes and usage, uiuilabel -- Usage

1 // initialize the Label and set the location and size of the label. 2 UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50,100,100, 40)]; 3 4 // set the label position and size 5 label. frame = CGRectMake (50,100,100, 40 );

// If the enabled attribute is set to No, the text color is dimmed, indicating that it is unavailable. The default value is YES.

Label. enabled = YES;

6

7 // set the background color 8 label. backgroundColor = [UIColor yellowColor]; 9 10 // clear the background color 11 label. backgroundColor = [UIColor clearColor]; 12 13 // set the highlighted 14 label. highlighted = YES; 15 16 // set to hide 17 labels. hidden = NO; 18 19 20 // set text shadow 21 label. shadowColor = [UIColor grayColor]; 22 23 // set the shadow size 24 label. shadowOffset = CGSizeMake (2.0, 2.0); 25 26 // set the rounded corner 27 label. layer. cornerRadius = 10; 28 29 // set the Border Width and color of the label, Before setting, add # import <QuartzCore/QuartzCore to the corresponding file. h> 30 // set the Border Width 31 label. layer. borderWidth = 1; 32 33 // set the border color 34 label. layer. borderColor = [UIColor redColor]. CGColor; 35 36 // set label 37 label. tag = 1; 38 39 // set the text content 40 label. text = @ "abcd"; 41 42 // assign the string value to label 43 NSString * textLabel = @ "abce"; 44 label. text = textLabel; 45 46 // set text type and text size 47 label. font = [UIFont fontWithName: @ "Arial" size: 30]; 48 // [Label setFont: [UIFont fontWithName: @ "Arial" size: 30]; 49 50 // use the default text type to set the size of 51 labels. font = [UIFont systemFontOfSize: 12]; 52 // [label setFont: [UIFont systemFontOfSize: 12]; 53 54 // set text color 55 label. textColor = [UIColor redColor]; 56 // [label setTextColor: [UIColor lightGrayColor]; 57 58 // sets the text alignment mode 59 label. textAlignment = NSTextAlignmentLeft; 60 // [label setTextAlignment: NSTextAlignmen TLeft]; 61 62/* among them, textAlignment has three setting methods: NSTextAlignmentLeft is left-aligned, and NSTextAlignmentCenter is center-aligned, NSTextAlignmentRight is right aligned 63 if some articles use UITextAlignmentCenter/UITextAlignmentLeft/UITextAlignmentRight, which was previously used by iOS6, the latest usage of iOS6 has been changed */64 65/* when there are a lot of text content and the label cannot be all displayed, the label will replace the text content with a ellipsis, 66. The label text is omitted */67 // The value of lineBreakMode is 68 [label setLineBreakMode: NSLineBreakByCharWrapping]; 69 // label. lineBr EakMode = NSLineBreakByCharWrapping; 70 // linBreakMode enum {71 // NSLineBreakByWordWrapping = 0, // keep the entire word with a space at the boundary 72 // NSLineBreakByCharWrapping, // keep the entire character 73 // NSLineBreakByClipping, // start with 74 until the boundary // NSLineBreakByTruncatingHead, // omit, replace 75 with a ellipsis // NSLineBreakByTruncatingTail, // omit the end, replace the ellipsis 76 // NSLineBreakByTruncatingMiddle // in the middle, and replace the ellipsis 77 //} 78 79 // set the number of lines in the text 80 label. numberOfLines = 1; // if this parameter is not set, the system will Recognize the number of rows as 1; 81 82/* when the number of rows to be set is unlimited, use numberOfLines = 0; To achieve this, the line feed will automatically be 83. When the label size uses the sizeToFit method, the value stored in this attribute will be taken into account when you adjust the size. 84. For example, if this attribute is set to 3, The sizeTOFit method will adjust the label to make it large enough to display three lines of text */85 [label sizeToFit]; 86 87 // display 88 label in multiple lines of text. lineBreakMode = NSLineBreakByCharWrapping; 89 label. numberOfLines = 0; 90 91 // The text is automatically adjusted according to the label size 92 label. numberOfLines = 1; 93 label. adjustsFontSizeToFitWidth = YES; 94/* adjustFontSizeToFitWidth can automatically adjust the font size of text according to the label size, until the size of the text reaches the maximum size and minimum value of the label text set by the user, and the maximum and minimum value of the string, there is a large limit only in numberOfL When ines is set to 1, */95 96/* can be used. If the number of rows exceeds 1, to automatically adjust the font size, there is no adaptive system method available, you can only use the code to implement it. In the design, the actual size of the mobile phone screen must be limited. If the font size is too small, the user experience may be affected. Therefore, you must set a minimum font size, the minimum font size is reduced. The following code mainly uses: 97 98 CGSize size = [label sizeWithFont: font constrainedToSize: CGSizeMake (100,180) lineBreakMode: NSLineBreakByCharWrapping]; 99 100 to get the height of the font under a certain word, and determine whether it is consistent with the label height. text is the text content of the input label, and sizWithFont sets the font, constrainedToSize: Specifies the rectangle size parameter of the constrained text. The width must be the same as that of the label. The height setting should be high enough, which is much higher than the label. Otherwise, the text display will be incomplete. The role of lineBreakMode is described above. If the calculated height exceeds the label height, the font size is reduced cyclically until the height complies with the label height. 101 102 float maxHeight = 50; // set the maximum height to 103 float minFontSize = 9; 104 float height; 105 int fontSize = 31; // set the maximum font size of 106 NSString * text = @ "input text content"; 107 do {108 fontSize = fontSize-1; 109 UIFont * font = [UIFont fontWithName: @ "Arial" size: fontSize]; 110 // The width is the same as that of the label, and the height should be higher than the label height 111 CGSize size = [text sizeWithFont: font constrainedToSize: CGSizeMake (100,180)] lineBreakMode: NSLineBreakByCharWrapping]; 112 heig Ht = size. height; 113 NSLog (@ "height = % f, fontSize = % d, text = % @", height, fontSize, text ); 114} while (height> maxHeight & fontSize> minFontSize); 115 116 UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,100, 50)]; 117 label. text = text; 118 if (fontSize = 9) {// determines whether the font size is smaller than or equal to the minimum font size. if the font size is smaller than the minimum font size, the system displays the 119 label by default. font = [UIFont fontWithName: @ "Arial" size: 15]; 120} 121 else {122 label. font = [UIFont fontW IthName: @ "Arial" size: fontSize]; 123 label. lineBreakMode = NSLineBreakByCharWrapping; // The 124 label is displayed for multiple lines of text. numberOfLines = 0; 125} 126 [self. view addSubview: label]; 127 */128 129 // set the background image 130/* method 1. Set the background image to put a picture of the same size as the label on the next layer of the label, 131 then set the background of the label to transparent, so that the label has a background 132 UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 133)]; 134 UIImageView * imageView = [[UIImageView alloc] ini T]; 135 imageView. frame = CGRectMake (50, 50,200,400); 136 UIImage * image = [UIImage imageNamed: @ "1.jpg"]; 137 imageView. image = image; // imageView will change the size of the added image Based on its size, so you do not need to set the image138 label. backgroundColor = [UIColor clearColor]; 139 label. text = @ "hello world"; 140 label. font = [UIFont systemFontOfSize: 30]; 141 label. textColor = [UIColor yellowColor]; 142 [self. view addSubview: imageView]; // The Order of adding cannot be incorrect. Otherwise, the image will overwrite labe. L143 [self. view addSubview: label]; */144 145/* method 2. Use UIColor to set the image and use UIColor as the background color, you can set the background image 146 147 UIColor * color = [UIColor colorWithPatternImage: image]; // The image is the background image to be added. 148 UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,100,200)]; 149 [label setBackgroundColor: color]; 150 [self. view addSubview: label]; 151 152 but this method has a serious defect, that is, when the size of the background image is different from that of the label, if the background image is partially captured or tiled, 153 In a better way, you must first modify the size of the background image to be consistent with that of the label, and then set the background color. You can use the following function to set the image size to 154-(UIImage *) scaleImage :( UIImage *) img ToSize :( CGSize) itemSize {155 UIImage * I; 157 // create a bitmap context and set it to the currently used context158 UIGraphicsBeginImageContext (itemSize); 159 CGRect imageRect = CGRectMake (0, 0, itemSize. width, itemSize. height); 160 // draw a 161 [img drawInRect: imageRect]; 162 // create an image 163 I = UIGraphicsGetImageFromCurrentImageContext () from the current context (); 164 // make the current context out of the stack 165 UIGraphicsEndImageContext (); 166 // return the image 167 return I after the new size change; 168} 169 170 and then call it in the main function: 171 172 CGSize size = CGSizeMake (100,200); 173 UIImage * image = [UIImage imageNamed: @ "1.jpg"]; 174 UIImage * laterImage = [self scaleImage: image ToSize: size]; 175 UIColor * color = [UIColor colorWithPatternImage: laterImage]; 176 UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,100,200)]; 177 [label setBackgroundColor: color]; 178 [self. view addSubview: label]; */179 180 // text baseline 181 label. baselineAdjustment = UIBaselineAdjustmentNone; // align the lowest-end text with the label midline by 182 label. baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // The top of the text is aligned with the midline 183 label. baselineAdjustment = UIBaselineAdjustmentAlignCenters; // align the text midline with the label midline 184 185 // automatically contract 186 label. minimumScaleFactor = 0.5; 187 188 // Add view 189 [self. view addSubview: label];

 

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.