我正在开发一个视图,该视图允许我显示大量文本,并通过一次显示一个页面并允许用户左右滚动来移动它。
我想我可以通过将 UITextView 放在 UIScrollView 中来做到这一点,该 UIScrollView 的 pagingEnabled 属性设置为 YES,同时将内容宽度设置为屏幕的倍数。
我看到的只是第一页,虽然我可以向右滑动并看到其他空白页。任何人都可以给出任何提示。这是代码...
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];
视图是在 IB 中创建的,UIScrollView 被添加到 IB 中,其中“scroller”是插座的名称。
我已经为此工作了一段时间,并且觉得我缺少有关 UIScrollView 的一些基本内容,因为我似乎无法使其正常工作。
保罗