自动布局UILabels

时间:2013-02-09 21:31:28

标签: ios xcode cocoa-touch ios6 autolayout

我的自定义UILabels中有三个UITableViewCell。 可能有些UILabels将为空(label.text == @""

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EventCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        NSString *key = [[keysArray objectAtIndex:indexPath.section] description];

        UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
        namelabel.text = @"Label 1";

        UILabel *locationLabel = (UILabel *)[cell viewWithTag:101];
        location.text = @"Label 2";

        UILabel *timeLabel = (UILabel *)[cell viewWithTag:102];
        timeLabel.text = @"";

        return cell;
}

如何使用自动布局在窗格中垂直居中所有非空UILabels

这是三个标签的外观

This is how it looks with three labels:

UILables中的一个为空

时的外观

How it looks when one of <code>UILables</code> is empty

这就是我希望它看起来的样子:

And this is how I want it to look:

1 个答案:

答案 0 :(得分:3)

这很难实现,但一个简单的快捷方式可能是使用单个标签,使用三行文本,约束以填充单元格的整个高度。然后标签将垂直自动居中。

您在单元格上有三个字符串属性而不是三个标签,并且您的setter将构建最终字符串(对新行使用\n)并设置标签的文本。

如果每行文字都有不同的样式,没问题,因为你可以使用属性字符串。

相关问题