自动布局UILabels [英] Auto Layout UILabels

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

问题描述

我在自定义 UITableViewCell 中有三个 UILabels
可能有些 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 使用自动布局在单元格中垂直?



b
$ b



UILables 之一为空时如何查看



看起来如何>>



这是我想要的外观:



解决方案

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



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



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


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;
}

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

This is how it looks with three labels

How it looks when one of UILables is empty

And this is how I want it to look:

解决方案

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.

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天全站免登陆