Text in a UILabel isn't wrapping - ios

I'm displaying 2 or three lines of text in a UILabel however the text is getting truncated rather than wrapping.
Within IB Line Breaks is set to Word Wrap and the height of the label is easily plenty to accommodate the lines of text.
I'm doing exactly the same thing as this in other labels in other views and they do wrap, I can't see any reason or anything different why this one won't wrap.

You need to set numberOfLines for the UILabel to either 0 or whatever number of lines you want it to be. Setting it to 0 will automatically choose the number of lines based on the length of the text. The default is 1.
Edit: I just checked and in IB, the property is just called Lines.

Related

minimum number of lines UILabel in iOS development

I was trying to make a cell in uicollectionview. please see these two images
Thats the cell of a collectionview and the first uilabel's number of line is 2. what I am trying to do is the uilabel will always take 2 lines of height regardless of the content of the uilabel, is it possible? if the text is short, second line will be blank.
If I add height constraint, the text is vertically centred. How to make it top-aligned?
There are 2 steps.
First select your label, click the add new constraints, and check the height constraints, as grow4gaurav said.
Next, go to the attributes inspector and set the number of lines to 0. This makes it so that the text uses as many lines as it wants. So, if the text is short, and it only uses one line, it will just use the top line. If it is longer, it will use the bottom line too.
Hope this helps

iOS Autoshrink not working on single word in UILabel

Using autolayout to have a multiline UILabel auto size the text to fit does not work for single words.
Here is how storyboard is set up:
Here's the problem.
This does not display properly:
But this does:
I have adjusted every setting in the storyboard. I have changed all of the Line Break settings (wrap, truncate, etc), I have adjusted # of lines, I have made text plain vs attributed, I've changed font sizing and scaling, everything. What am I missing? Why does "California" get cut off instead of shrinking to fit? I've seen many other posts on SO and haven't yet found a solution (which needs to be compatible back to iOS 9)
Your label is being filled both horizontally and vertically until it runs out of space, then it starts shrinking the text to fit. In the one word example it breaks the word into two lines because there is vertical room.
Do you know in advance or can you calculate in the app when you have only one word in this label? If so you can change the number of lines to 1 instead of 0 so the app will keep the contents on one line and shrink this word.

UILabel Word Wrap produces non-uniform spacing between words

I have a UILabel in a UITableViewCell that displays content generated from an API. The Label is set to be multiline and with autolayout constraints such that the leading spacer is set to be equal to a specific number and the trailing spacer is set to be greater than or equal to a specific number. I have the line break mode set to word wrap. Unfortunately, that is producing the following:
but also the following in the same table view:
As you can see the spacing is uneven. I can't use character break as the line break mode, so I am wondering what I have done wrong or how I can get a word-break line wrapping mode that is not changing the width of the white space between words.Is this possible with UILabel? If not what do I need to do?

iOS what element is most suitable for large texts eg text view

I wonder what ui element I should use for large sets of text. Eg anything between 200 - 1000 characters.
I already place the text inside a scrollview so it doesn't have to ve scrollable or editable etc, I just want to display text.
So what should I use between label / text view / text field?
Thanks in advance.
Text views are good for texts with varying lengths, but they have a scroll view of their own. You may consider using a normal UILabel and setting the "Lines" property to 0. I know that sounds strange, but setting it to 0 tells the label that it is a multi-line label. Then you can use auto layout to establish the width of the UILabel. It will grow down based on the amount of text in it. Text fields would be inappropriate for displaying texts in most cases; they are better for user input.

Multiline UILabel with auto layout does not work

I'm trying to set constraints to get a multiline label in a static table view cell, but apparently this does not work for me, the label is still in one single line. I've set the numberOfLines property to 0 and also the height constraint to greater than or equal. And I'm setting the height for the cell correctly in tableView:heightForRowAtIndexPath. Please have a look at my screenshot to see the settings in IB.
In the comments above you mentioned you're not currently setting preferredMaxLayoutWidth. This property tells your label that it should lay out its text over the width of that property's value. In UILabel.h:
If nonzero, this is used when determining -intrinsicContentSize for multiline labels
In other words, if you don't set that, the label's intrinsic content size is whatever width the label needs to draw its text. If you set this property to the label's bounds, it will start drawing on the next line (or else it will cut the text off if numberOfLines is 0).
In your case, I would probably do that in tableView:willDisplayCell:forRowAtIndexPath:.
You can set the preferredMaxLayoutWidth in code, but when designing your user interface in storyboard, you still see a long one-line label that gets cut off at the edges. An alternative is to manually insert line breaks anywhere in your text right within the storyboard using Option-Return. You then just set the number of lines to more than your text will fit. Then select your label, hit Cmd-=. This will calculate the intrinsic content size for your label with the line breaks exactly where you want them to be.
Theoretically, I think setting the preferredMaxLayoutWidth is the more correct way to go about this, especially using Autolayout. However, I found it more practical to use the method I described above because it lets you see the layout at design time right in the storyboard, and you have total control of your line breaks. This usually works better for labels with text that don't change often. If you are dynamically changing the label text, setting preferredMaxLayoutWidth is the preferred way.

Resources