Search code examples
iphoneobjective-ciosxcodexcode4

UIScrollView and UITextView autosizing possible?


I have a view that is pushed onto the Screen via navigation controllers:

Inside is a UIScrollView.

Then inside the UIScrollView are a few static objects like Images and Labels.

Then comes the hard bit, There is a UITextView with its text loaded from different text files of varying length.

I need to be able to have the UITextView size dynamically to its contents, and the same for the UIScrollView. Is this possible?


Solution

  • float length = [yourText length];
    textview.frame = CGRectMake(44, 87, 923, ceilf(length/142)*25);
    

    Here 25 is the constant value assumed as text font width. From this you can set scrollview frame reference to the textview frame.