UILabel Related Property Settings

stay iOS UILabel is a commonly used control in programming. Here is a way to set the relevant properties of UILabel.

Many iOS 6 programming starts with storyboard, and when UILabel is used, controls are dragged into storyboard to generate implementations. If you want to use code in - (void)viewDidLoad, such as [_label]. InititWithFrame: The CGRectMake (X, Y, WIDTH, HEIGHT)] method does not work by changing the label size dragged onto storyboard, because the program loads with the code of -(void)viewDidLoad first, and then loads storyboard. Storyboard generates label according to the size set when dragging the control, that is, it overrides the label in -(void) viewDidL. The size set in oad, so to dynamically set the size of label, you should use code to create UILabel.

  1. UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(5050200400)];  
  2. [self.view addSubview:label];  
This implements label creation with code, where initWithFrame sets the location and size of label, where CGRectMake can declare CGRect rect =CGRectMake(50,50,200,400) separately and place variable rect after method initWithFrame to realize four values of CGRectMake representing the location coordinate x value of rect, coordinate y, coordinate y respectively. Value, width, height. The second sentence [self.view addSubview:label] is to add a sub-view in the current view self.view by calling the addSubview method, and paste it like a sticker. The phenomenon that the former sub-view is covered by the latter sub-view because of the different order of adding, which is also mentioned later when adding background map to label. It will be mentioned.

Or use the following code to change the size of label

  1. label.frame = CGRectMake(974722319);  
Setting label tag s

  1. label.tag =101;   
Setting up the text content of label

  1. label.text =@"abcd"  perhaps  
  2. NSString *labelText = @"abcd";  
  3. label.text = labelText;  
Assign the value of the string to label
Setting the text type and size of label

  1. label.font = [UIFont systemFontOfSize:12];//Set size by default text  
  2. label.font = [UIFont fontWithName:@"Arial" size:30];//Setting text type and size  

Setting the text color of label

  1. label.textColor = [UIColor lightGrayColor];//Where textColor uses UIColor type  

Setting text alignment

  1. label.textAlignment = NSTextAlignmentLeft;  

textAlignment has three settings: NSTextAlignment Left is left aligned, NSTextAlignment Center is center aligned, and NSTextAlignment Right is right aligned.

If some articles are introduced with UITextAlignment Center/UITextAlignment Left/UITextAlignment Right, that's the previous usage of iOS 6, and the latest usage of iOS 6 has changed.

When the text content is too much and label can't display it all, label will replace the text content with ellipsis. Here's how to set up the ellipsis mode of label text.

  1. label.lineBreakMode =NSLineBreakByCharWrapping;//Where the lineBreakMode optional value is  
  2. linBreakMode enum{  
  3. NSLineBreakByWordWrapping = 0,//Keep the whole word, with the space as the boundary  
  4.    NSLineBreakByCharWrapping,//Keep the whole character  
  5.    NSLineBreakByClipping,//To the border  
  6.    NSLineBreakByTruncatingHead,//To begin with an ellipsis, replace it with an ellipsis sign  
  7.    NSLineBreakByTruncatingTail,//Ellipse the end and replace it with an ellipsis sign  
  8.    NSLineBreakByTruncatingMiddle//Ellipse the middle and replace it with an ellipsis sign  
  9.    }  
Set the number of lines of text

  1. label.numberOfLines = 1;//Set the number of rows to 1. If not, the default number of rows will be 1.  
numberOfLines=0 can be used when the number of rows to be set is unlimited
When the label size is adjusted using the sizeToFit method, the values stored in the attribute are taken into account. For example, if this property is set to 3, the sizeToFit method adjusts the label to make it large enough to display three lines of text.
  1. [label sizeToFit];  
Realization of multi-line text display
  1. commentTextLabel.lineBreakMode = NSLineBreakByCharWrapping;  
  2. commentTextLabel.numberOfLines = 0;  
Text automatically adjusts font size according to label size

  1. label.numberOfLines =1;  
  2. label.adjustsFontSizeToFitWidth =YES;  
The adjustFontSizeToFitWidth method can automatically adjust the font size according to the label size until the text size reaches the maximum and minimum size of the label text and the maximum and minimum value of the string. If this method is used, a big limitation is that only when numberOfLines is set to 1. Ability to use


If the number of lines is more than one line, there is no adaptive system method to realize the function of automatically adjusting font size. It can only be realized by code. Because the actual size of mobile screen is limited, if the font is too small, it will affect the user experience, so it is necessary to set a minimum font size. To make a judgment, abbreviations should be used for smaller than the smallest font size. The following code mainly uses abbreviations.
  1. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100180) lineBreakMode:NSLineBreakByCharWrapping];  

To get the height of the font under a certain font size and judge whether the height of the font is the same as that of the label. Text is the text content of the input label. sizWithFont sets the font, constrained ToSize sets the rectangular size parameters of the constrained text. The width of the constrained text should be the same as that of label, and the height setting should be high enough, much higher than that of label, otherwise it will be. There is a problem of incomplete text display. The role of lineBreakMode has been discussed above. If the calculated height exceeds the label height, the font size is reduced in a circular manner until the height matches and then jumps out of the circle.

  1. float maxHeight =50;//Set maximum height  
  2.     float minFontSize =9;  
  3.     float height;  
  4.     int fontSize = 31;//Set the maximum font size  
  5.     NSString *text = @"Input text content";  
  6.     do {  
  7.         fontSize = fontSize - 1;  
  8.             UIFont *font =[UIFont fontWithName:@"Arial" size:fontSize];  
  9.             CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100180)/*The width is the same as the width of label, and the height should be higher than the height of label.*/ lineBreakMode:NSLineBreakByCharWrapping];  
  10.         height = size.height;  
  11.         NSLog(@"height=%f,fontSize=%d,text=%@",height,fontSize,text);  
  12.     } while (height > maxHeight&&fontSize>minFontSize);  
  13.   
  14.     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(505010050)];  
  15.     label.text =text;  
  16.     if (fontSize ==9) {//Determine whether the font is smaller than the smallest font size and use the default system abbreviation when the font is smaller than the smallest font size  
  17.         label.font = [UIFont fontWithName:@"Arial" size:15];  
  18.     }  
  19.     else{  
  20.     label.font = [UIFont fontWithName:@"Arial" size:fontSize];  
  21.     label.lineBreakMode = NSLineBreakByCharWrapping;//Realizing multi-line display of text  
  22.     label.numberOfLines = 0;  
  23.     }  
  24.     [self.view addSubview:label];  
Automatically adjust label height according to the number of text
In fact, the above method is used to get a high degree of regeneration label.

  1. NSString *text =[[NSString alloc]init];  
  2.   text = @"Input text content";  
  3.   CGSize size = CGSizeMake(280180);  
  4.   UIFont *fonts = [UIFont systemFontOfSize:14.0];  
  5.   CGSize msgSie = [text sizeWithFont:fonts constrainedToSize:size lineBreakMode: NSLineBreakByCharWrapping];  
  6.   UILabel *textLabel  = [[UILabel alloc] init];  
  7.   [textLabel setFont:[UIFont boldSystemFontOfSize:14]];  
  8.   textLabel.frame = CGRectMake(20,70280,msgSie.height);  
  9.   textLabel.text = text;  
  10.   textLabel.lineBreakMode = NSLineBreakByCharWrapping;//Realizing multi-line display of text  
  11.   textLabel.numberOfLines = 0;  
  12.   [self.view addSubview:textLabel];  
Set the border thickness and color of label, and add #import <QuartzCore/QuartzCore.h> to the corresponding file before setting it.

  1. label.layer.borderColor = [UIColor lightGrayColor].CGColor;//Border color, CGColor  
  2. label.layer.borderWidth = 1;//Border width  
Setting the background color of label

  1. label.backgroundColor =[UIColor yellowColor];  
Setting label background map
There are two ways to set up the background map. The first method is introduced below.
Setting the background map can put a picture of the same size as label on the back layer of label, and then set the background of label to be transparent, so that label has a background.

  1. UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(5050200400)];  
  2. UIImageView *imageView =[[UIImageView alloc]init];  
  3. imageView.frame =CGRectMake(5050200400);  
  4. UIImage *image=[UIImage imageNamed:@"1.jpg"];  
  5. imageView.image =image;//The imageView changes the size of the added image according to its size, so no additional image settings are required.  
  6. label.backgroundColor = [UIColor clearColor];  
  7. label.text =@"hello world";  
  8. label.font = [UIFont systemFontOfSize:30];  
  9. label.textColor = [UIColor yellowColor];  
  10. [self.view addSubview:imageView];//You can't add in the wrong order, otherwise the picture will overwrite label.  
  11. [self.view addSubview:label];  
This is a bit of an unorthodox approach. Here's a more standard second approach: Setting up pictures with UIColor, and then using UIColor as background color, you can achieve label setting of background images.

  1. UIColor * color = [UIColor colorWithPatternImage:image];//Image is a background image to be added  
  2.  UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(5050100200)];  
  3.  [label setBackgroundColor:color];  
  4.  [self.view addSubview:label];  
But this method has a serious defect, that is, when the size of background image is not consistent with the size of label, the background image will be partially intercepted or overlapped. Therefore, a more perfect method is to modify the size of background image to be consistent with the size of label before setting the background color. You can use the following functions to set the image size

  1. -(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{  
  2.     UIImage *i;  
  3.     //Create a bitmap context and set it to the context currently in use  
  4.     UIGraphicsBeginImageContext(itemSize);  
  5.     CGRect imageRect=CGRectMake(00, itemSize.width, itemSize.height);  
  6.     //Draw resizing pictures  
  7.     [img drawInRect:imageRect];  
  8.     //Create a resized image from the current context  
  9.     i=UIGraphicsGetImageFromCurrentImageContext();  
  10.     //Make the current context out of the stack  
  11.     UIGraphicsEndImageContext();  
  12.     //Return the new size-changed image  
  13.     return i;  
  14. }  
Then it can be called in the main function.

  1. CGSize size= CGSizeMake(100200);  
  2.     UIImage *image =[UIImage imageNamed:@"1.jpg"];  
  3.     UIImage *laterImage =[self scaleImage:image ToSize:size];  
  4.     UIColor * color = [UIColor colorWithPatternImage:laterImage];  
  5.    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(5050100200)];  
  6.    [label setBackgroundColor:color];  
  7.    [self.view addSubview:label];  
Setting highlights

  1. label.highLighted =YES;  
Setting Text Shadows

  1. label.shadowColor =[UIColor grayColor];  
Set Shadow Size

  1. label.shadowOffset = CGSizeMake(2.02.0);  
Setting label rounded corners

  1. label.layer.cornerRadius = 10;  
To use this setting, add #import <QuartzCore/QuartzCore.h> to the header file.
top 2

Keywords: iOS Programming Attribute Mobile

Added by Hafkas on Mon, 15 Jul 2019 23:21:40 +0300