views:

517

answers:

1

Hi all :)

I am displaying some text from a sqlite3 database and would like the UITextView to resize automatically depending on the amount of text. I can do this fine as follows:

TextLabel.scrollEnabled = YES; TextLabel.userInteractionEnabled = YES;

[TextLabel setFrame:CGRectMake(55.10, 26.15, TextLabel.contentSize.width, TextLabel.contentSize.height)];

This works fine. I am struggling when trying to set a maximum height.

So the UITextView will resize to the text unless it reaches a maxiumum height in which case the user will have to use the scroll bar to view the remaining text.

Any help would be highly appreciated

A: 

Here's a way to discover the desired height for some multiline text:

int myWidth = 300;
int maxHeight = 9999;
NSString *myString = @"lorem ipsum dolor yadda";
UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont 
                           constrainedToSize:CGSizeMake(myWidth, maxHeight)
                               lineBreakMode:textLabel.lineBreakMode];

textLabel.frame = CGRectMake(10, 10, myWidth, myStringSize.height);

I got this from nevan's answer to this previous question:

Reference: Apple's docs on NSString UIKit additions.

Tyler