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.
Related
I'm doing an assignment from a book for learning iOS development. The assignment requires that the name label at the upper left area of a table cell should be able to display multiple lines if the name is too long and it should not go beyond the leading constraint of the value label at the far right of the same cell like this:
Besides the other usual constraints, I added the trailing constraint to the name label, which is equal to the trailing constraint of the value label, like this:
And the run result is like the following:
The name label text undesirably runs over the top of the value label text.
I've also tried to add a horizontal space between the name label and the value label, but the compiler reported a conflict of constraints. Would you please tell me what I should do to make it right?
Thank you very much!
First You need to set Your name label trailing constraint to your value label leading like as below
Then If you try to increase content of your name label it shows error like this
To Solve this error go to View Controller View hierarchy and change the priority of name label
Then You achieved...
If you typed that correctly, then your problem is you set the trailing anchor of the name label equal to the trailing anchor of the value label. That would mean that the right edge of each label has to be equal. You don't want that. The system is probably breaking your constraint and making its own weird one instead which is causing the undesirable behavior.
Set the trailing anchor of the name label equal to the leading anchor of the value label minus some constant (whatever padding you want).
Programmatically you do something like this
nameButton.translatesAutoresizingMaskIntoConstraints = false
nameButton.trailingAnchor.constraint(equalTo: valueButton.leadingAnchor, constant: -10)
If you must use storyboard, which I advise against in general, I think this or some other tutorial page will show you how. I don't know how to use storyboard very well at all.
https://www.raywenderlich.com/811496-auto-layout-tutorial-in-ios-getting-started
tried this one, hoping the following is the result you required
final result
in order to acquire this
first insert two labels in the cell as follows
labels added
given top and leading constraints for the title label
title label constraints
in order to give constraints (Horizontal spacing) between title and the amount
right click and hold in title label and drag to amount label. it will show a
pop up window as follows
horizontal spacing
once we select the horizontal spacing we have to change according to our need
in-order to change select title label then select size inspector from the
right window
then change the trailing space to the amount label
updated value
now given constraints for title label
leading
top
horizontal spacing to amount label (trailing)
once it given it will show error in the constraints since the amount label constraints are not given
next have to give constraints for amount label
trailing
width and size
vertical position on the cell
constraints for amount label
now try to input larger text values in title label
look like one more step to complete
in order to get multiple line change the number of line property value of title label to 0 in attribute inspector
final output
In a page of my application, I would like to add a Label text on top of a bottom bar.
I have succeeded that by adding the following constraint:
BottomBarImage.top = Label.bottom + 24
The problem is that the label is planned to show 2-3 lines of text.
I have tested on simulator and the result is only one line is showed.
Important note: By removing the constraint, the lines in Label are shown correctly (but the position of the Label is wrong of course)
How can we fix the multiline issue ?
Thanks
The problem is that your constraints are insufficient.
A label that is not under auto layout just sits there, inert; at layout time, its size is the size you gave it in the storyboard.
But as soon as you add a constraint to it, the label comes under auto layout. That changes everything. A label under auto layout is self-sizing: it automatically adapts to the size of its content. It will automatically adapt to the necessary height, but only if its width is fixed (so that it knows when to wrap). You have not given any width constraint for this label, nor have you given it a preferredMaxLayoutWidth, so the auto layout engine doesn't know how to do that.
So, to fix this problem, add a width constraint to your label or give it a preferredMaxLayoutWidth. Or, if you know both dimensions, give it both a width constraint and a height constraint. (And make sure the numberOfLines has been changed to 0; the default is 1 which is a single line only.)
I'm trying to set this up and have always done this in code and would like to get this done in modern version of Xcode 7.3.
I do the following steps:
drag out a UILabel
set the number of lines to 0
set Line Breaks to Word Wrap
But this isn't breaking and representing all the data. The data is being populated though. What else do I need to do?
edit 2
added border for this
The problem is that when you do this in code, your label is not using auto layout. But when you drag into the storyboard, your label is using auto layout, so the rules are different. You need to add constraints.
What you do now depends on what you want. First you'll need constraints for top and left of the label. Then, if you add constraints for just width, the label will grow downwards as it fills with text. If you add constraints for both height and width, the label will simply fill up within that size.
I have a UILabel with the following constraints
and I set the number of Lines equal to 0. The layout on IB can be seen below. I want this UILabel to expand dynamically based on the text that I receive where the views below it get pushed down based on the height of the UILabel. However, that is not what happens.
As seen below, the UILabel doesn't appear at all. It seems to have a height of 0 regardless of what I set the text to be. Does anyone know what I need to modify to make this happen? Does anyone know why the UILabel has a height of 0?
UPDATES:
Things I tried given the comments below.
word wrap, nothing changed
adding a height constraint on UILabel, text gets cut off after the first line
height constraint with greater than or equal to constant, text still gets cut off after the first line
I created a dummy View according to your requirement.
Button has fixed constraints : leading width ,height, top space.
For label : leading, trailing, topSpace to button
3.For View below the label: give leading, trailing, height , top space to label
so here label height is not fixed .so it will change according to text.
After setting the text in label try out "labelName.sizeToFit()" this will automatically adjust the height of your label.
You should try to set a height constraint to your Label.
Considering the information you provided, it seems like it doesn't have enough height because you are using bottom constraint to the view under it. Rearrange your constraints or give a height constraint to the UILabel.
Select the UILabel,Set the Line Breaks mode is Word Wrap and set the number of lines is 0, example is given below,
And set the Height Constraints, If you set height contraints then select the UILabel, see the right side, select show the size Inspector --> Double click the height Contraints --> Relation --> set the Greater Than or Equal, its automatically expand the label, example is given below,
hope its helpful
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.