【问题标题】:UILabel not showing all textUILabel 未显示所有文本
【发布时间】:2021-07-24 04:06:41
【问题描述】:

我在 UITableViewCell 子类中有两个标签。我希望两个标签都是多行标签,因此我将两个标签的行数设置为零。但问题是标签不断被截断。例如,我使右侧标签的文本大小更大,使其更高,但随后我在左侧的第一个标签中添加了很多文本。但是,它不会继续换行和添加更多行,而是在与右侧标签的大小相同时截断。

但是左边的标签应该显示更多的文本,并且应该多占用几行,但是当它到达另一个标签的高度时,它会被截断。这几乎就像我在两个标签之间有高度限制,但我没有。

这是我为此使用的代码。第一部分是具有两个标签的表格视图单元子类 - firstLabel(左侧)和 secondLabel(右侧)。接下来,我有包含表格视图的视图的代码,并显示了表格视图的配置方式。最后,还有一个 UITableView 子类,将 table 的 intrinsicContentSize 设置为 table 的内容大小。我添加了这个,因为没有这个表格框架总是保持为零,所以表格视图数据源方法不会被调用。如果有人也知道更好的方法来做到这一点,那将不胜感激。

UITableViewCell 子类

这是我的 UITableViewCell 的实现。我对这两个标签都有垂直内容拥抱和内容压缩优先级,但我已经尝试过有和没有这些优先级的情况,结果是一样的。

@interface MyTableViewCell ()

@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;

@end

@implementation MyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    [self.contentView setBackgroundColor:[UIColor systemTealColor]];
    [self.backgroundView setBackgroundColor:[UIColor systemGrayColor]];
    
    [self.contentView addSubview:self.firstLabel];
    [self.contentView addSubview:self.secondLabel];
    
    NSLayoutConstraint *heightConstraint = [self.heightAnchor constraintEqualToConstant:1.0f];
    [heightConstraint setPriority:50];
    
    [NSLayoutConstraint activateConstraints:@[
        [self.firstLabel.leadingAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.leadingAnchor],
        [self.firstLabel.topAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.topAnchor],
        [self.firstLabel.trailingAnchor constraintEqualToAnchor:self.centerXAnchor constant:-4.0f],
        
        [self.secondLabel.leadingAnchor constraintEqualToAnchor:self.centerXAnchor constant:4.0f],
        [self.secondLabel.topAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.topAnchor],
        [self.secondLabel.trailingAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.trailingAnchor],
        
        [self.contentView.layoutMarginsGuide.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.firstLabel.bottomAnchor constant:20.0f],
        [self.contentView.layoutMarginsGuide.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.secondLabel.bottomAnchor constant:20.0f],
        
        heightConstraint
    ]];
}

