I am trying to use the new Auto Layout in Lion because it seems quite nice. But I can not find good information about how to do things. For example:
I have two labels:
+----------------+
| +------------+ |
| + label 1 | |
| +------------+ |
| |
| +------------+ |
| | label 2 | |
| +------------+ |
+----------------+
but the first label gets not always populated with content, sometimes there ist just no content for it. What I would like to do is to automatically show the label 2 where label 1 would be when it would have content.
+----------------+
| +------------+ |
| + label 2 | |
| +------------+ |
| |
| |
| |
| |
+----------------+
What constrains would I have to add so it works automatically with autolayout? I understand that I could just code everything, but I have about 30 such labels and images and buttons of different styles and shapes which are all optional and I don’t want to add hundrets of lines of code when it could work automatically quite nice too.
If it does not work then I will just use a WebView and do it with HTML and CSS.
I don’t think you could do it that way. If you made the layout for label 2 be based on a distance constraint from label 1, even if you made label 1 auto-collapse to zero height when it has no content, label 2 is still going to be that distance down, ie in:
Where ^ is the autolayout distance constraint – If Label 1 knows how to become zero height when it’s string is empty, you’re still going to get:
Maybe it is possible by creating your NSLayoutConstraint manually. You could make the second attribute be the height of label 1, make the constant zero, and then carefully work out what the multiplier would be to make the distance be what you want based on a multiple of the non-zero label height.
But having done all this, you’ve now coded an NSLabel subclass that auto-sizes, created a constraint object manually instead of via the visual language, and bent NSLayoutConstraint beyond its will.
I think you’re better off just changing the frame of label 2 if label 1’s string is blank!