UILabel Word Wrap produces non-uniform spacing between words - ios

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?

Related

Force words hyphenation inside label

I have s string one, two, three and a label. How can I force each word to be in separate line inside label using auto-lauout ?
One way to do this is using autolayout with the following constraints on uilabel.
Center horizontal and vertical in container.
Give a width constraint in such a way that it break to next line.
Don't give height constraint.
Set lines number of uilabel to 0
Have a look at this GIF.
Hope this can help.

Multiline UILabel and UITextView using Storyboards, Auto Layout and Swift

I am trying to add a Multiline UI Label as a header followed by a UITextView as the content body.
I have added the UILabel and UITextView as follows:-
The Label has number of lines set to 0, character wrapping and standard left, right, bottom and top constraints. I have also set a minimum height constraint but that does not seem to change anything. The UITextView below it has a top constraint to the label and left, right and bottom constraints to the view/layout guide.
The actual output is this:-
There should be 3 lines for this label but it is showing only 2 lines/
This label should have 2 lines and the text as "Tricyclic anti-depressants" but the last line is not visible.
Is there anything wrong with my setup/constraints?
Thanks
Do these two things:
Increase the number of lines of the label
Set the Height Constraint to greater than or equal
Make sure the top label's Vertical Compression Resistance priority is higher than the textview.

UITextView with autolayout is wordwrapping too soon

I'm trying to set up a UITextView with autolayout rules inside a UICollectionViewCell.
I have constraints on the leading and trailing side of it, such that:
Leading is greater than or equal to 1:8 the screen's width and trailing is equal to the screen's width minus 15px.
This work well and achieves my desired behavior (keeping the text view as small as possible until it exceeds my maximum line length, then it will wordwrap) most of the time. However, for certain strings/string lengths it screws up and truncates way too early.
An example:
If I set the UITextView's text to: "http://www.billywitchdoctor.com" it all prints out on one line no problem.
However, if I set it to just: www.billywitchdoctor.com the "m" gets knocked off to the next line somehow, so it renders like:
www.billywitchdoctor.co
m
even though there is plenty of space left for the bubble to stretch width-wise. I've tried setting the Compression Resistance Priority to 1000 for the UITextView and reducing hugging but I can't seem to do anything to remove this issue so long as I have these two constraints on the bubble (leading >= 1:8 cell trailing and trailing == cell trailing.)
I know I can probably programmatically fix this issue for each cell by calculating and specifying the width myself, but I'd rather use auto layout if possible.
Try invalidating the layout whenever the text changes.
- (IBAction)textViewDidChange:(UITextView*)textView {
[textView invalidateIntrinsicContentSize];
}

Allow UILabel to grow dynamically using auto layout

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Text in a UILabel isn't wrapping

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.

Resources