gpt4 book ai didi

ios - 如何在 iOS 中使用自动布局根据文本长度更改 UILabel 宽度

转载 作者:行者123 更新时间:2023-12-03 05:46:18 25 4
gpt4 key购买 nike

我想显示两个UILabel,但是UILabel的文本长度可变,根据textsize UIlabel的宽度需要增加使用自动布局。

为此,我为两个 UIlabel 编写了下面的自动布局

第一个标签:

1)leading Space 
2)Top space
3)Width
4)height
5)Horizontal spacing

第二个标签:

1)Trailing space
2)Top space
3)Width
4)height

我们怎样才能做到这一点?

请帮助我。

我的代码:

textLabel1.numberOfLines = 0
textLabel1 .sizeToFit()
textLabel1.text = "asdfdsfdghjgjhkhkjlhjkhjk"

textLabel2.numberOfLines = 0
textLabel2 .sizeToFit()
textLabel2.text = "asdfdsfdghjgjhkhkjlhjkhjk"

最佳答案

选择第一个UILabel,设置Leading、Top、bottom并设置固定宽度,然后转到尺寸检查器 -> 选择固定宽度约束 -> 关系 -> 选择大于或等于,示例如下,

enter image description here

enter image description here

选择第二个UILabel,设置Leading、Bottom、固定宽度,然后执行上面相同的过程,

希望对你有帮助

关于ios - 如何在 iOS 中使用自动布局根据文本长度更改 UILabel 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com