如何解决UILabel动态高度不起作用
| 我在使我的UILabel高度动态方面遇到麻烦。标签包含在自定义TableViewCell中 我设法使TableViewCellHeight动态,但我不能使标签多行。 每当我将行数设置为0(在IB以及代码中)时,标签/文本就会完全消失。而且lineBreakMode也不起作用吗?- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
Nsstring *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height,44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
//return 66;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.eventDataUp count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static Nsstring *CellIdentifier = @\"EventUpCVCell\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
self.eventUpCVCell = nil;
[[NSBundle mainBundle] loadNibNamed:@\"EventUpCVCell\" owner:self options:nil];
cell = self.eventUpCVCell;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
UILabel *lblMonth = (UILabel *) [cell viewWithTag:kLabel1Tag];
UILabel *lblTitle = (UILabel *) [cell viewWithTag:kLabel2Tag];
UILabel *lblDate = (UILabel *) [cell viewWithTag:kLabel3Tag];
//month.text = eventUp.month;
lblTitle.text = eventUp.title;
lblDate.text = eventUp.date;
//lblDate.lineBreakMode = UILineBreakModeWordWrap;
lblDate.numberOfLines = 1;
lblMonth.text = [Nsstring stringWithFormat:@\"%@ \",eventUp.month];
//cell.textLabel.text = eventUp.title;
//cell.detailTextLabel.text = eventUp.date;
//NSLog(@\"Hello\");
cell.accessoryType = UITableViewCellAccessorydisclosureIndicator;
// Set up the cell
return cell;
}
解决方法
创建标签框后必须对其进行设置,实际上是使用CGRectZero Frame创建它
尝试:
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,size.width,size.height)];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。