Swift: UILabel how to show all text
I am very new to Swift and I have no idea how to render my UILabel all text.
This is what my shortcut looks like in my storyboard:
As you can see, some of the text has been omitted. I can show all the text by dragging the UILabel to resize. However, at runtime, if the text is set dynamically, how do I resize the label so it can display the most text?
I tried UILabel.sizeToFit()
it but it doesn't change anything.
source to share
you can do this by setting numberOfLines
in0
Example
myLabel.text = "whatever"
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
And you have a frame on it
Refer: to this link
source to share
YourUILabel.sizeToFit()
right, but that's not all.
You need to set the number of lines and the type of line break before calling .sizeToFit()
..
YourUILabel.numberOfLines
equal0
for unlimited lines ...
YourUILabel.lineBreakMode
equal to aNSLineBreakMode
dot (.)ByWordWrapping
eg.
source to share
Why is this old. Let's use autolayout . If you haven't used it yet. Let's get started - Steps:
-
Create a UIlabel and set its width to whatever amount you want on screen.
-
Select the label go to "Editor" → "Pin" and "one by one". Leading trailing and top space for Superview (not bottom).
-
Since you have fixed width and you have dynamic height as well as pin width.
-
Go to the Attribute Inspector tab and set the rows to 0 (means unlimited).
-
Go to the size inspector and set the crawl priority (250/251) to low and the Resistance priority to high (say 750).
And you're done. !! (not one line of code)
First you need to calculate the height of the label according to the length of the text you want to set and then increase the number of lines accordingly as
yourLabel.numberOfLines = "Your calculation number"
and then set the mark box according to the calculated height and
If you cannot calculate the height of the label, I would say use a TextView , it will automatically add scrolling to the text and you can view all the text.
source to share