Force words hyphenation inside label - ios

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.

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

Two UILabels like UITableViewCellRightDetail using UIStackView

I have a Horizontal UIStackView with two UILabels. I need it to be styled like UITableViewCellRightDetail.
The first Label (on the left) should have more priority in resizing.
Here is the example:
I need it to be like this:
You can just use right alignement for the second label to achieve the desired appearance on your second image.
However this will not affect size of the labels. If you want first label to fit as much as it can, you can define a width constraint and set it manually from code after you calculate it. The second label will just have leading and trailing constraints.

Adding multiple UILabels in view with constant height between them

I am trying to add some 5-6 UILabel's (Single line only) in a UIView using auto layouts. I have added constraints for top most & bottom most UILabel. The problem is variable screen sizes.
Just wants to know a better approach of adding those remaing (3-4) middle UILables (As I want to show equal space between all the UILabels).
Approach 1 - Add height constarint for middle lables & based on screen size change the height constraint constat at runtime.
Approach 2 - I tried adding >= and <= constarints but I am not getting the output as desired.
Any help, how can I keep constant height between all UILabels.
(Can be achieved via UIScrollView or UITableView but I dont want scrolling .. only using auto layouts:))
You can add dummy UIViews between your UILabels and set those dummy views' heights to proportional to their container.
Or you should be able to achieve that by setting your current constraints constants at runtime and then calling setNeedsLayout&layoutIfNeeded
You can't achieve that by playing with "less than" or "greater than" constraints.
Edit: You can also use UILayoutGuide to do that on iOS9.0+
You can use stack view from left window where you have button/label/view etc..... or also you can add it from constraints tools(see attached image)
If you are looking for the attached solution, please add below constraints shown in the third image.

String with new line character ios7

I'm very new to iPhone programming. actually i've started to write a small app and I'm trying to set a text that contains a new line character to a label.
I have set the label properties to 0 number of lines and selected word wrap.
my text looks like this : #"ABCD\nEFGH\nIJK"
instead of printing on a new line on the label it truncates the string after the first new line character. any ideas?
If auto layout is on, and you don't add any explicit constraints, the system adds them for you. If you look at the size inspector when you haven't added ay constraints yourself, it says this,
The selected views have no constraints. At build time, explicit left,
top, width, and height constraints will be generated for the view.
Having that height constraint is what's probably causing your problem. Add constraints to position the view horizontally and vertically, but don't add any width or height constraints, and that should solve your problem. The system will adjust the width of the label to match your longest line.

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.

Resources