0

According to this tutorial, I had an UITextView inside an UITableViewCell . When UITableView has been loaded , I want the UITextView and the UITableViewCell resize their height.

But I did everything based on the tutorial, the UITextView can not display all the content , part of UITextView content is still needed to scroll to display.

Here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *contentTableIdentify = @"contentTableIdentify";

    UITableViewCell *cell = nil;
//UILabel *label = nil;
    UITextView *textView = nil;
    cell = [self.tableView dequeueReusableCellWithIdentifier:contentTableIdentify];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentTableIdentify];

        textView = [[UITextView alloc] initWithFrame:CGRectZero];
        [textView setTextColor:[UIColor blackColor]];
        [textView setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
        [textView setBackgroundColor:[UIColor clearColor]];
        [textView setTag:1];
        [textView setEditable:NO];
        [[cell contentView] addSubview:textView];
    }

    NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
    CGSize constraint = CGSizeMake(320 - (10 * 2), 99999.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    if (!textView)
        textView = (UITextView *)[cell viewWithTag:1];

    [textView setText:text];
    [textView setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [self.contentArray objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 44.0f);

    return height + (10 * 2);
}
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
lixiaoyu
  • 378
  • 5
  • 18

2 Answers2

0

You need to factor in the padding of the text view when calculating the height of your cell.

If you do the following:

//  Add 16px (8+8) to account fr the UITextView padding    
CGSize constraint = CGSizeMake(320 - (10 * 2) -16, 99999.0f);
GSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]

CGFloat height = MAX(size.height, 44.0f);

return height + (10 * 2) + (8*2);

You'll find more information on this question

Community
  • 1
  • 1
Eric Genet
  • 1,260
  • 1
  • 9
  • 19
0

textview have default padding inside

enter image description here

using UILabel instead of UITextview or remove the padding

textView .contentInset = UIEdgeInsetsMake(-11,-8,0,0);
adali
  • 5,977
  • 2
  • 33
  • 40