【发布时间】:2016-05-19 05:52:48
【问题描述】:
我正在使用 swift 语言开发基于表格的 iOS 应用程序。我已将 UILabel 集成到 UITableview 单元格中。我想根据 UILabel 高度更改单元格高度。
【问题讨论】:
-
你的 usinf Storyboard right
标签: ios swift uitableview uilabel
我正在使用 swift 语言开发基于表格的 iOS 应用程序。我已将 UILabel 集成到 UITableview 单元格中。我想根据 UILabel 高度更改单元格高度。
【问题讨论】:
标签: ios swift uitableview uilabel
您可以将numberoflines 属性设置为0 以获取多行标签。比如,
yourLabel.numberOfLines = 0;
您可以根据字符串更改标签高度,例如,
//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
您可以通过实现委托方法来增加tableview行的高度,
heightForRowAtIndexPath
希望这会有所帮助:)
【讨论】:
首先在 Storyboard 中将 UILabel 行设置为 0。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// need to calculte height of label text
NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:@"Testo"];
CGFloat whidt = 260;
UIFont *FONT = [UIFont systemFontOfSize:15];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:@ { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +130;
}
whidt 是标签宽度。
【讨论】: