ios - 自动布局 UILabel

标签 ios xcode cocoa-touch ios6 autolayout

我的自定义 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

这是三个标签的样子

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:

最佳答案

这很难实现,但一个简单的捷径可能是使用一个标签,三行文本,限制填满单元格的整个高度。然后标签将自动将其内容垂直居中。

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

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

关于ios - 自动布局 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14791831/

相关文章:

ios - WKWebView 无法使用 loadHTMLString(_, baseURL :)) 加载图像和 CSS

ios - 使用NSLog打印变量,为什么要添加nil?

ios - 无法从处理程序访问类中存在的变量

ios - 如何在不破坏 Rust 工具链的情况下在 XCode 中测试 iOS 应用程序?

ios - cocoapods 中常见的 pod 依赖性问题

ios - viewForZoomingInScrollView 工作但有额外的空白空间

ios - NSLayoutConstraint 和动态宽高

iphone - 防止方法被不同实体同时调用的优雅方式

cocoa-touch - 不使用 transitionFromViewController 时我必须发送的 UIViewController 包含消息的正确顺序是什么?

ios - (Swift) 尝试记录应用程序数据,但检查数据失败