无法让 UILabel 显示超过 3 行
-
13-09-2019 - |
题
我有一个包含一堆单元格的 tableivew,我试图让 uilabel 显示超过 3 行。我适当地设置了换行模式和行数,但它仍然没有显示超过三行。有什么建议么?表格单元格会自动调整其高度以适应字符/行数,但文本显示三行,然后显示一个椭圆(当您单击单元格时,它会转到另一个显示全文的视图。
下面是我必须创建和显示 UILabel 的代码:
self.commentLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:YES];
self.commentLabel.textAlignment = UITextAlignmentLeft; // default
self.commentLabel.lineBreakMode = UILineBreakModeWordWrap;
self.commentLabel.numberOfLines = 0; // no limit to the number of lines
[myContentView addSubview:self.commentLabel];
[self.commentLabel release];
我希望整个评论显示在表格单元格中。
解决方案
好像标签的矩形是太小,不适合所有文本...你必须作出更大的矩形,以便它不显示的椭圆和显示整个文本
其他提示
采用自动布局设计理念, 不要为 UILabel 设置高度限制并设置 no。行数为 0.
自动布局根据标签文本自动处理标签的动态高度。如果标签具有单行文本,则它将仅占用单行空间。如果标签有多于一行,那么它将根据文本大小和显示文本所需的行数调整标签大小。
- 分配并实现 tableview dataSource 和 delegate
- 分配
UITableViewAutomaticDimension
到 rowHeight 和预计的RowHeight - 实现委托/数据源方法(即
heightForRowAt
并返回一个值UITableViewAutomaticDimension
到它)
-
目标C:
// in ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property IBOutlet UITableView * table;
@end
// in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
self.table.rowHeight = UITableViewAutomaticDimension;
self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
看看这个答案: 在 UITableViewCell 中动态调整 UILabel?
不隶属于 StackOverflow