Both of these priority attributes are used in the iOS development Auto Layout.

Go to the official account [iOS Development Stack] to learn more SwiftUI, iOS development related content.

Content Hugging Priorities

Content Hugging Priorities: In Auto Layout, when both sub-views are insufficient to fill space, the Content of the sub-views that satisfy the larger attribute value will be preferred, while the sub-views with lower attribute values will be stretched.

Suppose that two horizontal uilabels show “slash programmer” and “www.iosprogrammer.tech”, respectively. Add five constraints to them, of which 2, 3, and 5 are horizontal constraints. Assume that each of the three horizontal constraints has a value of 10.

The size of the text content in the two Uilabels plus the three horizontal constraints is still less than (<) the width of the superview. We’ll see an error in the storyboard, but we can still run the project and see the display.

We can see that the space occupied by the label of “www.iosprogrammer.tech” has increased. At this point, set the label’s Content Hugging Priorities to 750.

The label for “www.iosprogrammer.tech” is just enough to display content, while the label for “slash programmer” takes up more space.

This is how Content Hugging Priorities works, which controls the Content presentation of sub-views that satisfy a larger value than their intrinsics, while stretching sub-views that select at a lower value when their Content seems insufficient to fill space.

Content Compression Resistance Priorities

Content Compression Resistance Priorities: In Auto Layout, when the Content required by a child view exceeds the space of the parent view, the child view with a larger value is displayed first, while the child view with a smaller value is omitted.

Assume that two horizontal uilabels say “slash programmers focus on improving their programming ability” and “the official website of slash programmers is www.iosprogrammer.tech” respectively. Add five constraints to them, of which 2, 3, and 5 are horizontal constraints. Assume that all three horizontal constraints have values of 10.

The text content in both Uilabels is now larger than the width of the superview. We’ll see an error in the storyboard, but we can still run the project and see the display.

The first Label is compressed so that the text inside is omitted; The second label is displayed normally. At this point, we set the Content Compression Resistance Priorities of the first label to be larger than the second label.

You can see that the second label is compressed and the first label is displayed normally.

Content Compression Resistance Priorities: In Auto Layout, when the Content required by a child view exceeds the space of the parent view, the child view with a larger value is displayed first, while the child view with a smaller value is omitted.

Go to the official account [iOS Development Stack] to learn more SwiftUI, iOS development related content.