Trying to set auto constraint on label, failing on long text

226 views Asked by At

I'm trying to display rows containing a thumbnail and a title.

It works great for short title strings, but for long strings, it breaks. Rather than expanding horizontally, I would prefer it to expand vertically. Checkout the screenshot below to see what happens when a long string is used. I'm using PureLayout to manage Auto Layout:

thumbnailView.autoSetDimensions(to:CGSize(width: 75, height: 75))
thumbnailView.autoPinEdge(toSuperviewEdge: .leading, withInset: kLabelHorizontalInsets)
thumbnailView.autoPinEdge(toSuperviewEdge: .top, withInset: kLabelVerticalInsets)
thumbnailView.autoPinEdge(toSuperviewEdge: .bottom, withInset: kLabelVerticalInsets, relation: .greaterThanOrEqual)

titleLabel.autoPinEdge(.leading, to: .trailing, of: thumbnailView, withOffset: kLabelHorizontalInsets, relation: .lessThanOrEqual)
titleLabel.autoPinEdge(toSuperviewEdge: .top, withInset: kLabelVerticalInsets)
titleLabel.autoPinEdge(toSuperviewEdge: .trailing, withInset: kLabelHorizontalInsets)
titleLabel.autoPinEdge(toSuperviewEdge: .bottom, withInset: kLabelVerticalInsets)

The appropriate settings on the UITextLabel are also set:

 titleLabel.lineBreakMode = .byWordWrapping
 titleLabel.numberOfLines = 0
 titleLabel.textAlignment = .left

Any suggestions are appreciated. Thanks tableview screenshot

2

There are 2 answers

0
user339946 On

Found one possible solution: solution

Turns out UILabel's numberOfLines property and Auto Layout don't play along nicely.

0
kai On

You don't need to restrict top and bottom constraint. The UILabel will auto adjust the contentHeight. You can refer to contentHuggingPriorityForAxis: and contentCompressionResistancePriorityForAxis:. You can fix the center of label rather than the bottom and top.