gpt4 book ai didi

ios - 如何在 Objective c 中动态设置 UILabel 高度?

转载 作者:行者123 更新时间:2023-11-28 21:17:48 28 4
gpt4 key购买 nike

我是 iOS 的新手,我面临着动态创建 UILabel 的问题。我需要根据来自网络服务的数据更改它的高度和 Y 位置。我的代码是这样的

 NSMutableArray *remarkarray =[[NSMutableArray alloc] init];
remarkarray=[responsedict valueForKey:@"Key"];
NSString *RemarkString=[NSString stringWithFormat:@"%@",[remarkarray objectAtIndex:0]];
remarklbl.text=RemarkString;
remarklbl.numberOfLines=0;
[remarklbl sizeToFit];

我已经像这样创建了 UILabel

enter image description here

但它的 Y 位置是固定的。我需要创建一个 UILabel,我可以在其中根据其中的文本更改高度和 Y 位置。像这样

enter image description here

提前致谢!

最佳答案

您可以通过在 Storyboard 中声明两个标签来非常轻松地做到这一点。

这是步骤。

  1. 将两个 UILabel 拖到您的 viewController 中并选择第一个并设置约束,如下图所示。

enter image description here

  1. 选择第二个标签并添加以下约束。

enter image description here

  1. 选择两个标签并将 numberOfLines 设置为零。

现在您可以通过代码动态设置文本。

Adding text from code... Output here..

关于ios - 如何在 Objective c 中动态设置 UILabel 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903414/

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