GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

Adjusting Scrollview to Adapt to Length of Text

I have a scroll detail view in which I am displaying some text from a database using Core Text(iPhone and iPad). At the moment my scroll view is a default set length. However as some text entries are longer than others, this means I have a lot of white space underneath the text for some detail views.

Could anyone suggest how I can dynamically change the length of the scroll view according to the length of the text ?

All ideas welcome.

Thanks !

Upvotes: 0

Views: 277

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 808

try this...

NSString *text = @"your text from db";

CGSize constraint = CGSizeMake(textview.frame.size.width, 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

textview.frame = CGRectMake(0, 0, size.width, size.height);

[yourScrollView setContentSize:CGSizeMake(textview.frame.size.width,textview.frame.size.height];

Upvotes: 1

Groot
Groot

Reputation: 14261

Try something like:

float yourDynamicHeight = 0;
for(UILabel *lbl in yourArrayOfLabels) {
    yourDynamicHeight += lbl.frame.size.height;
}
// Perhaps add some arbitrary extra height

[yourScrollView setContentSize:CGSizeMake(yourWidth,yourDynamicHeight];

Note: Writing this on the go so have not compiled tested it.)

Hope it helps!

Upvotes: 0

Related Questions