1

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

iDev
  • 23,310
  • 7
  • 60
  • 85
Mahir
  • 1,684
  • 5
  • 31
  • 59
  • Change scrollview contentSize after you print the text on the UIlabel. I think it should solve your issue. – iCreative Oct 23 '12 at 07:00
  • I tried adding it after body.text = ... but the results are the same – Mahir Oct 23 '12 at 07:10
  • add one more line into the code: self.body.numberOfLines = 0; After self.by.frame = newlblFrame; I am also editing the code above. – iCreative Oct 23 '12 at 07:52
  • I already set that in interface builder, but I also tried adding it in code. Same results – Mahir Oct 23 '12 at 07:57
  • are you setting it 0?? or some other value. Also put a NSLog after getting new frame for the label. and see what is coming. It is same as old frame or it is being updated. – iCreative Oct 23 '12 at 08:01
  • Yes, it is 0. The NSLog shows that the same frame for the label – Mahir Oct 23 '12 at 19:45
  • By the way, I realized I needed a UITextView, so I changed the UILabel to a UITextView. I've also added some more details to the question – Mahir Oct 24 '12 at 02:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18511/discussion-between-icreative-and-mahir) – iCreative Oct 24 '12 at 08:37
  • Can you provide the frame of your scrollview??? Make Sure the Scrollview frame is not more than your screen resolution i,e (320x460 px). – iCreative Oct 24 '12 at 08:40
  • Have you checked the AutoresizingMask property of UITextView in IB? Is it fixed or flexible? Also in which method are you doing the above frame calculation and setting thing? – iDev Oct 26 '12 at 07:01
  • The above code is in `viewDidLoad` – Mahir Oct 26 '12 at 07:04
  • I can't seem to find the Autoresizing mask property – Mahir Oct 26 '12 at 07:08

5 Answers5

5

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.

Community
  • 1
  • 1
Mahir
  • 1,684
  • 5
  • 31
  • 59
3

i hope this will help you

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

ganesh manoj
  • 977
  • 10
  • 24
1

Try to add this :

[_scrollView setClipsToBounds:YES];
Techie
  • 44,706
  • 42
  • 157
  • 243
0

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.

iCreative
  • 1,499
  • 1
  • 9
  • 22
0

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;
Thein Hla Maw
  • 685
  • 1
  • 9
  • 28