UILabel 不显示所有文本
UILabel not showing all text
我在 UITableViewCell subclass 中有两个标签。我希望两个标签都是多行标签,所以我将两个标签的行数设置为零。但问题是标签不断被截断。例如,我使右侧标签的文本大小更大,因此它更高,但随后我在左侧的第一个标签中添加了很多文本。但是,与其继续换行并添加更多行,它只是在与右侧标签大小相同时截断。
但是左边的标签本来应该显示更多的文字,也应该多占几行,但当它到达另一个标签的高度时却被截断了。这几乎就像我在两个标签之间有高度限制,但我没有。
这是我为此使用的代码。第一部分是 table 视图单元格 subclass,它有两个标签 - firstLabel(左边的)和 secondLabel(右边的)。接下来,我有具有 table 视图的视图的代码,并显示了如何配置 table 视图。最后,有一个 UITableView subclass 将 table 的 intrinsicContentSize 设置为 table 的内容大小。我添加这个是因为没有这个 table 框架总是保持在零,所以 table 视图数据源方法不会被调用。如果有人也知道更好的方法,将不胜感激。
UITableViewCell 子class
这是我的 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
主视图 - 有一个 table 视图作为子视图
这是显示的实际视图。作为较大应用程序的一部分,此视图随后显示在垂直 UIStackView 中。
此视图有一个 table 视图作为唯一的子视图,并且 table 视图使用自动版式固定到此视图的边缘。 UITableView 实际上是另一个名为“AutosizingTableView”的 class 的实例,用于使 table 视图自动调整大小(没有这个,table 的框架保持为零,table 查看数据源方法 tableView:cellForRowAtIndexPath:
从未被调用,因为 table 高度为零。此 table 视图的代码包含在本节之后。
@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
如前一节所述,这只是为了让 table 视图自动调整大小。没有这个,table 视图高度为零并保持为零,因为 tableView:cellForRowAtIndexPath:
方法从未被调用,因为 table 视图高度。
@implementation AutosizingTableView
- (CGSize)intrinsicContentSize {
return self.contentSize;
}
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
[self invalidateIntrinsicContentSize];
}
@end
有点难以确定,因为您没有提供完整的工作示例,但这可能解决您的问题。
修改见评论:
@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
我在 UITableViewCell subclass 中有两个标签。我希望两个标签都是多行标签,所以我将两个标签的行数设置为零。但问题是标签不断被截断。例如,我使右侧标签的文本大小更大,因此它更高,但随后我在左侧的第一个标签中添加了很多文本。但是,与其继续换行并添加更多行,它只是在与右侧标签大小相同时截断。
但是左边的标签本来应该显示更多的文字,也应该多占几行,但当它到达另一个标签的高度时却被截断了。这几乎就像我在两个标签之间有高度限制,但我没有。
这是我为此使用的代码。第一部分是 table 视图单元格 subclass,它有两个标签 - firstLabel(左边的)和 secondLabel(右边的)。接下来,我有具有 table 视图的视图的代码,并显示了如何配置 table 视图。最后,有一个 UITableView subclass 将 table 的 intrinsicContentSize 设置为 table 的内容大小。我添加这个是因为没有这个 table 框架总是保持在零,所以 table 视图数据源方法不会被调用。如果有人也知道更好的方法,将不胜感激。
UITableViewCell 子class
这是我的 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
主视图 - 有一个 table 视图作为子视图
这是显示的实际视图。作为较大应用程序的一部分,此视图随后显示在垂直 UIStackView 中。
此视图有一个 table 视图作为唯一的子视图,并且 table 视图使用自动版式固定到此视图的边缘。 UITableView 实际上是另一个名为“AutosizingTableView”的 class 的实例,用于使 table 视图自动调整大小(没有这个,table 的框架保持为零,table 查看数据源方法 tableView:cellForRowAtIndexPath:
从未被调用,因为 table 高度为零。此 table 视图的代码包含在本节之后。
@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
如前一节所述,这只是为了让 table 视图自动调整大小。没有这个,table 视图高度为零并保持为零,因为 tableView:cellForRowAtIndexPath:
方法从未被调用,因为 table 视图高度。
@implementation AutosizingTableView
- (CGSize)intrinsicContentSize {
return self.contentSize;
}
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
[self invalidateIntrinsicContentSize];
}
@end
有点难以确定,因为您没有提供完整的工作示例,但这可能解决您的问题。
修改见评论:
@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