简体   繁体   English

Swift-自动布局3个具有动态宽度的视图

[英]Swift - Autolayout 3 views with dynamic widths

I'm working with constraints and it was ok till now. 我正在处理约束,到目前为止还可以。

I have a tableview and in each row I have 3 labels. 我有一个tableview,在每一行中都有3个标签。 the contents of this 3 labels are dynamic so I need the width of those labels to be dynamic as well. 这3个标签的内容是动态的,因此我也需要这些标签的宽度也要动态。 The thing is, I want the rightmost label to shrink if needed to show the full content of left label and middle label in their full font size. 关键是,如果需要显示最左边的标签和中间标签的全部字体,我希望最右边的标签缩小。

The right label set to adjustsFontSizeToFitWidth = true so the font shrink if needed, and the other labels have fixed font size. 正确的标签设置为adjustsFontSizeToFitWidth = true因此字体可以根据需要缩小,其他标签具有固定的字体大小。 Here is what I have so far - 这是我到目前为止的内容-

MainView.addConstraint(NSLayoutConstraint(item: leftlabel,
                                          attribute: .Left,
                                          relatedBy: .Equal,
                                          toItem: MainView,
                                          attribute: .Left,
                                          multiplier: 1,
                                          constant: 15))
MainView.addConstraint(NSLayoutConstraint(item: midlabel,
                                          attribute: .Left,
                                          relatedBy: .Equal,
                                          toItem: leftlabel,
                                          attribute: .Right,
                                          multiplier: 1,
                                          constant: 15))
MainView.addConstraint(NSLayoutConstraint(item: rightlabel,
                                          attribute: .Left,
                                          relatedBy: .Equal,
                                          toItem: midlabel,
                                          attribute: .Right,
                                          multiplier: 1,
                                          constant: 15))
MainView.addConstraint(NSLayoutConstraint(item: rightlabel,
                                          attribute: .Right,
                                          relatedBy: .Equal,
                                          toItem: MainView,
                                          attribute: .Right,
                                          multiplier: 1,
                                          constant: 15))

what actually happens is that the left most label width is shrinking and I can't see the whole text inside it, instead I see some of the text and 3 dots after few letters. 实际发生的是最左边的标签宽度在缩小,我看不到其中的整个文本,而是看到一些文本和几个字母后面的3个点。

another thing I wanted, is that the middle label will always be next to the left label even if there is "white space" (for example if the text inside the 3 labels is shorter than the main view width). 我想要的另一件事是,即使有“空白”(例如,如果3个标签内的文本短于主视图宽度),中间标签也将始终在左侧标签旁边。

See Image below: In the first two rows you can see what I want, and in the last row is what happens 参见下图: 在前两行中,您可以看到我想要的内容,而在最后一行中,将发生什么情况

Figured it out! 弄清楚了!

Adding width to the left most label solved the problem. 在最左边的标签上增加宽度可以解决此问题。

Because I don't know the width of the label from the beginning, I used leftlabel.intrinsicContentSize() to get the width, and added another constraint with this width. 因为我从一开始就不知道标签的宽度,所以我使用leftlabel.intrinsicContentSize()来获取宽度,并为此宽度添加了另一个约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM