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
?
我想根据标签的高度来调整单元格的高度,根据文本来调整标签的高度。或者有什么方法可以根据在UITextView中输入的文本来调整单元格的高度?
2 个解决方案
#1
17
THIS METHOD IS DEPRECATED SINCE iOS 7.0.
自ios7.0以来,不赞成使用此方法。
There is a UITableView
delegate method called heightForRowAtIndexPath
that is called before you create a cell or a table.
有一个名为heightForRowAtIndexPath的UITableView委托方法,在创建单元格或表之前调用它。
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.
您可以使用传递给它的NSIndexPath来获取特定行的文本,并使用uistringdraw的sizeWithFont方法。h来计算这一行的CGSize。
For example:
例如:
CGSize size = [text sizeWithFont:font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
And finally you would return size.height
.
最后返回size。height。
#2
6
--For iOS7--
——为iOS7
Basing this off of Josh Vera's answer … place this in heightForRowAtIndexPath.
根据Josh Vera的回答,把这个放在heightForRowAtIndexPath中。
I have my table data stored in an NSArray *tableData.
我的表数据存储在NSArray *tableData中。
// set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar)
CGFloat widthOfMyTexbox = 250.0;
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get a reference to your string to base the cell size on.
NSString *cellText = [self.tableData objectAtIndex:indexPath.row];
//set the desired size of your textbox
CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT);
//set your text attribute dictionary
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName];
//get the size of the text box
CGRect textsize = [cellText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
//calculate your size
float textHeight = textsize.size.height +20;
//I have mine set for a minimum size
textHeight = (textHeight < 50.0) ? 50.0 : textHeight;
return textHeight;
}
I haven't tested it for iOS<7, but I believe it should work for that as well.
我还没有对iOS<7进行测试,但我认为它也应该适用。
#1
17
THIS METHOD IS DEPRECATED SINCE iOS 7.0.
自ios7.0以来,不赞成使用此方法。
There is a UITableView
delegate method called heightForRowAtIndexPath
that is called before you create a cell or a table.
有一个名为heightForRowAtIndexPath的UITableView委托方法,在创建单元格或表之前调用它。
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.
您可以使用传递给它的NSIndexPath来获取特定行的文本,并使用uistringdraw的sizeWithFont方法。h来计算这一行的CGSize。
For example:
例如:
CGSize size = [text sizeWithFont:font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
And finally you would return size.height
.
最后返回size。height。
#2
6
--For iOS7--
——为iOS7
Basing this off of Josh Vera's answer … place this in heightForRowAtIndexPath.
根据Josh Vera的回答,把这个放在heightForRowAtIndexPath中。
I have my table data stored in an NSArray *tableData.
我的表数据存储在NSArray *tableData中。
// set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar)
CGFloat widthOfMyTexbox = 250.0;
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get a reference to your string to base the cell size on.
NSString *cellText = [self.tableData objectAtIndex:indexPath.row];
//set the desired size of your textbox
CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT);
//set your text attribute dictionary
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAttributeName];
//get the size of the text box
CGRect textsize = [cellText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
//calculate your size
float textHeight = textsize.size.height +20;
//I have mine set for a minimum size
textHeight = (textHeight < 50.0) ? 50.0 : textHeight;
return textHeight;
}
I haven't tested it for iOS<7, but I believe it should work for that as well.
我还没有对iOS<7进行测试,但我认为它也应该适用。