How to resize UILabel for text

I have UILabel, which I posted in a storyboard centered on the view. It has the "touch input" source code.

I change the label value as the username after login. As soon as I do this, the label is no longer centered since UILabel did not resize.

How to do this with autorun in the interface builder?

+7
ios xcode interface-builder uistoryboard
source share
5 answers

enter image description here

see this scrrenshot

1 first select a label width limit

2 set the ratio greater than or equal to

3 set the default constant value here, I set it to 10

when you change the label text, it changes the label size according to its text. hope this helps :)

+8
source share

You can set the UILabel frame.width more, possibly the same width as the screen.

Then you install

label.textAlignment = NSTextAlignmentCenter

0
source share

1. Definition of the source text on the label 2. After pressing the button for changing the text displayed in the middle of the screen. 3. Look at the limitations of the third image, the two restrictions indicated by 1.top for viewing 2. horizontally in the middle in the container

Another thing is, if you have a fixed width for the label, change the ratio as more than equal. [Limit click width, see the right side at the top, change the ratio ]

0
source share

I think the label does not get height and width, think about providing these restrictions, and then when your text changes use this method to determine the height and width:

  func labelSizeWithString(text: String, maxWidth : CGFloat,numberOfLines : Int) -> CGRect{ let label = UILabel(frame: CGRectMake(0, 0, maxWidth, CGFloat.max)) label.numberOfLines = numberOfLines label.text = text label.sizeToFit() return label.frame } 
-one
source share

Just set the binding as follows Allignment Label . It will fix your problem.

-one
source share

All Articles