问题描述
我想根据标签的高度和标签的高度调整单元格的高度。或者有什么方法可以根据在 UITextView
中输入的文字来调整单元格的高度?
I want to resize cell's height according to the label's height and label's height according to text. Or is there any way I can resize the cell's height according to the text entered in UITextView
?
推荐答案
此方法自iOS 7.0开始被弃用。
有一个 UITableView
在创建单元格或表之前调用的名为 heightForRowAtIndexPath
的委托方法。
There is a UITableView
delegate method called heightForRowAtIndexPath
that is called before you create a cell or a table.
您可以使用传递给它的 NSIndexPath
来获取特定行的文本并使用 sizeWithFont
来自 UIStringDrawing.h
的方法,为该行计算 CGSize
。
You could use the NSIndexPath
passed to it to get the text at a specific row and use the sizeWithFont
method from UIStringDrawing.h
to compute a CGSize
for that row.
例如:
CGSize size = [text sizeWithFont:font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
最后你会返回 size.height
。
这篇关于将UITableViewCell的大小动态调整为UILabel的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!