How to handle long text UILabel - ios

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.

Related

iOS multiline text with VFL Constraints

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.

how to set which label should be truncated if labels overlap?

i have a similiar problem as asked in How to resolve the Storyboard's warnings on Xcode?
However my left label can get really wide at runtime and the standard behaviour is, that the right label is truncated. How can i force the interface builder to truncate the left label and not the right one?
Both texts in the labels are set on runtime, so i have no knowledge of the resulting width of each label.
As stated above i used the solution from the linked question but the wrong label is truncated. So my problem differs from the one stated in the link.
There is a property called content compression resistance.
This determines how hard it resists compression of content.
So... for the label that you want to keep full length run this...
label.setContentCompressionResisyancePriority(.required, axis: .horizontal)
I’m typing on my phone so I may have messed up the parameter names. But auto complete will help.
This will make sure that the other label is truncated first.
You can also set this in IB in the measurements panel near the bottom.

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.

Autoshrink in UILabel only for width?

I have some ASCII syntax diagrams which must not have line breaks in the middle.
These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle.
I just want to shrink only if the content doesn't fit the width. It would be absolutely fine to scroll vertically through the text.
What is the best way to do this with UILabel or any other UI element?
Use UITextView with 'editable' property set to false.
So let me rephrase your question. I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. If this is what you want, well the imagination is weird to me...
But anyway, I feel there's a conflict in your settings. First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. But then Autoshrink will play no effect. I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code.
I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. I guess you want to take a look about this:
Autoshrink on a UILabel with multiple lines
The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. Maybe this can achieve what you want.

Xcode label causes line break in middle of word

I've been working with a label on a storyboard and can't seem to get it to behave correctly. I have my lines set to 0 because I display a couple different things in the label, depending on what the user clicked before. 3 or 4 words will display fine and on multiple lines, but when I get to a larger single word it will break the word in half. I don't won't it to do this, I just want it to resize and stay on a single line if it's only one word. Any ideas how to do this? I already have it working so that it will auto resize long text to fit there, the only problem is this.
If a UILabel is too small in its width, it cuts off the last part of the text or breaks its content into another line. As one word does not break into half automatically it can't be displayed entirely and therefore is cut off at its end.
In order to solve your problem I have some suggestions:
Change the number of lines to at least 1
Update your frames
Resize the UILabel so it can display the entire word.
Take a look at your constraints, as they might force the UILabel to resize under certain conditions.
I hope this might help you.

Resources