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: enter image description here

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.

+3


source to share


5 answers


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

+4


source


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

equal 0

for unlimited lines ...

YourUILabel.lineBreakMode

equal to a NSLineBreakMode

dot (.) ByWordWrapping

eg.

+3


source


If you are not resizing the frame UILabel

using autoplay, you can change the autoplay setting by selecting a shortcut from the storyboard and adjusting this setting in the inspector panel.

enter image description here...

+2


source


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)

+1


source


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.

0


source







All Articles