Search code examples
iphoneobjective-cipaduiscrollviewcore-text

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 !


Solution

  • 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];