自动布局UILabels [英] Auto Layout UILabels

查看:103
本文介绍了自动布局UILabels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义有三个 UILabels 的UITableViewCell
这可能是一些 UILabels 将是空的( label.text == @

I have three UILabels in my custom UITableViewCell. It might be that some UILabels will be empty (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 垂直使用自动布局单元格?

How can I centre all non-empty UILabels vertically in the cell using auto-layout?

这是它的外观与三个标签

它的外观的产品之一 UILables 为空

How it looks when one of UILables is empty

这就是我希望它看起来:

推荐答案

这是棘手的实现,但一个简单的快捷方式可能是有一个标签代替,用三行文字,约束填补的整体高度细胞。标签将自动然后中心它的内容垂直。

This is tricky to achieve, but a simple shortcut may be to have a single label instead, with three lines of text, constrained to fill the whole height of the cell. The label will then auto-center its contents vertically.

您不得不对细胞,而不是三个标签3字符串属性,你的制定者将建立最后一个字符串(使用 \\ n 新行),并设置标签的文本。

You'd have three string properties on the cell instead of three labels, and your setters would build the final string (using \n for new lines) and set the label's text.

如果文本的每一行都有不同的造型,没有问题,因为你可以使用归因字符串。

If each line of text has different styling, no problem, as you can use attributed strings.

这篇关于自动布局UILabels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