在自定义UITableViewCell中调整UITextView的大小 [英] Resizing UITextView in custom UITableViewCell
问题描述
我有自定义 UITableViewCell
,我正在尝试根据内容大小调整其中的 UITextView
。我在iOS7上使用Autolayout。
I have a custom UITableViewCell
and I'm trying to resize the UITextView
inside it based on the content size. I'm on iOS7 and using Autolayout.
我尝试过使用以下内容:
I've tried using the following:
[cell.question setScrollEnabled:YES];
[cell.question sizeToFit];
[cell.question setScrollEnabled:NO];
和
- (CGRect)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width
{
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
CGRect finalFrame = CGRectMake(0, 0, width, size.height);
return finalFrame;
}
。我能够调整框架的大小。但问题是我无法看到明显的变化。从某种意义上说,当我记录它时,我可以看到变化。但是 UITextView
没有调整大小。我也找不到任何autolayout依赖项。
from different SO posts. I'm able to resize the frame. But the issue is I'm not able to see the change visibly. In the sense, when I log it, I can see the change. But the UITextView
doesnt resize. I cant find any autolayout dependencies either.
当我禁用AutoLayout时,它似乎工作。如何启用AutoLayout?
When I disabled AutoLayout, it seems to work. How do I do this, by enabling AutoLayout?
谢谢。
编辑
这是我的 UITextView
约束
推荐答案
你必须做这个计算
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
方法并相应调整单元格高度。
method and also resize the cell height accordingly.
如果你明白了,那没关系。或者如果您需要代码示例,请再次询问。
我想你明白了!
If you got it, it's okay. Or If you need the code sample, just ask again. I think you got it !
已更新
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITextView *tempTV = [[UITextView alloc] init];
[tempTV setText:@"your text"];
CGFloat width = self.tableView.frame.size.width - TEXT_ORIGIN_X - TEXT_END_X;
CGSize size = [tempTV sizeThatFits:CGSizeMake(width, MAX_HEIGHT)];
return (TEXT_ORIGIN_Y + size.height + TEXT_BOTTOM_SPACE);
}
这篇关于在自定义UITableViewCell中调整UITextView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!