自 iOS 8 发布以来,我在评论显示视图中遇到了一个奇怪的问题,我的长多行标题并不总是在屏幕上显示完整标题。让这个问题更令人沮丧的是显示不一致,有时会显示完整的标题,有时会像这样将其截断:
在我的 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;
}
我的视图代码中的相关部分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];
这是约束设置(使用砌体):
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);
}];
在过去两个月左右的时间里,我一直在尝试解决这个问题,断断续续,但到目前为止还没有任何运气。如果有人遇到过类似的问题,请发表您的经验。