我正在尝试将表格中的一行调整为textView的高度,以便用户不必滚动textView,而是滚动表格。我到目前为止所实现的似乎大部分时间都在工作,虽然有时行高有点太小,迫使用户稍微滚动textview,或者行高太多会在文本结束后产生大量的空白。我怎样才能恰到好处地构建它?
代码:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Set the textview frame
CGRect frame = self.contentTextView.frame;
frame.size.height = [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 60;
self.contentTextView.frame = frame;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return the height for the rows
if (indexPath.section == 0) {
return 60;
}
if (indexPath.section == 3) {
// Size content row based on text
return [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 100;
}
return 44;
}
答案 0 :(得分:0)
试试这个
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* str = ... // here is a string which will be in text view
CGSize boundingSize = CGSizeMake(textView.frame.size.width - 20, CGFLOAT_MAX);
CGSize textSize = [str sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];
return textSize.height + 40; // add aditional space for margins
}