简体   繁体   中英

Get dynamic UILabel's height

I would like to catch the height of a UILabel called descriptionText . I could log it with the below code, but the problem is it's not the data I want. It's only the size of the basic label with 1 line and the text has 3 (or more) lines. I'm using auto layout and it works, I can display the text with dynamic height, but I need to know its real height because of an other bug.

Here's my try that gives the basic height of the label:

- (void)viewDidLoad {

      [super viewDidLoad];
      NSLog(@" description height = %f", self.descriptionText.frame.size.height); 
}

Is it possible to get the somehow the real height of a UILabel that has been rendered in the screen?

As an alternative I could calculate the desired values with the count of the lines.

As you probably realize viewDidLoad was not right place to put the log in. viewDidLoad is called after view is loaded into memory, but no layout has been performed yet, so the frame of your labels reflects what's originally set in the Storyboard file.

If you want to check frames after layout has been complete you need to override viewDidLayoutSubviews , which will be called after layout is complete and all frames are set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM