ios dynamically sets the height of the lbl text label

  • 2020-05-07 20:30:29
  • OfStack


 txtlbl.font = [UIFont boldSystemFontOfSize:14.0f]; 
   txtlbl.numberOfLines = 0; 
NSString *str = @"        A side decided to set up a special network to leave the TV drama w Well, two continents have lost their high-tech Japan i Countdown to leave I said the teacher KFC frank sa jiangdong fathers will fiddler said a side decided to set up technology special network to leave TV sub w Well, two continents have lost their high-tech Japan i Countdown to leave me said teacher KFC frank sa jiangdong father will fidler said ";
   CGSize size = [str sizeWithFont:txtlbl.font constrainedToSize:CGSizeMake(txtlbl.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
   // Reset according to the calculation result txtlbl The size of the
   [txtlbl setFrame:CGRectMake(7, 0, 310, size.height)];
   txtlbl.text = str;

Method 2:


  // iOS7_API_ According to the word Dynamic determination of word count Label Wide high
    // Set up the Label The font HelveticaNeue  Courier
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:24.0f];
    _nameLabel.font = fnt;
    // According to the font NSString The size of the
    CGSize size = [_nameLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    // The name of the H
    CGFloat nameH = size.height;
    // The name of the W
    CGFloat nameW = size.width;
    _nameLabel.frame = CGRectMake(0, 0, nameW,nameH);

Method 3:


    // The width of the W
    CGFloat contentW = self.bounds.size.width - _content.frame.origin.x - kMargin;
    // label The font HelveticaNeue  Courier
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f];
    _content.font = fnt;
    _content.numberOfLines = 0;
    _content.lineBreakMode = NSLineBreakByWordWrapping;
    // iOS7 Replace the outdated with the following methods iOS6 In the sizeWithFont:constrainedToSize:lineBreakMode: methods
    CGRect tmpRect = [_content.text boundingRectWithSize:CGSizeMake(contentW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];
   
    // highly H
    CGFloat contentH = tmpRect.size.height;
    NSLog(@" Adjust the display width :%f, According to height :%f"contentW,contentH);
    _content.frame = CGRectMake(0, 0, contentW,contentH);


Related articles: