-1

I have previously worked in Windows phone and see that every control in windows phone has an Auto property, meaning occupy the size of the content.

I see that in iOS such a property does not exist. When there are dynamic data to be bound to a UILabel, I always need to calculate the height of the data and then assign to the UILabel. This takes a good amount of time and bit painful. Is not there an Auto property or am I missing anything here?

Brian
  • 14,610
  • 7
  • 35
  • 43
SRIRAM
  • 137
  • 1
  • 1
  • 7

3 Answers3

1

iOS has AutoLayout which is really helpful, get familiar with it.

  1. Click on the Label
  2. Click on the pin constraints button (little square button)
  3. Add your custom LEFT, RIGHT, TOP margins or LEFT, RIGHT, BOTTOM margins
  4. Click on "Add 3 Constraints"
  5. Set number of Lines to 0 which means as much lines as view needs
  6. Then you probably got warning lines, but you can solve them
  7. Just click on fix constraints button (little triangle button)
  8. Click update frames enter image description here enter image description here

UPDATE

Important: the answer to your question is to PUT NUMBER OF LINES TO 0 you can use that UILabel with 0 lines(which is autosizing) with frames and AutoLayout. AutoLayout is just a friendly suggestion that can be helful to setup views. Also put Line Breaking Word Wrap

Here you go also with some useful links for working with AutoLayout. AutoLayout is great because you don't care anymore what size is the screen, what orientation has the device at that moment. You just need to setup everything correctly and everything works amazingly but if your setup is wrong then AutoLayout might become your enemy. So start learning and experiencing right now.

Very good point to Begin learning AutoLayout
If Your are being lazy, start from video tutorial series
Great iOS7+ table view tutorial with autoresizing cells
Also check out this Stack Overflow discussion

Community
  • 1
  • 1
  • Without using auto layout, by using the old method of springs and structs, can we still achieve label auto resizing by itself. In our project , we are not using auto layout and hence want to know how can label auto resizing be achieved. – SRIRAM Oct 23 '15 at 04:35
  • It's pretty complicated and you will have bad performance. You need to decide if you want your collection view cell to have like 1/3 of the screen then in the cell subclass add those frames or use collection view delegate methods. Anyways i recommend updating your project frames are old and unprofessional – Bagrat Kirakosian Oct 23 '15 at 04:56
  • Thanks for the answer. In my table view I have each cell with different contents. They are not uniform cells. So, I have created one table view cell and added many views in it and each view represents a cell and during run time I show one of the views based on row number as a cell. Can I still apply auto layout for this layout? If yes can you give some tips on this. I assume I need to apply auto layout constraints both in table view cell and also in the xib file that has the table view. Any tips on this would be helpful. – SRIRAM Oct 23 '15 at 05:26
  • @NAVEENRAM check out my update. For using autolayout you just need to add 4(leading, trailing, top, bottom) constraints to your table view and then follow my links to get WP Auto effect everywhere. – Bagrat Kirakosian Oct 23 '15 at 06:44
  • I tried using auto layout for table view cells with three labels in the custom table view cell. It works fine for iPhone 6 and iPhone 6 plus but the third label gets cut in iPhone 5,5s and before. what is the reason? – SRIRAM Oct 24 '15 at 22:18
  • It is now working. In table view cell, lets say I have three labels and I hide one of the labels based on some condition. When the label is hidden I do not want to show the extra space that appears when the third label is hidden. how can this be achieved. – SRIRAM Oct 25 '15 at 03:56
  • @NAVEENRAM I already told you :D calculate your height of 2 labels and assign those values in heightForRowAtIndexPath method. iOS has Data Source methods and you need to learn about them. This is stack overflow and not a personal assistance. If the answer satisfies to your question(Auto ui label height) you can mark it as right and then in private chat ask for further assistance – Bagrat Kirakosian Oct 25 '15 at 09:59
0

You need to familiarize yourself with Auto Layout:

Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views.

iOSX
  • 1,270
  • 13
  • 18
0

Just give top, left and right constraints and make label's numberOfLines to 0. That's it. Label's height will resize automatically.

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23