简体   繁体   English

如何在swift中为包含在stackview中的标签提供动态高度

[英]How to give dynamic height to labels contained withing stackview in swift

Inside a UItableview Cell I have following structure.在 UItableview 单元格内,我有以下结构。

在此处输入图片说明

It has two Stackviews Left and Right它有两个 Stackviews Left 和 Right
1) Left Stackview contains 3 views. 1) 左 Stackview 包含 3 个视图。 Each view containing 1 label (Label1,Label2,Label3)每个视图包含 1 个标签 (Label1,Label2,Label3)
2) Right Stackview contains 2 views. 2) 右 Stackview 包含 2 个视图。 Each view containing 1 label (Label5,Label6)每个视图包含 1 个标签 (Label5,Label6)

Constraints for Label1,Label2 and Label3 are Top,Left,Right and Bottom with respect to it's parent views. Label1、Label2 和 Label3 的约束是 Top、Left、Right 和 Bottom 相对于其父视图。

Now when I add text to Label1 , Label2 and Label3 at run time it gives dynamic height but leaves space in top and bottom, for those label having less text.现在,当我在运行时将文本添加到 Label1 、 Label2 和 Label3 时,它会提供动态高度,但在顶部和底部留下空间,对于那些文本较少的标签。 Below is the output.下面是输出。

在此处输入图片说明

I want to reduce the top and bottom spaces for all the three labels ie Label1,Label2,Label3.我想减少所有三个标签的顶部和底部空间,即 Label1、Label2、Label3。

Any help will be appreciated.任何帮助将不胜感激。

This may help you这可能会帮助你

I think You have use distribution of Stack view as fill equally so it gives equal space to each one.我认为您已经使用 Stack 视图的分布作为平均填充,因此它为每个视图提供了相等的空间。 I think for this you have to use fill proportionally.我认为为此您必须按比例使用填充。 this will adjust height accordingly这将相应地调整高度

Choose fill proportionally from here从这里按比例选择填充

if this not working then choose fill proportionally and give height to each label it will adjust.

Check how to give height to labels .检查如何为标签指定高度。 在此处输入图片说明

after giving each label height inside stack view like this .在像这样在堆栈视图中给出每个标签高度之后。 your it will adjust itself according to data.Like this你的它会根据数据自行调整。像这样

在此处输入图片说明

For dynamic height use self sizing cells in tableview.对于动态高度,在 tableview 中使用自我调整大小的单元格。 Set rowHeight and estimatedHeight properties for tableView为 tableView 设置 rowHeight 和estimatedHeight 属性

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100

Set the stackview distribution property to .fillproportionally将 stackview 分布属性设置为.fillproportionally

as per the UIStackView documentation of .fillproportionally property根据 .fillproportionally 属性的 UIStackView 文档

A layout where the stack view resizes its arranged views so that they fill the available space along the stack view's axis.一种布局,其中堆栈视图调整其排列视图的大小,以便它们沿着堆栈视图的轴填充可用空间。 Views are resized proportionally based on their intrinsic content size along the stack view's axis.视图根据其沿堆栈视图轴的内在内容大小按比例调整大小。

in code:在代码中:

stackView.distribution = fillproportionally

or in storyboard或在故事板中

在此处输入图片说明

Please check the attached screenshot, there are showing fully step by step guide line.请检查随附的屏幕截图,其中显示了完整的分步指南。 How to set UILabel dynamically height inside vertical stackview with multiple UILabel's.如何在具有多个 UILabel 的垂直堆栈视图中动态设置 UILabel 高度。

If you have any queries please comment.如果您有任何疑问,请发表评论。

Check the attached screenshot检查附加的屏幕截图

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

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