Auto Layout questions for UILabel sizing (project attached)

88 Views Asked by At

I'm a bit stumped regarding a few things I am trying to setup using Auto Layout.

Here's what I currently have. As you can see, I am trying to implement something similar to the Messages.app. I made the test/demo project very similar to explain it a bit easier.

Here's the project: https://github.com/runmad/MessagingApp

enter image description here

I am trying to achieve a few things:

• Use Auto Layout for self-sizing cells: I have set this up no problem, since it pretty much comes for free.

• Add inset between the label's frame and its text: I was under this was possible using layoutMargins (http://carpeaqua.com/2014/07/24/auto-layout-in-ios-8-layout-margins/) but I have no been able to do this.

• Size the label to the text OR a 60% maximum width of the contentView: I have been able to do this for the grey messages above (except maybe not for the first grey one?), but I have not been able to correct size the blue ("sent") messages to be a maximum of 60% of the width of the cell's contentView.

I am targeting iOS 8.0, so for layoutMargins, this should be OK. I am also using Storyboard and Auto Layout within IB.

Any help and insight much appreciated!

1

There are 1 best solutions below

0
On BEST ANSWER

The problem with your constraints (for the blue label) is that you can't use a multiplier with a leading (or left or top) constraint. The value of leading or top is 0, so multiplying that by something doesn't get you anywhere. You need to make the constraint between the trailing edge of the cell (like you do for the gray label), and the leading edge of the label. Since you're measuring from the other side, you want that to have a multiplier of 0.4 rather than 0.6.

enter image description here