iOS multiline text with VFL Constraints - ios

I want to display a switch button and a text on the same line. (and a list of those)
But when the text is too long, it doesn't spread on 2 lines right away, it first takes over the switch. Then goes on 2 lines if even longer. I, of course, cannot manage the length this string will be.
My constraint right now is very basic : #"H:|-[switchBtn%d]-[nameL%d]-|"
And the top view is simply #"H:|-[containerView]-|"
I tried using a size that was about 70% of the screen width with the metrics, but when I'm rotating the screen, I can't make it update this value correctly. It will not resize when changing screen orientation. I've tried updating the metrics dictionary, and re-attaching the constraints/updating them. No can do.
Is there something I'm missing somewhere?
Is it a bad idea to change the metric dictionary?

[nameL setContentCompressionResistancePriority: UILayoutPriorityDefaultLow forAxis: UILayoutConstraintAxisHorizontal];
Did the trick. Thanks Guy.

Related

Constraints with static cell are ambiguous

I'm having some problems with properly setting up my static cell the way I want it while making it look good for different screen sizes.
Somehow I don't seem to get how constraints are supposed to work.
One of my views contains a UIViewController with a table view. However, if I change my preview device in the storyboard to, say, an iPad, the table view doesn't get resized. Am I supposed to handle stuff like this from code only or is there a way to handle this from the storyboard (like setting width and height to always fill)?
I'm trying to set up a static cell so it looks good on devices with different screen sizes. Things work well enough when I have only two elements (e.g. one label left and one label right - just like in the standard template), but as soon as I add more things, I'm running into a lot of warnings, e.g. that width and height are ambiguous.
To me, it seems like I'm missing some key concept of constraints.
Look at the picture provided. What I want is the following:
Date to be a certain distance away from my left table border
The Stack View of volume + pic in centered in the middle
pic & price & label (there is one more for the measurement) to be represent the right label
I'm unable to actually try this at the moment but here's the path I would go.
Date:
Fixed Top, Bottom, & Leading to Container constraints to 8 (or whatever you'd like).
Volume Stack
Fixed Top & Bottom to Container constraints to 8.
Price Stack
Fixed Top, Bottom, & Trailing to Container constraints to 8.
You'll have some overlap issues with smaller screens at this point. You should get the "Red Circle" to suggest which constraints to add. Click and read each one and it will be specific enough to walk you through which you want resizing, resetting priority, etc.
*I hate dealing with constraints.
Good luck! I'll try tonight to see if I gave an acceptable answer. :)

UIStackView, make it give up .spacing in compression resistance?

Say you have a vertical stack view, it has three items, and the spacing is set to 50. It turns out to be 4 inches tall.
It turns out it's going to compress to about 3 inches (say, when the keyboard comes up - whatever).
You set CCRP to a low value - you indeed want the stack view to compress.
Oddly in all our experimentation, it seems that it will not collapse the "50" no matter what.
It seems that no matter what you do, it will take every other possible step, crushing the three items etc - but it will never give up the spacing.
Am I in error?
is there perhaps some separate way to set the CCRP "for the spacings" as such?
Do you have to flag it somehow that you want it to give up spacings?
BTW I'm totally, completely familiar with the other approaches to do this. The question is how to make a UIStackView change it's mind about the spacings seemingly being immutable.
You're correct. The spacing doesn't change. You will want to add a spacer view to accomplish what you're looking for (the last paragraph of your post makes it sound like you're familiar with this approach).

iOS horizontal UIStackView with 3 labels - First label pushed other labels out of sight

I'm not sure if I understand UIStackView correctly. I have a UIStackView in a UITableViewCell. It contains 3 UILabel and is set to align and distribute Fill.
When my first labels text is not very long, it looks all good. The second and third label take up as much space as they need and they are aligned to the right. The first label takes up the rest of the space.
However, when the first label contains a lot of text, it pushes the other two labels out of the visible area. What do I have to do to make sure the second and third label are always visible and take as much space as they need to display their content. The first label should always take up the rest of the available space and then truncate its text with "..."
This is my configuration in the Storyboard:
And this is how the result looks like in the TableView:
I tried the others solutions mentioned here and found them to be to difficult or unpractical. I didn't want to setup and change layout constraints programmatically so I continued playing around with the values. In the end, I found a very simple solution which does exactly what I wanted.
All I had to do was, setting the "Content Compression Resistance Priority" to be lower on my first label. Second and third label have 750 and also they have 251 and 252 for the "Content Hugging Priority" whereas the first label has a value of 749 and 250. This gives me exactly the results I wanted.
Thank you guys anyways. Sometimes I feel like I find solutions to my problems a lot faster after asking it here. I hope my answer will help somebody else in the future.
This:
Gives me this as a result (notice that I set "isHidden" to true for second and third label on the third row):
you may consider placing those 3 views directly to the contentView instead of using UIStackView, and set the first label trailing to the second's

UIScrollView and variable text size

I'm creating an app with quite a lot of text. It will be of variable size, so I was wondering if there is a way to adapt my ScrollView to the size of the UILabel, not knowing in the IB what will be the number of lines of that label. I'm doing it from the IB, drag n dropping it, then I'm putting 4 elements inside : an image for the logo, a title, another image, and then the label.
My question is : how can I tell my ScrollView to adapt to the text ? With constraints ? In the code ? I'm sooooo lost with that scrolling thing…
I think I need a very very good tutorial about autolayout…
Here's some screenshots of my project
The project on simulator
The layout
Thanks a lot!
You have to add the constraints to your label so that it will always have the same spacing to the scroll view. Also, you will have to set your labels lines to 0.
Don't press Add Missing Constraints because it will probably set your height and you don't want that to happen. Use the Pin button to set the spacing to nearest neighbor.
These are the buttons you will have to use, they are bottom right.
First of all, press the one in the right and select All Views > Clear Constraints.
Then, select your label in the storyboard and press the button with a square Pin, here you will have to select everything until you have something like this with different values... Also click in the red lines so that you get a continuous red line. Don't check any of the boxes you see.
After this, you have to do the same process for your Scroller.
It should be enough. I will also need to know where your scroller is.
If you have anything else in your view, do the same.

How to handle long text UILabel

I have a view which has a lot of labels. Some of them get the text set dynamically. Sometimes the text is to long to display it within one line. I know how i can display the text in multiple lines. My problem is that when i do display the text in multiple lines then the margins are broken between the labels. Sometimes a label even swapes into the content of the next label.
I want to avoid fixing this "by hand" (repositioning of all other elements). Maybe Autolayout can fix this, but sadly i'm not able to use it for now. But it would be a good argument to convert my project from springs&struts into autolayout. However, maybe there is another way to fix this issue. Would be nice if somebody can help me out. Thanks in advance!
You can use sizeWithFont:constrainedToSize:lineBreakMode: to calculate the height which will be required by each of the labels. You should use this in a loop which iterates over each label in the order they should appear on screen. As you go, increment the y position by the height of the current label and the margin. Each label will now have the correct position and at the end of the loopy will hold the full required height.

Resources