Label's content is being cropped in a stackview - ios

I am showing two labels in a stackview as shown in the image below. I would like the first label to show its full content. For this, I tried making its horizontal content hugging priority as well as its horizontal content compression resistance priority higher than the other label. Still i am getting the cropped label as in the image. Isn't this supposed to work? How do I get the desired result? The stackview's alignment is set to 'Fill' and its distribution to 'Fill Proportionally'.

Set the distribution to “Fill”, not “Fill Proportionally”.

Related

Horizontal StackView issue for multiline label and fixed size icon image

I want to show a an icon image which is fixed size of 20px X 20px ,It is showing good when a label is only one line but not showing accurately when label became in two lines.Screenshot is attached.
Working Constraints.!
Set inner Stack view Alignment to Top and Distribution set to
Fill Proportionally
Set width of icon to fixed of 20px(No Aspect ratio and no
Height)
Set icon Content Compression Resistance property Horizontal
priority to 1000.
Images attached
What're the attributes for yous vStackView(Container)?
Make sure that your vStackView attributes are:
Alignment: Fill
Distribution: Fill
What're the attributes of your hStackView(Each Row)?
Make sure that your hStackView attributes are:
Alignment: Top/Center/Bottom (Depend of your requirement)
Distribution: Fill
Make sure that you set the stack view's distribution to fill and alignment to top (or center if you want the icon vertically centered)! If you specify a distribution of fill and don't change the content hugging and content compression priority of the arranged subviews, with the constraints you've set up, the intrinsic sized label should take up most of the space while the icon remains at it's natural size. I would also recommend you to make a 20px width constraint instead of the aspect ratio on the image view, but I don't think that was the problem. This might help.
Here is an example:
Outer Stack View (vertical): alignment = fill, distribution = fill
Inner Stack View (horizontal): alignment = top, distribution = fill

Content hugging priority behaves different with auto layout

While learning content Hugging priority I came up with the weird scenario, I have taken 2 labels, 1 Green and 2 Blue.
Content Hugging Priority of these labels are like
Green - Horizontal hugging priority 200
Blue - Horizontal hugging priority 251
"
Green.text = "Hello There"
Blue.text = "How are you?, have a good day, big text
Here Green labels text is truncated and blue will show full text.
Now Problem is when I try to revers the priority of both labels and text then it is not working same as above.
New Priority is now
Green - Horizontal hugging priority 251
Blue - Horizontal hugging priority 200
Green.text = "How are you?, have a good day, big text
Blue.text = "Hello There"
Now I thing Green should show all the text rather than truncating it and blue should be truncated. but its not happening so, I want to know why this is not working?. am I missing or miss interpreting this concept?
Please correct me if I am wrong. Thanks
You need to increase Content Compression Resistance Priority of green label.
Try selecting "Fill Proportionately" in the dropdown-menu for your Stack View's (Attributes Inspector) "Distribution" category.
Make sure you're selected on the Stack View.
Click Attributes inspector menu
Click Distribution and select "Fill Proportionately"
Not sure if you even need to do anything with each element's "Content Compression Resistance Priority" or "Content Hugging Priority" when doing it this way; you can try it with and without (an element with a lower Compression Resistance setting allows element to be compressed, and higher Hugging Priority number means the element will tend to grab more space) and see if it does anything different.
For me, Fill Proportionately took care of the whole problem, and I adjusted the two Priorities mentioned above back to neutral (each element have same number). Before this image shot, "Baseball Sunsetters" was truncated as both it and the numerical element ("1") were taking up the same amount of horizontal space in the stack view. After (Photo): the Name Label shifted to the right and Members Label compressed (without using compression or hugging priorities)
There are times when Content Hugging works pretty well, but look at its documentation for more details.

How to have two labels side by side, one that can grow, and one cannot?

