What Is Intrinsic Content Size In The Interface Builder?

Intrinsic content size is defined as the natural amount of space a view will take. Not all views have intrinsic content sizes.

For example, a UILabel has the intrinsic content size of its text and some margin. If you changed the text, the font size or the font style, the intrinsic content size will change.

Intrinsic content size is important because it allows views to determine their ideal height and width without us having to manually define it. This is especially useful in text views, for example in a messaging app.

Here is a list of views that have intrinsic content sizes:

  • Sliders
  • Labels
  • Buttons
  • Switches
  • Textfields
  • Textviews
  • Imageviews

Note that UIViews (or NSViews) do not have intrinsic content sizes.

Auto layout will use a views intrinsic content size to correctly size the view. However, there are times when a view doesn’t have enough space or has too much space. What should Auto Layout do then?

There are two concepts called Content Compression Resistance and Content Hugging that determine the size in these scenarios.

Content Compression Resistance

This sets the priority of how much a view resists being made smaller than its intrinsic size. If we set this to a higher value, that means we don’t want this view to shrink smaller than its intrinsic content size.

For example, if a label had a width constraint that squeezed forced some of the text to be cut off, we could fix this by adding a content compression resistance that has a higher priority than its width constraint. This will ensure the text is always visible and that the label will expand to fit the text.

Content Hugging

This sets the priority of how much a view resists being made larger than its intrinsic size. If we set this to a higher value that means we don’t want this view to grow larger than its intrinsic content size.

For example, a label had a width constraint that makes it take up the whole width of the screen despite it only having one word in it. To make this label’s width constrain to its text, we could make its content hugging priority higher than the width constraint priority.

This way, the label’s width will be whatever its intrinsic content size is. However, it still has the option to expand up to the whole screen width if there’s more text.

If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts