Jay Zhao
Jay Zhao

Reputation: 956

Auto layout: how to set the hugging & resistance priority for two multi-line UILabel stacked vertically?

I've already implement a custom cell using auto layout like this:

enter image description here

It has 4 parts as you can see: [thumbnail, title, summary, feedName].

I need to layout title & summary beautifully. They're both multi-line UILabels.

What I need to achieve is that when there are too many texts, the summary's line count gets decreased first, then the title's, not like what the picture is showing (the title only occupies one line).

I've tried setting the hugging and resistance priorities like this, but it's not working, any ideas?

title:

enter image description here

summary:

enter image description here

Upvotes: 2

Views: 835

Answers (1)

Jay Zhao
Jay Zhao

Reputation: 956

Seems I need to always set the preferredMaxLayoutWidth for a multi-line label, even its width can be calculated by constraints to surrounding views.

By setting this, it's working perfectly now.

self.titleLabel.preferredMaxLayoutWidth = maxLayoutWidth;
self.summaryLabel.preferredMaxLayoutWidth = maxLayoutWidth;

enter image description here

Upvotes: 5

Related Questions