多线UILabel有时会完全显示,有时会切断

时间:2015-05-13 16:36:46

标签: ios objective-c ios8 uikit ios-autolayout

自从iOS 8发布以来,我在评论显示中遇到了一个奇怪的问题。查看我的长多行标题并不总是在屏幕上显示完整标题。是什么让这个问题更加令人沮丧的是显示不一致,有时会显示完整的标题,有时会像这样切断它:

Cut off title screenshot Fully displayed title screenshot

在我的ViewController中,cellForRowAtIndexPath方法如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Since we have multi-line labels, do an initial layout pass and then set the preferredMaxLayoutWidth
    // based on their width so they will wrap text and take on the correct height
    [self.cell.contentView setNeedsLayout];
    [self.cell.contentView layoutIfNeeded];

    [self.cell.titleLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.cell.contentView.frame)];
    [self.cell.titleLabel sizeToFit];

    [self.cell.descriptionLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.cell.contentView.frame)];
    [self.cell.descriptionLabel sizeToFit];

    return self.cell;
}

我的View代码中init的相关部分如下所示:

 // title label
    self.titleLabel = [[UILabel alloc] init];
    [self.titleLabel setFont:[UIFont fontWithName:[StringFactory defaultFontType] size:kTitleFontSize]];
//    if (self.review.title)
//    {
//        [self.titleLabel setText:[NSString stringWithFormat:@"\"%@\"", self.review.title]];
//    }
//    else
//    {
//        [self.titleLabel setText:@""];
//    }
    [self.titleLabel setText:@"\"This title should be too long to fit on one line. If it isn't, we have bigger problems.\""];
    [self.titleLabel setTextColor:[ColorFactory CC333]];
    [self.titleLabel setAdjustsFontSizeToFitWidth:NO];
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel sizeToFit];
    [self.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.titleLabel setBackgroundColor: [ColorFactory CCOrange]];
    [self.contentView addSubview:self.titleLabel];

这是约束设置(使用Masonry):

typeof (self) __weak weakSelf = self;

// title
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.contentView.mas_top).with.offset(kPadding);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
}];

[self.ratingBubbles mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.titleLabel.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.height.equalTo(weakSelf.bubbleHeight);
    make.width.equalTo(weakSelf.bubbleWidth);
}];

[self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.titleLabel.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.ratingBubbles.mas_right).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
}];


// description
[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.ratingBubbles.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
    make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-kPadding);
}];

在过去的两个月左右,我一直试图找到解决这个问题的方法,但是到目前为止还没有任何运气。如果有人遇到类似问题,请发表您的经验。

2 个答案:

答案 0 :(得分:0)

尝试使用自我调整大小的api。只要您的单元格使用约束正确设置,您只需在viewDidLoad中添加几行,

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 50;
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated]; 
    [self.tableView reloadData]; // this may or may not be necessary I found that cells didn't size correctly until scrolled or rotated without this line in iOS 8.3
}

您的约束需要从单元格的顶部到底部的任何视图都是连续的。您不需要cellForRowAtIndexPath中目前拥有的任何代码;除了将单元格出列外,你应该做的唯一事情是设置标签的文本。在单元格课程中,您不需要sizeToFit,并且您不应该在那里设置文本。

答案 1 :(得分:0)

是的,自我调整单元可以帮助你解决这个问题,正如rdelmar所提到的那样。需要注意的一点是,uilabel应该正确设置autolayout约束,并且行数应该设置为0.