Auto layout for dynamic width uilabel

So, I have two UILabels side by side in the Storyboard. The second label should be inserted against the right edge of the first (final restriction 1), but I also need the first label (one on the left) to set its width equal to its size if it does not reach the maximum width. Visually:




| Tag one text | | Mark two texts |




And I need the following restrictions:

1) The label must be changed in width if it does not reach the maximum size.

2) The label should always be inserted against the right edge of the label

How to set this in a storyboard?

+59
ios autolayout constraints storyboard
Dec 27 '13 at 9:55
source share
3 answers
  • Create a 1-point horizontal space between the labels: Control-drag from label2 to label1 . Select "Horizontal Spacing" in the pop-up window. Double-click the restriction. Change the constant to 1.
  • Give label1 maximum width: select label1 . Go to the top menu bar, choose Editor> Output> Width. Double-click the restriction. Change the ratio to <= and change the constant to the maximum width.
  • Align the marks vertically: select both marks. Go to the top menu bar, choose Editor> Align> Vertical Centers.
  • You still need to set limits on how your tags are placed in a container. I will leave it to you. I label1 32 points from the left edge of the root view and 34 points from the top location guide.
  • Refresh the label frames to reflect the above limitations. Go to the menu bar in the lower right corner of the canvas. Click the "Allow automatic linking of questions" button Tie-Fighter. In the pop-up window, select "Update all frames ...".

Note. Please note that I do not need to create restrictions so that the width of label1 reflects its size. Content size limits are automatically generated.

enter image description here

+104
Dec 27 '13 at 22:44
source share

The following are limitations for setting flexible widths for UIlabel and UIButton;

Set the flexibility of UIlabel and UIButton to match text with xib autolayout

+3
May 26 '17 at 2:34 a.m.
source share

Please get textSize first with the code below:

  CGSize textSize = { 230.0, 10000.0 }; CGSize size = [[NSString stringWithFormat:@"%@", yourLabelText] sizeWithFont:[UIFont systemFontOfSize:10] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping]; 

then set your first label frame with this content size:

  UILabel *lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, size.height)]; lblFirst.lineBreakMode = YES; lblFirst.lineBreakMode = NSLineBreakByWordWrapping; lblFirst.numberOfLines =size.height; lblFirst.backgroundColor = [UIColor clearColor]; [self.view addSubview:lblFirst]; 

then the second Frame label will be:

  UILabel *lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(lblFollowerName.frame.size.width + lblFollowerName.frame.origin.x, Y, W, H)]; 
+1
Dec 27 '13 at 10:08
source share



All Articles