UILabel Intrinsic Size
suggest changeWe have to create a view which will have an image prefix to a text. The text could be of variable length. We have to achieve a result where the image + text is always in the center of a parent view.
Step 1: First create a single view project and name it something of your choice and open the storyboard’s first view. Drag a view with some reasonable size and set its background color to yellow. I have resized my view controller to 3.5″. The resultant view should look something like this
Step 2: Now we will add constraints to the yellow view. To begin with we will add width and height constraints (Wait a minute, didn’t we say that the view will have dynamic width? Ok, we will get back to it later). Add the following constraints as in the image below. Do not bother with the width value; any value will do just fine for width. Just keep it large enough so that we can add the constraints properly.
After adding these two constraints you will see that Xcode is giving you errors as in the image below. Let’s see them and understand them.
We have two errors (red means error). As discussed above, let’s revisit the ambiguity part.
Missing Constraints : Need constraints for : X position :- As discussed above we have given the view a width and a height so its “BOUNDS” is defined but we have not given its origin so its “FRAME” is not defined. Autolayout is not able to determine what will be the X position of our yellow view
Missing Constraints : Need constraints for : Y position :- As discussed above we have given the view a width and a height so its “BOUNDS” is defined but we have not given its origin so its “FRAME” is not defined. Autolayout is not able to determine what will be the Y position of our yellow view. To solve this we have to give Auto Layout something to resolve X and Y. Since we cannot set frames we will do it the Auto Layout way. Add the following constraints as in the image given below. I will explain it later.
What we have done is, we have added a “Vertical Center” and “Horizontal Center”. These constraints tell Auto Layout that our yellow view will always be in the center horizontally, so X is determined. The same is true of the vertical constraint, and Y is determined. (You might have to adjust the frame.)
Step 3: By now our base yellow view is ready. We will add the prefix image as a subview of our yellow view with the following constraints. You can choose any image of your choice.
Since we have a fixed size for our prefix image, we will have a fixed width and height for this image view. Add the constraints and proceed to the next step.
Step4: Add a UILabel as the subview of our yellow view and add the following constraints.
As you can see I have given only relative constraints to our UILabel. It’s 8 points from the prefix image and 0 from the top, trailing, and bottom of the yellow view. Since we want the width to be dynamic we will not give width or height constraints.
Q: Why are we not getting any errors now, when we have not given any width and height? Ans: We get an error or warning only when Auto Layout is not able to resolve anything that is required to render a view on screen, be it height, width, or origin. Since our label is relative to the yellow view and the prefix image and their frames are well defined, Auto Layout is able to calculate the frame of our label.
Step 5: Now if we recall we will realize that we have given a fixed width to our yellow view, but we want it to be dynamic, depending on the text of our label. So we will modify the width constraint of the yellow view. That width is necessary to resolve ambiguity, but we want it to be overruled at runtime based on the content of the UILabel. So we will select our yellow view and go to the Size inspector and reduce the priority of the width constraint to 1 so that it will be overruled. Follow the image given below.
Step 6: We want our UILabel to expand according to the text and push our yellow view. So we have reduced the priority of the yellow view’s width. Now we will increase the content-compression-resistance priority of our UILabel. We want our view to shrink as well, so we will increase the content-hugging priority of the UILabel. Follow the image below.
As you can see we have increased the content-hugging priority to 500 and the compression-resistance priority to 751, which will successfully overrule the width constraint’s priority of 1.
Now build and run. You will see something like the following.