Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chirone

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
I'm doing this all in code so I don't have to do the same thing twice.

I have two labels and a button.
I want one label to always be 20 points from the top
I want the button to always be 20 points from the bottom
I want the other label to always be 20 points down from the bottom of the label at the top and to be multilined with word wrap.
The problem is that I won't know how big the multilined label should be as the text will keep changing.

How do I use constraints to keep the title from resizing?
If I use the visual constraint:
Code:
:@"V:|-[titleLabel]-[descriptionLabel]-[startButton]-|"
then the descriptionLabel doesn't change height. The titleLabel does.
I can put in a value for the descriptionLabel's height but I don't know what that should be.

I know in the old days I could just call sizeToFit on descriptionLabel to make the right height to fit all the text but I don't know what you do for the constraints.

Anyone got any ideas?

I would also like to add that
Code:
V:|-[titleLabel]-[descriptionLabel(>=20)]-(>=20)-[startButton]-|
gets me a large gap between descriptionLabel and startButton but descriptionLabel doesn't change height to fit the text.

Code:
V:|-[titleLabel]-[descriptionLabel(>=20)]-[startButton]-|
will change the titleLabel's height. I don't know how to restrict it to not change size when I don't know what size it is.
 
Last edited:
Wait, nevermind.
While I was looking for something in the UILabel docs I found the preferredMaxLayoutWidth property. Just had to set that and the constraint with the variable gap between descriptionLabel and startButton and it works the way I want it to.
 
As an aside, you could use a UITextView that is non-editable. Just set it the max possible height and it will display the text at the top. The inability to flush text to the top of a label is often a pain.
 
Indeed I could have and it actually probably wouldn't have been different visually.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.