2

I'm loading my UITextView from an XML feed so the text is constantly changing. I'm trying the following to resize the cell and text, and it resizes the cell but not the text view, it's just not displaying the text view, or sometimes just part of it.

Any tips along the right way will be really appreciated;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    AssessObject *newObj1;
    newObj1=[totalArray objectAtIndex:indexPath.row];

    NSString *cellText = newObj1.routeText;

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:16.0];
    CGSize constraintSize = CGSizeMake(188.0, CGFLOAT_MAX);
    CGSize textViewSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];


    return textViewSize.height + 200;


}

4 Answers 4

3

Check the AutoresizingMask of the UITextView you have added to your cell.

Make sure it is set so that it resizes with the cell (you can do this either in IB, or via code using the UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingMaskFlexibleWidth value)

Sign up to request clarification or add additional context in comments.

2 Comments

It's now set to scale to fit, however being really weird with the redrawing / original drawing of the cells. First time it shows them it's half there, you scroll down and back up and then the full text is there
I was not talking about the scaleToFit mode but the autoresizingMask ; make sure you have everything setup correctly here so that it resizes according to its superview. By the way, it seems to me after watching your screencast that it may also be because of a typical misuse of the reuse mechanism of UITableViewCells.
1

Set the textView size equal to textView's contentSize. Something like this:

CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);

I'm making the height of the textView equal to the height of it's contentView.

Comments

0

setup the font size, text content and frame rect of the UITextView, then [UITextView sizeToFit] to calculate the contentSize of UITextView, then calculate the row height with the size of contentSize.

Don't forget to resize the frame rect of UITextView;

Comments

0

I have used Bruno's idea to resize my TextView according to the amount of text, when I put it to the ScrollView. This is how I do this. A bunch of constants there, that you may not use. It is important to resize textView after adding it to the ScrollView.

    // Programmatic creation of scroll view layout 
NSString *text = @"Your text";
CGFloat textOffSetInColumn = 10;
CGFloat infoTextWidth = 196;
CGFloat infoOffsetVertical = 36;
CGFloat initialTextHeight = 50;

// Create textView with initial height
CGRect infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, initialTextHeight);
infoTextView = [[UITextView alloc] initWithFrame:infoTextFrame];
infoTextView.text = text;
[scrollView addSubview:infoTextView];

// Resize textView
CGFloat infoTextHeight = infoTextView.contentSize.height;
infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, infoTextHeight);
infoTextView.frame = infoTextFrame;

If you want to change the size of TextView and center it to the previous center, you can use this code:

   // Changing size of TextView and centering
    CGPoint center = self.categoryTextView.center;
    self.categoryTextView.frame = CGRectMake(_categoryTextView.frame.origin.x, _categoryTextView.frame.origin.y, _categoryTextView.frame.size.width, _categoryTextView.contentSize.height);
    self.categoryTextView.center = center;

Instead of categoryTextView use your own Outlet name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.