- (UILabel *)firstLabel {
    if (!self->_firstLabel) {
        self->_firstLabel = [[UILabel alloc] init];
        self->_firstLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self->_firstLabel.numberOfLines = 0;
        self->_firstLabel.userInteractionEnabled = NO;
        self->_firstLabel.contentMode = UIViewContentModeScaleToFill;
        [self->_firstLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
        [self->_firstLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisVertical];
        [self->_firstLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self->_firstLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        self->_firstLabel.textAlignment = NSTextAlignmentNatural;
        self->_firstLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self->_firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
        self->_firstLabel.adjustsFontSizeToFitWidth = NO;
        self->_firstLabel.backgroundColor = [UIColor orangeColor];
    }
    return self->_firstLabel;
}

- (UILabel *)secondLabel {
    if (!self->_secondLabel) {
        self->_secondLabel = [[UILabel alloc] init];
        self->_secondLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self->_secondLabel.numberOfLines = 0;
        self->_secondLabel.userInteractionEnabled = NO;
        self->_secondLabel.contentMode = UIViewContentModeScaleToFill;
        [self->_secondLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
        [self->_secondLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisVertical];
        [self->_secondLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self->_secondLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        self->_secondLabel.textAlignment = NSTextAlignmentNatural;
        self->_secondLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self->_secondLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
        self->_secondLabel.adjustsFontSizeToFitWidth = NO;
        self->_secondLabel.backgroundColor = [UIColor yellowColor];
    }
    return self->_secondLabel;
}

- (void)setData:(MyModel *)data {
    self.firstLabel.text = data.first;
    self.secondLabel.text = data.second;
    [self invalidateIntrinsicContentSize];
}

@end

主视图 - 有一个表视图作为子视图

这是显示的实际视图。作为更大应用程序的一部分,该视图随后显示在垂直 UIStackView 中。

这个视图有一个表视图作为唯一的子视图,并且表视图通过 AutoLayout 固定在这个视图的边缘。 UITableView 实际上是另一个名为“AutosizingTableView”的类的实例,用于使表格视图自动调整大小(没有这个,表格的框架保持为零,并且表格视图数据源方法tableView:cellForRowAtIndexPath:,自从表格高度为零。此表格视图的代码包含在本节之后。

@interface MyView ()

@property (strong, nonatomic) AutosizingTableView *tableView;

@end

@implementation MyView

- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    if (self = [super initWithCoder:coder]) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    [self addSubview:self.tableView];
    
    [NSLayoutConstraint activateConstraints:@[
        [self.tableView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
        [self.tableView.topAnchor constraintEqualToAnchor:self.topAnchor],
        [self.tableView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        [self.tableView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
        [self.tableView.widthAnchor constraintEqualToAnchor:self.widthAnchor]
    ]];
}

- (UITableView *)tableView {
    if (!self->_tableView) {
        self->_tableView = [[AutosizingTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        self->_tableView.translatesAutoresizingMaskIntoConstraints = NO;
        self->_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self->_tableView.rowHeight = UITableViewAutomaticDimension;
        self->_tableView.estimatedRowHeight = UITableViewAutomaticDimension;
        self->_tableView.allowsSelection = NO;
        self->_tableView.scrollEnabled = NO;
        self->_tableView.delegate = self;
        self->_tableView.dataSource = self;
        [self->_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"myTableViewCell"];
    }
    return self->_tableView;
}

- (void)setdata:(NSArray<MyData *> *)data {
    self->_data = data;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myTableViewCell" forIndexPath:indexPath];
    MyData *data = self.data[indexPath.row];
    cell.detail = detail;
    return cell;
}

@end

AutosizingTableView

如上一节所述,这只是为了使表格视图自动调整大小。没有这个,表格视图高度为零并保持为零,因为由于表格视图高度,tableView:cellForRowAtIndexPath: 方法永远不会被调用。

@implementation AutosizingTableView

- (CGSize)intrinsicContentSize {
    return self.contentSize;
}

- (void)setContentSize:(CGSize)contentSize {
    [super setContentSize:contentSize];
    [self invalidateIntrinsicContentSize];
}

@end

【问题讨论】:

  • 所以我们在这里再次,即使这个问题已经被回答了两次。我建议你扔掉所有额外的东西,让自己成为一个简单的表格视图应用程序,没有堆栈视图自调整大小的业务,并说服自己这在那里可以正常工作。然后,您可以调查堆栈视图增加的复杂性。
  • (另外我感觉你实际上并不需要 heightConstraint。另外,你的代码有 很多 不必要的东西;你应该把它剪掉以获得最小的工作示例,就像我之前的回答一样。)
  • @matt 这是另一个问题。到目前为止,我在这张桌子上遇到了几个问题,但它们都是不同的问题。第一个是根据较高标签的高度来调整单元格的大小。您添加不等式的建议是正确的,对此我表示感谢。我遇到的下一个问题是我直接将标签添加到单元格,而不是添加到 contentView。这个问题还是不一样的。在这里,由于某种原因,较高的标签会根据较短标签的高度被截断(或者这就是它似乎正在做的事情)。我不知道如何解决或解决这个问题。
  • @matt 我添加了 heightConstraint 因为我以为你说过要这样做。我可能误解了您在第一次回复中的建议,如果我这样做了,我很抱歉。但是我对您的建议的解释是将每个标签底部之间的不等式约束添加到单元格底部。然后,我认为建议再做一个约束,即低优先级,将高度设置为短高度,这样可以消除歧义,然后它会被其他约束覆盖。如果我误解了,我很抱歉。我还是 AutoLayout 的新手。
  • @ashipma - 我同意马特的观点......你在这里做了一些非常奇怪的事情,其中​​大部分是不需要的。从简单的开始......简单的表格视图,带有两个标签的单元格类......让它工作。 然后 - 假设您有理由将表格视图嵌入到另一个视图中并希望它自动调整其高度 - 处理 那个 部分.

标签: ios uitableview autolayout uilabel nslayoutconstraint


【解决方案1】:

有点难以确定,因为您没有提供完整的工作示例,但这可能解决您的问题。

查看 cmets 进行更改:

@interface MyTableViewCell ()

@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;

@end

@implementation MyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    [self.contentView setBackgroundColor:[UIColor systemTealColor]];
    [self.backgroundView setBackgroundColor:[UIColor systemGrayColor]];
    
    [self.contentView addSubview:self.firstLabel];
    [self.contentView addSubview:self.secondLabel];
    
    // not needed
    //NSLayoutConstraint *heightConstraint = [self.heightAnchor constraintEqualToConstant:1.0f];
    //[heightConstraint setPriority:50];
    
    [NSLayoutConstraint activateConstraints:@[
        [self.firstLabel.leadingAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.leadingAnchor],
        [self.firstLabel.topAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.topAnchor],
        [self.firstLabel.trailingAnchor constraintEqualToAnchor:self.centerXAnchor constant:-4.0f],
        
        [self.secondLabel.leadingAnchor constraintEqualToAnchor:self.centerXAnchor constant:4.0f],
        [self.secondLabel.topAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.topAnchor],
        [self.secondLabel.trailingAnchor constraintEqualToAnchor:self.contentView.layoutMarginsGuide.trailingAnchor],
        
        [self.contentView.layoutMarginsGuide.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.firstLabel.bottomAnchor constant:20.0f],
        [self.contentView.layoutMarginsGuide.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.secondLabel.bottomAnchor constant:20.0f],
        
        // not needed
        //heightConstraint
    ]];
}

- (UILabel *)firstLabel {
    if (!self->_firstLabel) {
        self->_firstLabel = [[UILabel alloc] init];
        self->_firstLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self->_firstLabel.numberOfLines = 0;
        self->_firstLabel.userInteractionEnabled = NO;
        
        // not needed
        //self->_firstLabel.contentMode = UIViewContentModeScaleToFill;
        //[self->_firstLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
        //[self->_firstLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisVertical];
        //[self->_firstLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        //[self->_firstLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        
        self->_firstLabel.textAlignment = NSTextAlignmentNatural;
        
        // word-wrapping, not truncating
        //self->_firstLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self->_firstLabel.lineBreakMode = NSLineBreakByWordWrapping;
        
        self->_firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
        self->_firstLabel.adjustsFontSizeToFitWidth = NO;
        self->_firstLabel.backgroundColor = [UIColor orangeColor];
    }
    return self->_firstLabel;
}

- (UILabel *)secondLabel {
    if (!self->_secondLabel) {
        self->_secondLabel = [[UILabel alloc] init];
        self->_secondLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self->_secondLabel.numberOfLines = 0;
        self->_secondLabel.userInteractionEnabled = NO;
        
        // not needed
        //self->_secondLabel.contentMode = UIViewContentModeScaleToFill;
        //[self->_secondLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
        //[self->_secondLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisVertical];
        //[self->_secondLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        //[self->_secondLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

        self->_secondLabel.textAlignment = NSTextAlignmentNatural;
        
        // word-wrapping, not truncating
        //self->_secondLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self->_secondLabel.lineBreakMode = NSLineBreakByWordWrapping;
        
        self->_secondLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
        self->_secondLabel.adjustsFontSizeToFitWidth = NO;
        self->_secondLabel.backgroundColor = [UIColor yellowColor];
    }
    return self->_secondLabel;
}

- (void)setData:(MyData *)data {
    self.firstLabel.text = data.first;
    self.secondLabel.text = data.second;
    
    // this is needed
    [self.firstLabel layoutIfNeeded];
    [self.secondLabel layoutIfNeeded];
    
    // not needed
    //[self invalidateIntrinsicContentSize];
}

@end

【讨论】:

  • 非常感谢您的详细回复。我会试试这个,让你知道它是怎么回事。
  • 是的,这正是我在评论中说要消除不必要的东西时所指的那种东西。
  • 非常感谢。这解决了这个问题。我不确定到底是哪个更改修复了它。也许它正在将换行模式从截断更改为自动换行,但我认为标签只会在无法再增加其大小的情况下截断文本,因为它位于应该增长的表格视图单元格中,它应该总是能够增加大小。无论如何,再次感谢您的帮助。
  • @matt 和 DonMag - 感谢你们在这些问题上提供的帮助。我真的很感激,我很抱歉所有的问题,我只是不确定我应该把所有这些放在一个大问题还是几个小问题中。无论如何,我确实为这个问题添加了一个更新,标题为“更新 - 我正在尝试做的事情的高级概述”,我在其中大致解释了我正在做什么以及它是如何构建的,以查看你们中的任何一个是否有建议或cmets 关于如何布局具有不同动态大小部分的 UI。我在编辑中更详细地介绍。再次感谢您。
猜你喜欢
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多