目标c-以编程方式约束表格单元格中的UILabels



我正在以编程方式创建带有3个标签和一个图像的UITableViewCells。现在,我的所有标签都是重叠的,单元格有一个固定的高度。

所需的效果是在左边有一个固定大小的图像,标签从屏幕左边75点开始,每行一个。

以下是我想要的:

_____ | | Label 1 | img | Label 2 with potentially multi-line text |_____| Label 3 我不确定让标签堆叠在一起并根据三个标签的组合高度调整单元格大小的最佳方法。

self.title = [[UILabel alloc] initWithFrame:frame];
[self.title setLineBreakMode:NSLineBreakByWordWrapping];
[self.title setTextColor:[UIColor colorWithRed:21/255 green:32/255 blue:48/255 alpha:1]];
[self.title setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0f]];
[self.title setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.title setNumberOfLines:0];
[self.contentView addSubview:self.title];
self.description = [[UILabel alloc] initWithFrame:frame];
[self.description setLineBreakMode:NSLineBreakByWordWrapping];
[self.description setTextColor:[UIColor colorWithRed:21/255 green:32/255 blue:48/255 alpha:0.7]];
[self.description setFont:[UIFont fontWithName:@"HelveticaNeue" size:12.0f]];
[self.description setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.description];
[self.description setNumberOfLines:0];
self.user = [[UILabel alloc] initWithFrame:frame];
[self.user setLineBreakMode:NSLineBreakByWordWrapping];
[self.user setTextColor:[UIColor colorWithRed:136/255 green:136/255 blue:136/255 alpha:1]];
[self.user setFont:[UIFont fontWithName:@"HelveticaNeue" size:10.0f]];
[self.user setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.user setNumberOfLines:0];
[self.contentView addSubview:self.user];

感谢

由于您使用了setTranslatesAutoresizingMaskIntoConstraints = NO,我认为您希望使用VFL。

在你的情况下,我认为最简单的方法是在你的XIB中设置自动布局,但如果你没有,你可以使用VFL。

你可以在这里阅读-VFL

或者这里有代码示例VFL教程

祝好运

您为所有三个标签设置相同的帧坐标。

self.title = [[UILabel alloc] initWithFrame:frame];

所以你必须为每个标签设置三个不同的框架。

self.title = [[UILabel alloc] initWithFrame:frame1];
self.title = [[UILabel alloc] initWithFrame:frame2];
self.title = [[UILabel alloc] initWithFrame:frame3];

其中帧1、帧2和帧3仅具有y坐标的不同值。

frame2 = [75,"any constant value",70,10];
frame2 = [75,label1.frame.origin.y + 5,70,10];
frame3 = [75,label2.frame.origin.y + 5,70,10];

这对你有帮助吗。

最新更新