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