1
votes

I have a UITextView, which sets its text dynamically from an RSS feed. The textview is a subview of a UIScrollview. Ultimately, I am creating a mobile newspaper app, so the user should be able to scroll through the text (and other subviews).

After creating the views in IB, I added

NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.URL] encoding:NSUTF8StringEncoding error:&error];
sourceCode = [self parseHTMLText:sourceCode];
CGSize maximumLabelSize = CGSizeMake(320,9999);
CGSize txtStringSize = [sourceCode sizeWithFont:self.body.font
                              constrainedToSize:maximumLabelSize];
CGRect newlblFrame = CGRectMake(0, 0, 320, txtStringSize.height);
self.body.frame = newlblFrame; //body is the textview
self.body.text = sourceCode;
self.scroll.contentSize=CGSizeMake(320, self.body.frame.size.height+300); //scroll is scrollview

A typical NSLog will display body frame = {{0, 0}, {320, 2088}} scrollview frame = {{0, 0}, {320, 417}} scrollview content size = {320, 2388}

However, when I run the app, body maintains its interface builder height of around 196 and the scrollview won't scroll (when i move it, it just bounces back to its original position)

On a side note, when I try to manually change the frame of the textview with CGRectMake, the NSLog shows the correct height, but the view doesn't appear different. I made sure that it's hooked up correctly in IB, because I can adjust other properties, like background color.

EDIT:

After I set the cliptoBounds property of the textview to NO, the textview now adjusts its height and tries to show the entire text. However, it cuts off at the end of the screen, and I still cannot scroll.

Here is what I see currently. I made the scrollview background color gray for convenience. I'm not sure why part of the scrollview is partially in white and and partially gray. (Title) is a separate label btw)

enter image description here

5
Change scrollview contentSize after you print the text on the UIlabel. I think it should solve your issue.iCreative
I tried adding it after body.text = ... but the results are the sameMahir
add one more line into the code: self.body.numberOfLines = 0; After self.by.frame = newlblFrame; I am also editing the code above.iCreative
Let me know if it works.iCreative
I already set that in interface builder, but I also tried adding it in code. Same resultsMahir

5 Answers

5
votes

I fixed the problem by moving the code from viewDidLoad to viewDidAppear. Apparently this is an Xcode 4.5 specific issue, resulting from the AutoLayout feature overriding the logic in the viewDidLoad method. A better explanation can be found here.

3
votes

i hope this will help you

self.scrollview.contentSize=CGSizeMake(320, label.frame.size.height+30);

1
votes

Try to add this :

[_scrollView setClipsToBounds:YES];
0
votes

Try this:

CGSize maximumSize = CGSizeMake(200.0, 30.0); // Specify your size. It was for my screen. 
            UIFont *txtFont = <Ur Font size & Var>;
            //new
            //fontValue = txtFont;
            //new

            lblValue.font = txtFont; 
            lblValue.lineBreakMode = UILineBreakModeWordWrap;

            CGSize txtStringSize = [commentTxt sizeWithFont:txtFont 
                                          constrainedToSize:maximumSize 
                                              lineBreakMode:lblValue.lineBreakMode];

            CGRect newlblFrame = CGRectMake(105.0, y, 200.0, txtStringSize.height);

            lblValue.frame = newlblFrame;          
            lblValue.text = @"Your String";

After this set scroll Content size. Hope it'll work for you. It worked for me.

0
votes

Try to solve in the following way.

// adjust the height of UITextView with content height
CGRect frame = self.body.frame;
frame.size.height = self.body.contentSize.height;
self.body.frame = frame;

// adjust UIScrollView's height with the height of UITextView
self.scroll.frame = frame;