Swift: UILabel how to show all text - ios

I am very new to Swift and I don't know how to make my UILabel show all text.
Here is how my label looks like in my storyboard:
As you see, part of the text is omitted. I can show all the text by dragging the UILabel to resize. However, during the runtime, if the text is set dynamically, how can I resize the label so that it can show more all of the text?
I tried UILabel.sizeToFit() but that doesn't seem to change anything.

you can do this by setting numberOfLines to 0
Example
myLabel.text = "whatever"
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
And you have so set frame according to it
Refer : to this link

YourUILabel.sizeToFit() is right but that is not all of it..
You need to set the number of lines and the kind of line break before calling .sizeToFit()..
YourUILabel.numberOfLines equal to 0 for unlimited number of
lines...
YourUILabel.lineBreakMode equal to NSLineBreakMode dot(.)
ByWordWrapping for example..

If you are not resizing the UILabel frame using autolayout you can change the Autoshrink option by selecting your label from the storyboard and adjust this option on the inspector pane.
.

Why do it the old way.. Let's use autolayout. If you haven't used it yet. Lets begin - Steps :
Create a UIlabel and set it's width to any amount you want it on screen.
Select the label go to Editor menu -> Pin and one by one pin Leading Trailing and Top space to Superview (not the bottom one).
Since you have fixed width and you have dynamic height so also pin Width.
Go to Attribute inspector tab and set lines to 0 (means unlimited).
Go To size inspector and set Content Hugging priority(250/251) to low and Content Resistance priority to high (say - 750).
And you are done.!!(not a single line of code)

You first have to calculate the Height of label according to the length of the Text you want to set and then accordingly increase the number of lines
as
yourLabel.numberOfLines = "Your calculation number"
and then Set the Frame of the Label according to the Height calculated
and
if you cannot calculate the height of the Label then i would say use the TextView it will automatically add a scroll to the text and you can view all the text.

Related

Adjust height of UILabel dynamically using iOS storyboard

I have a label, which has been created using iOS Storyboard layout.
Contents of label, however, are dynamic and can change during run time.
How do I make sure that label's height gets adjusted based on the content in the label.
I tried:
Setting number of lines to 0
Setting Editor -> Size to Fit Content.
But they don't work. Text in label gets printed only in single line, and as a result of that some of text doesn't appear on the screen.
Any help would be highly appreciated.
Try this way
Your lable should be number of lines to 0
give height constraint to lable and select height constraint then set greater than equal , it will automatically adjust height based on content
Please check your constraints. You can't set label's height constraint.If superview's height is fixed,you can't set top and bottom constraint at same time.Because it means you set the height constraint of label. So you can must fix label width and you can't fix label height.

UILabel keeps resizing to a height of 0 regardless of the text

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

iOS Swift Text Fill Screen

I have taken a label and dragged it onto a blank UIView.
I have changed the text orientation with
codeLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
but I now want the text to fill the screen, I have browsed the web and found this but it doesnt seem to work
codeLabel.adjustsFontSizeToFitWidth = true
Add a width equal constraint between the UILabel and its parent UIView. Do that by draging from the UILabel to the UIView.
What you want to achieve is to let the UILabel with equals to the UIView height. but what you have so far is an equal widths constraint.
Select that constraint by clicking it and change the value next to "Second Item" to Superview.Height.
To let font fit the screen:
select the label.
set a very large font. example 100.
change the value of Autoshrink to Minimum font size.
set a small value there. example 5.

How to set this kind of constraints: UIImageView's width based on UILabel's Length in AutoLayout?

I already set UIImageView.width(Green Color Background) == UILabel.width + 20 in my tableView cell ,but when the label's content changing , the label 's height is changing at first ,but I want the label to grow wider first , if it can't be more wider then grow higher. what 's the problem here
Hi yes you can do it. I am explaining below how can u do it with auto layout.
At the time of applying constraint, instead of giving hight and width
to UIImageView just try to give it three constraint with UILabel which
are " Equal Width, Equal Height and Aspect Ratio"
For Applying these three constraints with UILabel the shortcut is just
drag and drop your mouse cursor from UIImage to UIlabel with right
click only, so you can see above three options. After applying it ,give
UIlabel Leading and trailing constraint( or whatever u want) and most important thing, set
your UIlabel to size to content fit. For UILabel,shortcut is just
click on UILabel and click on top toolbar's tab EDITOR and you will
find option for setting UILABEL as size to content fit so after
setting it UILabel will automatically set its frame as per its content
and so as UIImageView.

UILabel AutoShrink property in not working in ios 7

I added Autoshrink Property of my label from nib file as follow;
It is working good in ios6 but not effected in ios7.
ios6:
ios7
Why this happen? I am using numberoflines=2
Help to solve this
Thank you
Yes that is because you are using
numberOfLines = 2
that means your label will be of two lines, so its auto adjusting the size accordingly as your label will be of 2 lines,
set background color of the Label to get to know about the label dimension.
EDIT -
If you want your labeltext to fit someother Superview element accordingly and have thinking that your label can have a text too long, then set numberOfLines = 0.
Basically numberOfLines means the Maximum line breaks the text of the label can have
Set your FONT to a lower value - 14,13 (judging by your previous one that is what i am sensing)
Set your Label to a fixed width, so that you know that a nextLine would appear after that width,
Set Number of lines to 0

Resources