-1

I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization. So I tried to get the height of the UILable by the following method.

uiLable.bounds.size.height/ uilable.frames.size.height

This method was implemented in both viewDidAppear and viewDidLoad methods. In both methods, the height returned is same on all devices and when the number of lines increases/decreases. Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text (Remember for all meetings and These can be changed in settings).

enter image description here

Any help would be appreciated. Thanks in Advance

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
  • https://stackoverflow.com/questions/7174007/how-to-calculate-uilabel-height-dynamically –  Jun 13 '18 at 18:45
  • When are no of lines increased or decreased? In viewdidload? – HAK Jun 13 '18 at 18:45
  • "In both methods, the height returned is same on all devices and when the number of lines increases/decreases." Why should we believe that the height is different on different devices? Maybe the height returns is _right_. – matt Jun 13 '18 at 19:21
  • @matt When the device language is changed the text is translated to the corresponding system language. As a result the number of lines increases based on the device size. In the case of iPhone 5c number of lines will be 4 and in case of other devices it would be 3 and if the language is English it would be a single line – Vishnu M Menon Jun 14 '18 at 04:03
  • Very well. And why do you need to know the height? If you use auto layout, which you really _must_ do if you are internationalizing, the label will just increase or decrease its height automatically, and other views will move accordingly. – matt Jun 14 '18 at 04:13
  • The main thing is that there is a switch button right next to this Label which should be aligned to the center of the label @matt – Vishnu M Menon Jun 14 '18 at 04:16
  • Then align it. That is what auto layout is for. It will remain aligned no matter how tall the label becomes. That's the whole point. You should not be _measuring_ anything. You should be using auto layout. – matt Jun 14 '18 at 04:18

3 Answers3

1

After updating the text in your label you can force the layout system to update its UI and then get the correct bounds of your label.

// set your label text
label.text = "multiline text here"

// Now force layout system to update its UI instantly
self.view.setNeedsLayout()
self.view.layoutIfNeeded()

// Now you will get the correct bounds
let bounds = label.bounds
HAK
  • 2,023
  • 19
  • 26
0

I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization.

Do not try to do that. You have no reason to want to know it.

Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text

Then align it with auto layout. That is exactly what it is for. Let the runtime take care of the problem for you. You cannot possibly internationalize successfully without using auto layout to take care of this sort of thing.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can place both the label in a single view and set autolayout as verticalCenter of view is equal to vertical center of toggle button.

Anamika
  • 56
  • 5