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.
Related
how can i prioritize a label to be displayed fully and another label to display it too but if its too big it should make ... in the UIStackview
This is my StackView
Here is an example that follows what you want:
First, a UIStackView will actually make things harder here because we want to fine-tune the distance between each label. I just used leading and trailing constraints to layout each label in a line.
The trick is to set the proper content compression resistance priority and content hugging priority for each label. First, we want 0s to always match the size of its content, so we set its content compression resistance priority to 1000. Content hugging priority can stay at the default of 250.
I also split the # sign into its own label, since we always want it to appear. Set its content compression resistance priority to 1000 and keep the content hugging priority at 250.
Next, for the shrink_label, we set the content compression resistance priority to 749 and the content hugging priority to 1000. That is, we want the shrink_label to never try and grow bigger than its text content size, and we want it to allow its content to be compressed if we need.
Finally, for the laaabel we set the content hugging priority to 1000 and the content compression resistance priority to 750. That is, the label will take up as much space as it can, and since its compression resistance priority is 1 higher than the shrink_label's, the shrink_label will compress before the laaabel.
You can read about content compression and content hugging in the Apple docs here:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/ViewswithIntrinsicContentSize.html
The full source code for my example app is available here:
https://github.com/elliotfiske/StackOverflowContentHuggingExample
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.
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”.
What effect do the First Baseline and Last Baseline properties have on the subviews of a UIStackView?
Generally, can someone please illustrate the implementation of these values?
Suppose you have a two-line text view (or field or label or button) and a three-line text view, and you add them to a horizontal stack view. Do you want to align their first lines, or their last lines? That's the difference.
.FirstBaseline:
.LastBaseline:
Note: I increased the vertical content hugging and compression resistance priorities to 1000 (required) to get this to work properly.
var firstBaselineAnchor: NSLayoutYAxisAnchor
Represents the baseline for the topmost line of text in the view.
var lastBaselineAnchor: NSLayoutYAxisAnchor
Represents the baseline for the bottommost line of text in the view.
Check out the loading date label. It could be '2 May' or '24 December'.
I want to display the label 'days remaining' after it. Since the width of 'loading date' is dynamic, via auto layout I haven't been able to place 'days remaining' after it. It stays at a fixed distance.
How to adjust it in xcode, using autolayout?
I've been able to achieve it via code by disabling autolayout. However with iOS 7 coming up and autolayout would be essential to maintain iOS 6 and 7 at the same time, I thought it would be a good time to do it with autolayout.
Make sure that the spacing between your two labels is set to auto and that your days remaining label is left aligned. I suggest keeping a constraint between the trailing edge of your days remaining and the superview, but lowering the priority a bit. The goal there is to ignore it when the label should be far away from the superview, but not clip or go outside of the visible area when the date is very long.
You may also need to increase the content hugging priority of the date label and/or decrease the compression resistance of your days remaining label.
You may get better insight into what your labels are doing by setting the background color on them temporarily. The goal is to see how large your labels are, where the text is in the label, and where the spaces are. If there are large gaps between the labels that will help you figure out where to add constraints or adjust the priorities. If the labels are taking up the full width then it will help you get the alignment set correct.