UILabel dynamic sizing- Swift

197 Views Asked by At

I have created a UIView, and inside of this view there is a label with a image above the label. The view looks like this: (don't mind the other labels on the side):

enter image description here

For the label that says "title" I wanted to make it so the height of it would be the height of the text, this is the code I used:

label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping

The problem is that because of the image above it, the text will always stay as just one line... My plan was that the image will resize based on how tall the text is, keeping its 3:4 aspect ratio using a constraint. I'm not sure why this doesn't work , but I think it's because of the constraints on the two elements.

These are the current constraints on the views:

label:

enter image description here

imageView (the trailing constraint is not the issue):

enter image description here

Thanks!

1

There are 1 best solutions below

7
On

My plan was that the image will resize

The image view has a fixed leading constraint, a fixed trailing constraint, a fixed top, and a fixed aspect ratio. Therefore it cannot resize. You have absolutely determined its size.

I had no difficulty doing what I think you describing, with no code at all (just autolayout — the label text size is changed through dynamic text):

enter image description here