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

标签 ios objective-c uilabel

我是 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/

相关文章:

ios - UILabel 分词

ios - 设置标签文本等于变量

ios - 如何使用外部数据源转换单元格类型

iphone - CGImageCreateWithImageInRect 没有正确裁剪

c++ - 如何在 openFrameworks 中自定义 OS X 菜单栏选项?

iphone - UILabel 在行太长时自动换行

ios - 在灯塔周围度过的时间

ios - ScrollView 手势识别器吃掉所有的触摸事件

ios - 在按钮上播放声音(AV 音频播放器)返回 nil 字符串参数

ios - 在没有 segue 的 ViewController 之间发送数据