Label does not have time to update depending on the text. When you enter text in a label and then add the width and height of the label to variables, it writes the old label values. How can I solve the problem?

labelHidden.text = "Какой-то текст..." textWidth = labelHidden.bounds.width textHeight = labelHidden.bounds.height 
  • one
    Are you sure that the dimensions of the label change at all after a new text value is assigned? Perhaps resizing occurs when a label is drawn, i.e. on the next frame? - Nick Volynkin
  • And how do I make a delay per frame? - Artem Efimenko
  • I can not imagine, just suggested. - Nick Volynkin
  • Is it UILabel or just Label ? What is the full class name? - Nick Volynkin
  • @IBOutlet weak var labelHidden: UILabel! - Artem Efimenko

2 answers 2

Changing the text UILabel does not change its size. Before writing the width and height, you need to update the dimensions using the sizeToFit method:

 labelHidden.text = "Какой-то текст..." labelHidden.sizeToFit() textWidth = labelHidden.bounds.width textHeight = labelHidden.bounds.height 

    In this case, when you put any object in the storyboard that it doesn’t matter that it is a label or a button, just drag it to the size you want, fix the matter in the constraints and then just change the text size, and if you don’t have time then use the streams

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { // здесь выполняете все манипуляции dispatch_async(dispatch_get_main_queue(), { // здесь меняете текст }); });