ios 自动布局以动态调整 uilabel 宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20798514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:54:13  来源:igfitidea点击:

Auto Layout to dynamically size uilabel width

iosstoryboardconstraintsautolayout

提问by Msencenb

So I have two UILabels side by side in Storyboard. The second label should butt up against the right edge of the first one (trailing constraint of 1), but I also need the first label (the one on the left) to set it's width equal to it's content size unless it hits a max width. Visually:

所以我在 Storyboard 中有两个并排的 UILabels。第二个标签应该靠在第一个标签的右边缘(尾随约束为 1),但我还需要第一个标签(左侧的标签)将其宽度设置为等于其内容大小,除非它达到最大宽度. 视觉上:



|Label one text| |Label two text|

|标记一个文本| |标签两个文本|



And I need the following constraints:

我需要以下约束:

1) Label one should resize width wise unless it hits a max size.

1)除非达到最大尺寸,否则标签一应调整宽度。

2) Label two should always be butted up against the right edge of label one

2)标签二应始终靠在标签一的右边缘

How do I set this up in Storyboard?

如何在 Storyboard 中进行设置?

回答by bilobatum

  1. Create the 1 point horizontal space between the labels: Control-drag from label2to label1. Choose Horizontal Spacing from the pop-up. Double click the constraint. Change the constant to 1.
  2. Give label1a max width: Select label1. Go to the top menu bar, select Editor > Pin > Width. Double click the constraint. Change the relationship to <= and change the constant to the max width.
  3. Vertically align the labels: Select both labels. Go to the top menu bar, select Editor > Align > Vertical Centers.
  4. You still need to set constraints that define how your labels are positioned in their container view. I leave that up to you. I pinned label132 points from the left edge of the root view and 34 points from top layout guide.
  5. Update the frames of the labels so they reflect the above constraints: Go to the menu bar in the lower right-hand corner of the canvas. Tap the "Resolve Auto Layout Issues" Tie-Fighter button. Select "Update All Frames…" in the pop-up.
  1. 创建标签之间的1点的水平空间:从控制拖动label2label1。从弹出窗口中选择水平间距。双击约束。将常数更改为 1。
  2. 给出label1最大宽度:选择label1。转到顶部菜单栏,选择 Editor > Pin > Width。双击约束。将关系更改为 <= 并将常量更改为最大宽度。
  3. 垂直对齐标签:选择两个标签。转到顶部菜单栏,选择编辑器 > 对齐 > 垂直中心。
  4. 您仍然需要设置约束来定义标签在其容器视图中的定位方式。我把这留给你。我label1从根视图的左边缘固定了32 个点,从顶部布局指南固定了 34 个点。
  5. 更新标签的框架,使其反映上述约束: 转到画布右下角的菜单栏。点击“解决自动布局问题”Tie-Fighter 按钮。在弹出窗口中选择“更新所有帧...”。

Note: Notice that I did not have to create constraints to make label1's width reflect its content size. The content sizing constraints are generated automatically.

注意:请注意,我不必创建约束来使label1的宽度反映其内容大小。内容大小限制是自动生成的。

enter image description here

在此处输入图片说明

回答by Harshal Wani

Please refer below constraints to Set UIlabel and UIButton flexible width;

请参考下面的约束来设置 UIlabel 和 UIButton 灵活宽度;

Set UIlabel and UIButton flexible width according to text using xib autolayout

使用 xib 自动布局根据文本设置 UIlabel 和 UIButton 灵活宽度

回答by Abhishek Gupta

Please first get textSize with below code:

请首先使用以下代码获取 textSize:

    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 second label Frame will be:

那么第二个标签框架将是:

 UILabel *lblFirst = [[UILabel alloc] 
 initWithFrame:CGRectMake(lblFollowerName.frame.size.width + lblFollowerName.frame.origin.x, Y, W, H)];