I have two labels side by side. And I have two constraints set. Label-A starts at the leading edge of the view. Label-B starts 10px from trailing edge of Label-1. There are no width constraints.
Label-A's text length varies; Label-B's text is fixed (6 chars long). This works fine until length of Label-A goes over a certain point. Then Label-B goes off screen.
Before I assign the text value to Label-A, I want to truncate Label-A text with '...' if it will push Label-B beyond the trailing edge of the view. Any idea how to figure out if I should truncate Label-A's text and by how much? This has to work in both portrait and landscape modes.
Thanks.
Set Content Compression Resistance Priority for one of both label, according to your requirement.
Look at these snapshots:
Two labels with default content compression resistance priority.
I changed content compression resistance priority for label blablabla blablabla, from 750 to 749.
Result is:
For more details see Apple document: Setting Content-Hugging and Compression-Resistance Priorities
Make the horizontal content hugging priority and horizontal content compression resistance priority of Label-B higher than Label-A. And also add trailing of Label-B to superview.
First of all make sure that your labels have constraints for leading and trailing.
Your views layout should like this:
|-8-[Label-A]-10-[Label-B]-8-|
After that set the horizontal content hugging priority and the horizontal compression resistance of Label-B higher than Label-A.

How to lay out UIImageViews based on the height that is left?

I am trying to create this layout, but I am struggeling so badly that I have no words left for it. I have tried for three days but yet I can't create the layout.
I am trying to have the three imageviews in the middle scale if the height of the view changes, but they are not behaving correct. I have successfully manage to get the ratios correct though, but the images either goes behind the blue button, or the blue button suddenly looses its height or the text disappears.
Isn't it possible to have three image views in the middle that take whatever height is left after the title and text + blue button have been laid out and then properly show them with correct ratios?
You can try multiple approaches here:
Size Classes:
Lay out the views for each possible size (probably the easiest but the least flexible
Use Constraints with different priorities:
You can set a max-height for the image views by adding a constraint for the height and then setting its relationship to equal or less than. This allows the image to scale up until a maximum size is reached.
Then add a second constraint between the bottom box and the images. Set the priority of this constraint to 750. This makes the image grow in height, when the bottom view moves down.
Then add another constraint which sets the aspect ratio of the image. If you use the mode of the view to Aspect Fit, the image will now always be scaled up to fit the image view.
To make all images scale proportionally, add constraints for equal width and equal height between them, then set the factor to match the proportions between these images.
The last step is now to vertically align the images properly as you showed in the pictures. To do that, add constraints between the images, which align them by their vertical centers, bottoms, baselines or tops.

UILabel AutoResize cuts off the top part of the text

I have a UILabel which autoresizes along with its parent view. The label has AdjustsFontSizeToWidth turned on and has a minimum text size of 0 - so basically it tries to fit all the text into whatever size the UILabel is.
The problem I am having is that vertically the text gets cut off. So yes, the label is adjusting its font size to the width of the label but the text is too tall for the label and thus some of the text is getting cut off.
Is there anyway to work around this so that all of the text, the full height and full width are shown?
I attach an image to show what I mean. The red box is the parent view, the purple box is the UILabel.
Thanks for your help.
What you are adjusting automatically is the Width and not the Height. The Height is something you'll have to adjust manually based on the maximum font size you will use. If the maximum (assigned initial) font size fits in height, so will the smaller one's do, after they are automatically adjusted
I suspect that Lefteris is right, that minimum text size focuses on font size for the width of the control. Note, though, if you want it to resize the font to fit, you want a non-zero minFontSize. See minimizeFontSize notes. Also check out the various NSString UIKit Additions that can be used to get the size of the control necessary to fit your text, and programmatically adjust the size (i.e. the frame) your UILabel accordingly.
In my case there was a bogus vertical centering of a view under the labels being clipped and squashed. That somehow took priority over compression resistance priority of 1000 for the labels. No warning on console about conflict though. But the view debugger was of some help.

Resources