我从服务器获取一些文本,并将它放到UILabel中,它将被添加到UITableViewCell中,并且每次它可以很小或很大或多行时更改此文本。我的问题是我如何使这个UILabel自动适应文本(多行)并调整UITableViewCell的大小以适应UILabel?如何调整UITableViewCell的文本大小?
回答
你可以在你的tableView的数据源如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// for the sake of illustration, say your model for this table is just an array of strings you wish to present.
// it's probably more complex, but the idea is to get the string you want to present for the
// cell at indexPath
NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];
// you can get fancier here, and dynamically get the font from the UITextView prototype
// but for simplicity, just copy the font size you configured the text view with in your nib
CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];
// this is a little funky, because for it to be just right, you should format your prototype
// cell height to be a good-looking height when your text view has a zero height.
// the basic idea here is that the cell will get taller as the text does.
return tableView.rowHeight + size.height;
}
然后,当您将单元配置,获取字符串和大小相同的方式,并设定UITextView的框架相匹配的大小
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];
CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];
UITextView *myTextView = (UITextView *)[cell viewWithTag:kMY_TEXT_VIEW_TAG]; // make this tag match a tag you give the text view in the prototype cell
myTextView.frame = CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, size.width, size.height);
// the rest of your configure cell
}
您需要实现这一点: -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelsize;
UILabel * textDesc1 = [[UILabel alloc] init];
[textDesc1 setNumberOfLines:0];
textDesc1.text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
[textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
[textDesc1 release];
return (CGFloat)labelsize.height;
}
- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelsize;
UILabel *commentsTextLabel = [[UILabel alloc] init];;
[commentsTextLabel setNumberOfLines:0];
[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
commentsTextLabel.text=text;
[commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(0, 0, 320, labelsize.height);
[cell.contentView addSubview:commentsTextLabel];
[commentsTextLabel release];
}
我不认为你想回答标签高度作为表格单元格的高度。我认为你想增加高度(格式化一行后)到附加行的高度,如果发生自动换行。 – danh 2012-03-21 05:46:37
如果您仅返回标签高度,则会得到一个非常小的表格行,标签上方/下方没有填充。对于类似的情况,我已经为上面/下面的填充设置了自己的常量(基于标准标签如何看上去只有一行),然后在从'heightForRowAtIndexPath'返回之前将它们添加到标签高度。 – rickster 2012-03-21 05:49:56
你可以简单地做return(CGFloat)labelsize.height + 30;您可以添加一个数字以使表格单元高度比标签高度大,这取决于您决定添加的数量。 – Gypsa 2012-03-21 07:46:42
- 1. 在uitableviewcell中调整文本的大小
- 2. 如何调整UITableViewCell的imageview大小
- 3. 调整大小的UITableViewCell到
- 4. 调整UITableViewCell的大小
- 5. 调整大小UITableViewCell textLabel
- 6. 基于文本输入动态调整UITableViewCell的大小
- 7. 如何在调整窗口大小时调整Scene2D TextButton中的文本大小?
- 8. JavaFx文本大小调整
- 9. 通过手势调整UITableViewCell的大小
- 10. UITextField UITableViewCell中的自动调整大小
- 11. 调整UITextView和UITableViewCell的大小
- 12. 在UITableViewCell中调整UIImageView的大小
- 13. 如何动态调整UITableViewCell中两个文本标签宽度的大小?
- 14. 当文本框调整大小时,窗口大小调整
- 15. 当窗口调整大小时调整文本大小Python/Kivy
- 16. 如何根据标签大小调整文本大小?
- 17. 没有在UITableViewCell中调整大小
- 18. 自动调整大小自定义UITableViewCell
- 19. UITableViewCell backgroundView图像调整大小?
- 20. NSLayoutConstraint错误与调整大小UITableViewCell
- 21. 我如何根据CustomCell的高度来调整UITableViewCell的大小?
- 22. 防止调整大小的文本区域调整整个表的大小
- 23. 如何调整UITableViewCell的大小以适应其内容?
- 24. 如何调整自定义UITableViewCell的大小以适应内容?
- 25. 如何在UITableViewCell中调整UIImageView的大小?
- 26. 如何在可调整大小的UITableviewCell中添加UITextView
- 27. 如何让UITableView行高自动调整到UITableViewCell的大小?
- 28. 如何调整uitableviewcell附件按钮的大小
- 29. $ .removeAttr()调整输入文本的大小
- 30. 调整文本框的大小?
这是在栈上重复的问题,所以我只UPD ated stack with this solution to this answer.Just check out this solution:** [如何在UITextView里面创建一个UITableViewCell,以UITextView的形式动态调整它的高度](http://stackoverflow.com/a/4242916/434898)** – 2012-03-21 05:26:27