开发者

uiscrollview for scrolling a very, very tall page

From top to bottom I have UIView, UIScrollView, a UIImage, a UILabel, a UITextView and a UIButton.

My reason behind the top-most UIScrollView was so the whole vertical content would scroll.

What I really need a substitute for is the UITextView (5th down) because the UITextView is a subclass of UIScrollView. And this substitute must accomodate the very tall column of formatted text.

What I don't want is a scrollable object in the middle of the page; I want the whole开发者_Python百科 page to be scrollable.

One more thing ... please note there's a button immediately below this tall column of text.

John Love


THANK YOU! Ashack and Pentagp. I guess I finally had to surrender and dump the idea that IB could be used for all GUI ...

The entire UIView contains a UILabel, a UIImage, a UITextView and a UIButton at the very bottom.

The goal here was to make this entire content scrollable.

Thanks to you guys at stackoverflow.com:

  1. using IB, un-check "Scrolling Enabled" for the UITextView
    because it's a concrete type of UIScrollView
  2. using IB, drag a UIScrollView to the UIView and match sizes and insure that the UIScrollView encloses all sub-views
  3. make the UITextView height = its contentSize.height
  4. move the UIButton to below this UITextView

and then, thanks to iphonedevsdk.com:

  1. adjust the contentSize.height of the top-most UIScrollview to include the height of the re-positioned UIButton

Problem solved!!!


Your question is not very specific, but I think you're asking how to make a text view that does not scroll, and expands to fit the text. You can do this with UILabel, but it's a multi-step process. The same will work for UITextView if you disable scrolling and set the contentSize to match the frame dimensions.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)]; 
label.font = [UIFont systemFontOfSize:24];
NSString *yourTextString = @"your text";
CGSize maximumLabelSize = CGSizeMake(100, 500);
CGSize expectedLabelSize = [yourTextString sizeWithFont:label.font 
                    constrainedToSize:maximumLabelSize 
                    lineBreakMode:yourLabel.lineBreakMode];
CGRect labelFrame = label.frame;
labelFrame.size.width = expectedLabelSize.width;
labelFrame.size.height = expectedLabelSize.height;
label.frame = labelFrame;

Then from there, use the label's frame to set the position of the button and all elements below it.


If I understand your problem, you don't want nested scroll. So set yourTextView.scrollEnabled = NO; //Do it programmatically our in the nib

Also in order to display all the text set yourTextView frame height to its content height:

    yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, 
                                    yourTextView.frame.origin.y,
                                    yourTextView.frame.size.width,
                                    yourTextView.contentSize.height + (yourTextView.contentSize.width > yourTextView.frame.size.width ? (yourTextView.contentSize.width * yourTextView.contentSize.height / (yourTextView.contentSize.width - yourTextView.frame.size.width)) - yourTextView.contentSize.height :0)));

/*
If textView is dynamically filled, you have to check if yourTextView.contentSize.width is bigger than yourTextView.frame.size.width then add proportionally what is remaining to the height. 
*/

At the end change your button position if needed:

yourbutton.frame = CGRectMake(yourbutton.frame.origin.x, 
                                yourTextView.frame.origin.y + yourTextView.frame.size.height + 20, //If you want your button to be 20 point after your textView
                                yourbutton.frame.size.width,
                                yourbutton.frame.size.height);

Hope that will help you...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