views:

699

answers:

1

Hello,

I am working on a view that would allow me to display large amounts of text, and to move through it by showing a page at a time and allowing the user to scroll right and left.

I thought I could do it by putting a UITextView inside a UIScrollView which has the pagingEnabled property set to YES along with setting the content width to a multiple of the screen.

All I see is the first page, although I can flick to the right and see other blank pages. Can anybody give any hints. Here is the code...

label = [[UITextView alloc] initWithFrame: scroller.frame];

label.editable = NO;

label.scrollEnabled = NO;

label.multipleTouchEnabled = NO;

label.userInteractionEnabled = NO;

label.font = [UIFont systemFontOfSize: 14.0];

// Get text 
std::string str;

// Next line gets a single string, which is from 2000 to 8000

//characters long
str = GetStringContent();

NSString * nsStr = [NSString stringWithCString: str.c_str()];

// Measure text that will display in one page.
CGRect rect = CGRectMake(0.0, 0.0, scroller.frame.size.width, scroller.frame.size.height);
CGSize calcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 
constrainedToSize: rect.size lineBreakMode: UILineBreakModeWordWrap];
rect.size = calcSize;

CGRect maxRect = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 99999.0);

CGSize maxCalcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 

constrainedToSize: maxRect.size lineBreakMode: UILineBreakModeWordWrap];

float pages = maxCalcSize.height / self.view.bounds.size.height;

int numberOfPages = (int)ceil(pages);

// Set text
label.text = [NSString stringWithCString: str.c_str()];

// Setup scroller
scroller.scrollEnabled = YES;

scroller.pagingEnabled = YES;

scroller.bounces = YES;


scroller.contentSize = CGSizeMake(label.frame.size.width * numberOfPages, scroller.frame.size.height);


scroller.contentOffset = CGPointMake(0, 0); 

scroller.frame = rect;

scroller.clipsToBounds = YES;

[scroller addSubview: label];

[label sizeToFit];

The view is created in IB, and the UIScrollView was added into IB with "scroller" being the name of the outlet.

I've been working on this for a while, and feel like I am missing something fundamental about UIScrollView, because I can't seem to get this working.

Paul

+1  A: 

Paging just means that the scrollview will "snap" to the width of the scroll view. Meaning that as you swipe left, the next page will come but stop at the increment of 320, ow whatever width you have it.

To do what you wish to do will require a little bit more code than you have. I would start by making a 3 text views and keeping them in an array, you only ever show 1 at a time though.

You will need to implement logic to figure out what to display on these textviews (which part of the text to show) and use this logic to fill the current page, the previous page, and the next page.

You will need to listen to the uiscrollview delegate methods to know which page you are on, and you will need to recalculate and fill the pages as the user scrolls

If the user scrolls to the next page, you take the textview you had for 2 pages back, and move it to become the new "next" page. This is called "recycling" your views and there are plenty of questions on SO regarding this with sample code

coneybeare
Many thanks for explaining this. I have done a lot of research on this and this is the clearest explanation I have found, and I have done alot of googling. Related to implementing logic to figure out what to display, is there a method for NSString similar to sizeWithFont: constraintToSize: which will also indicate how much of the text is drawn, within the constrained area? Thanks.
I am not sure of any off hand but you can check the docs. I take it that your text is not formatted with line breaks? Perhaps using a constant font size, a monospaced font and a character count (not breaking up words) is the best way to do this
coneybeare