Auto Layout UILabels

纵饮孤独 提交于 2020-01-02 04:55:12

问题


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:


回答1:


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.



来源:https://stackoverflow.com/questions/14791831/auto-layout-uilabels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!